DrawerLayout's item click - When is the right time to replace fragment?

后端 未结 7 776
轻奢々
轻奢々 2020-12-07 11:16

I\'m developing an application which uses the navigation drawer pattern (With DrawerLayout).

Each click on a drawer\'s item, replaces the fragment in the main contai

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 11:57

    I know this question is old but I ran into the same problem and figured I would post my solution as I think it is a better implementation than adding a hardcoded delay time. What I did was use the onDrawerClosed function to verify that the drawer IS closed before doing my task.

    //on button click...
    private void displayView(int position) {
        switch (position) {
        //if item 1 is selected, update a global variable `"int itemPosition"` to be 1
        case 1:
            itemPosition = 1;
            //();
            break;
        default:
            break;
        }
    
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        mDrawerLayout.closeDrawer(mDrawerList); //close drawer
    }
    

    and then in onDrawerClosed, open the corresponding activity.

    public void onDrawerClosed(View view) {
        getSupportActionBar().setTitle(mTitle);
        // calling onPrepareOptionsMenu() to show action bar icons
        supportInvalidateOptionsMenu();
        if (itemPosition == 1) {
            Intent intent = new Intent(BaseActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    }
    

提交回复
热议问题