我的代码工作得很完美:每次单击导航抽屉中的项目时,项目就会被选中。

当然,我想启动应用程序与默认片段(家),但导航抽屉没有选择的项目。如何以编程方式选择该项目?

public class BaseApp extends AppCompatActivity {

    //Defining Variables
    protected String LOGTAG = "LOGDEBUG";
    protected Toolbar toolbar;
    protected NavigationView navigationView;
    protected DrawerLayout drawerLayout;

    private DateManager db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_layout);

        navigationView = (NavigationView) findViewById(R.id.navigation_view);


        // set the home/dashboard at startup

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame, new DashboardFragment());
        fragmentTransaction.commit();

        setNavDrawer();
    }

    private void setNavDrawer(){

        // Initializing Toolbar and setting it as the actionbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Initializing NavigationView

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state

                // I THINK THAT I NEED EDIT HERE...

                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();


                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {    

                    //Replacing the main content with ContentFragment 

                    case R.id.home:

                        DashboardFragment dashboardFragment = new DashboardFragment();
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
                        fragmentTransaction.commit();
                        return true;
[...]

我认为我需要在这里进行编辑:

if (menuItem.isChecked()) menuItem.setChecked(false);
                    else menuItem.setChecked(true);

或者在onCreate中在App启动时使用FragmentTransaction。

谢谢你的支持。


当前回答

你可以这样做, nav_view.getMenu () .findItem (R.id.menutem) .setChecked(真正的)

其他回答

为导航抽屉的个别项目做一个选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/darkBlue" android:state_pressed="true"/>
    <item android:drawable="@color/darkBlue" android:state_checked="true"/>
    <item android:drawable="@color/textBlue" />
</selector>

在你的NavigationView中做一些改变

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemBackground="@drawable/drawer_item"
        android:background="@color/textBlue"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/activity_main_drawer"
        />

出于某种原因,最好使用ID来查找MenuItem。

drawer.getMenu().findItem(R.id.action_something).setChecked(true);

谷歌的“哦,太棒了”支持库总是有问题。如果你想检查一个项目而不降低你的支持库版本,只需要在设置检查状态之前设置checkable。

MenuItem item = drawer.getMenu().findItem(R.id.action_something);
item.setCheckable(true);
item.setChecked(true);

如果你在菜单xml文件中设置checkable,它也可以工作

bottomNavigationView.setSelectedItemId(R.id.menuItem);

上面为我工作,但我已经把它放在onResume()方法。放置在 onNavigationItemSelected(@NonNull MenuItem项)导致我在活动之间来回切换时出现问题

   private BottomNavigationView bottomNavigationView;
    
     @Override
        public void onResume(){
            super.onResume();
            bottomNavigationView.setSelectedItemId(R.id.menuItem);
    
     } 

你可以这样做, nav_view.getMenu () .findItem (R.id.menutem) .setChecked(真正的)