How to select the first item in a navigation drawer and open a fragment on application start

前端 未结 8 508
鱼传尺愫
鱼传尺愫 2021-02-04 05:32

I have created MainActivity with NavigationView. When Activity is opened I want to automatically select the first item in the navigation d

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 06:18

    This can be done even better while considering orientation and other configuration changes. We could select whatever nav drawer menuitem depending on whether we are coming from a previous state. Check: For the Navigation drawer wielding Activity:-

    public static final String SELECTED_NAV_MENU_KEY = "selected_nav_menu_key";
    // The selected grid position
    private int mSelectedNavMenuIndex = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
    
       ...........................................................
    
        navigationView.setNavigationItemSelectedListener(this);
    
        if (savedInstanceState != null) {
            // Recover assets
            mSelectedNavMenuIndex = savedInstanceState.getInt(SELECTED_NAV_MENU_KEY);
    
            // Recover menu as selected
            MenuItem menuItem = navigationView.getMenu().getItem(mSelectedNavMenuIndex);
            toggleNavMenuItemCheck(menuItem);
            navigationView.getMenu().performIdentifierAction(menuItem.getItemId(), mSelectedNavMenuIndex);
            return;
        } else {
            MenuItem menuItem = navigationView.getMenu().getItem(mSelectedNavMenuIndex);
            toggleNavMenuItemCheck(menuItem);
            navigationView.getMenu().performIdentifierAction(menuItem.getItemId(), mSelectedNavMenuIndex);
        }
    }
    

    The toggle method that helps uncheck or check the menu item

    private void toggleNavMenuItemCheck(MenuItem menuItem) {
        if (menuItem.isChecked()){
            menuItem.setChecked(false);
        }  else {
            menuItem.setChecked(true);
        }
    }
    

    This is how I save the state of the selected menu item. Check:-

    @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            switch (id) {
                case R.id.nav_explore:
                    showExploreFragment(null);
                    mSelectedNavMenuIndex = 0;
                    break;
                case R.id.nav_orders:
                    mSelectedNavMenuIndex = 1;
                    break;
                case R.id.nav_settings:
                    mSelectedNavMenuIndex = 2;
                    break;
                default:
                    showExploreFragment(null);
                    mSelectedNavMenuIndex = 0;
            }
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    
    // Save any important data for recovery
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt(SELECTED_NAV_MENU_KEY, mSelectedNavMenuIndex);
        }
    

    NB: The line with code:

    navigationView.getMenu().performIdentifierAction(menuItem.getItemId(), mSelectedNavMenuIndex);
    

    Can be replaced by the code:

    onNavigationItemSelected(menuItem);
    

提交回复
热议问题