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

前端 未结 8 505
鱼传尺愫
鱼传尺愫 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:09

    In onCreate(), following code will load the first item's fragment upon first start:

    if (savedInstanceState == null) {
        navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
    }
    

    Thanks to calvinfly for this comment.

    0 讨论(0)
  • 2021-02-04 06:11

    Instead of normal listener ...

    navView.setNavigationItemSelected(new Navigation.View.OnNavigationItemSelectedListener() {bla, bla, bla})
    

    Create the listener as an Obj:

    NavigationView.OnNavigationItemSelectedListener navViewListener;
                navView.setNavigationItemSelectedListener(navViewListener = new NavigationView.OnNavigationItemSelectedListener() {bla, bla, bla})
    

    ...and use the Obj to trigger the listener event:

    navViewListener.onNavigationItemSelected(navView.getMenu().getItem(0));
    

    ...where getItem(0) is the first menu item.

    Use a method getItem(0).setChecked(true) or android:checked="true" at its menu item XML definition.

    0 讨论(0)
  • 2021-02-04 06:18

    You could also use navigationView.setCheckedItem(R.id.default)(javadoc) after you setup your navigationview.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-04 06:19

    1.) To land to the HomeFragment initially, use this inside your onCreate() in MainActivity:

    Fragment fragment = new HomeFragment();
    // replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }
    

    2.) To set the item as selected in navigationDrawer set the item as checked in navigation_menu.xml

     android:checked = "true"
    
    0 讨论(0)
  • 2021-02-04 06:22

    Add android:checked="true" to your first menu item.

    And manually select one item, using

    getSupportFragmentManager().beginTransaction().replace(R.id.frame, postListFragment).commit();
    

    to open fragment.

    0 讨论(0)
提交回复
热议问题