2

我以NavigationView编程方式添加了项目:

**HERE ADD ITEMS**    

Menu rightMenu = mRightDrawerView.getMenu();
for (DataParking dataParking : dataParkingList) {   
    MenuItem menuItem = rightMenu.add(dataParking.getTimeParking()); 
}

**HERE ADD CLICK LISTENER**

mRightDrawerView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // Select menu
            menuItem.setChecked(true);

            // Closing left_drawer on item click
            mDrawerLayout.closeDrawer(mRightDrawerView);

            return false;
        }
    });

导航抽屉中的项目可以正确点击,但选择不是持久的。如果我通过 XML 添加相同的项目一切正常。

4

2 回答 2

6

当您以编程方式添加项目时,不会设置项目的可检查标志。你应该只添加这一行:

menuItem.setCheckable(true);

将项目添加到菜单后。

于 2016-01-20T19:40:12.213 回答
0

As @Oncky answered, you can just setCheckable on your menuItem like this:

Menu rightMenu = mRightDrawerView.getMenu();
for (DataParking dataParking : dataParkingList) {   
    MenuItem menuItem = rightMenu.add(dataParking.getTimeParking()).setCheckable(true); 
}
于 2016-03-28T05:21:04.420 回答