在我的應用程序中,我使用了導航抽屜。在這裏我列出了所有的項目。如何在各個片段打開時隱藏導航抽屜項目
從形象,項目,
- 首頁
- 篩選&排序
- 願望清單
- 店
- MyOrder
- 設置
- 註銷
如果我在店鋪的片段,我需要隱藏它。這個怎麼做?
請幫幫我。
在我的應用程序中,我使用了導航抽屜。在這裏我列出了所有的項目。如何在各個片段打開時隱藏導航抽屜項目
從形象,項目,
如果我在店鋪的片段,我需要隱藏它。這個怎麼做?
請幫幫我。
您可以處理它在片段onAttach
方法。根據您的需要設置特定項目的可見性。
@Override
public void onAttach(Context context) {
super.onAttach(context);
YourActivity activity = (YourActivity)context;
NavigationView navigationView = (NavigationView) activity.findViewById(R.id.yournavigationviewid);
navigationView.getMenu().findItem(R.id.youritemid).setVisible(false);
}
在您的公共onNavigationItemSelected(MenuItem項)如果您設置一個片段,然後自動抽屜將隱藏。我這樣做:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
toolbar.setTitle(item.toString());
int id = item.getItemId();
if (id == R.id.dashboard) {
fragment = new DashboardFragment();
} else if (id == R.id.manage_users) {
}else{
}
setFragmentLayout(fragment);
return true;
}
根據您的要求設置您的片段。
在你的setNavigationItemSelectedListener裏面你可以得到選擇的菜單項,你可以實現代碼。另外你需要存儲隱藏菜單項的情況下,以使其可見後
MenuItem prevMenuItem;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if(prevMenuItem != null) prevMenuItem.setVisible(true) //making visible the previously hidden item.
menuItem.setVisible(false);
prevMenuItem = menuItem //storing the instance of currently hidden item to make it visible later.
return true;
}
});
您可以隱藏使用mDrawerLayout.closeDrawers()
抽屜中onNavigationItemSelected
監聽器是這樣的:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.navigation_item_shop:
//do your stuffs or attach fragment
mDrawerLayout.closeDrawers();
return true;
default:
return true;
}
}
}
這一個爲我工作! – Suchith
這也適用於我! –