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

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

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。

谢谢你的支持。


当前回答

以下代码将只使菜单项被选中:

navigationView.setCheckedItem (id);

要选择并打开菜单项,请在上面的行后添加以下代码。

onNavigationItemSelected (navigationView.getMenu () .getItem (0));

其他回答

API 23提供了以下方法:

navigationView.setCheckedItem(R.id.nav_item_id);

但是,由于某种原因,这个函数没有导致导航项后面的代码运行。该方法当然会突出显示导航抽屉中的项目,或“检查”它,但它似乎不会调用OnNavigationItemSelectedListener,导致启动时出现空白屏幕,如果您的启动屏幕依赖于导航抽屉的选择。可以手动调用监听器,但看起来很俗气:

if (savedInstanceState == null) this.onNavigationItemSelected(navigationView.getMenu().getItem(0));

上面的代码假设:

你已经实现了 NavigationView。OnNavigationItemSelectedListener在你的活动 您已经呼叫过: navigationView.setNavigationItemSelectedListener(这个); 您要选择的项目位于位置0

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

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

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

下面的代码用于选择第一个项目并在菜单中突出显示所选的第一个项目。

onNavigationItemSelected(mNavigationView.getMenu().getItem(0).setChecked(true));

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

当使用BottomNavigationView时,其他答案如navigationView.getMenu().getItem(0).setChecked(true);和 navigationView.setCheckedItem (id);不会工作调用setSelectedItemId工作:

    BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            // TODO: 10-Aug-19 your code here 
        }
    });

    bottomNavigationView.setSelectedItemId(R.id.myitem);