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

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

提交回复
热议问题