我在主屏幕中使用导航抽屉并使用第三方库 com.mikepenz:materialdrawer:5.1.6@aar 来构建我的Material Drawer。我的要求是我想根据条件隐藏一些菜单项。有没有办法实现它?这就是我创建抽屉的方式。
private Drawer result = null;
AccountHeader headerResult;
final PrimaryDrawerItem home = new PrimaryDrawerItem().withName("Home").withIdentifier(1).withIcon(GoogleMaterial.Icon.gmd_home);
final PrimaryDrawerItem profile = new PrimaryDrawerItem().withName("Profile").withIdentifier(2).withIcon(GoogleMaterial.Icon.gmd_account);
final PrimaryDrawerItem gallery = new PrimaryDrawerItem().withName("Gallery").withIdentifier(3).withIcon(R.drawable.ic_perm_media_black_24dp);
final PrimaryDrawerItem recognition = new PrimaryDrawerItem().withName("Recognition").withIdentifier(4).withIcon(GoogleMaterial.Icon.gmd_face);
final PrimaryDrawerItem maps = new PrimaryDrawerItem().withName("Maps").withIdentifier(5).withIcon(R.drawable.ic_place_black_24dp);
final PrimaryDrawerItem tagAndLocate = new PrimaryDrawerItem().withName("Tag & Locate").withIdentifier(6).withIcon(R.drawable.ic_remove_red_eye_black_24dp);
final PrimaryDrawerItem gamesAndPuzzle = new PrimaryDrawerItem().withName("Games & Puzzles").withIdentifier(7).withIcon(R.drawable.ic_casino_black_24dp);
final PrimaryDrawerItem backup = new PrimaryDrawerItem().withName("Backup").withIdentifier(8).withIcon(GoogleMaterial.Icon.gmd_save);
final PrimaryDrawerItem logout = new PrimaryDrawerItem().withName("Logout").withIdentifier(9).withIcon(FontAwesome.Icon.faw_sign_out);
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.withSelectionListEnabledForSingleProfile(false)
.addProfiles(userProfile)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.withDisplayBelowStatusBar(false)
.withTranslucentStatusBar(true)
.withSavedInstance(savedInstanceState)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(home)
.addDrawerItems(profile)
.addDrawerItems(gallery)
.addDrawerItems(recognition)
.addDrawerItems(maps)
.addDrawerItems(tagAndLocate)
.addDrawerItems(gamesAndPuzzle)
.addDrawerItems(backup)
.addDrawerItems(new DividerDrawerItem())
.addDrawerItems(logout)
.buildForFragment();
resultStandard.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
int drawItemId = (int) drawerItem.getIdentifier();
Intent intent;
Fragment fragment;
switch (drawItemId) {
case 1:
fragment = new HomeFragment();
gaFragmentStack.add(home);
break;
case 2:
fragment = new ProfileFragment();
gaFragmentStack.add(profile);
break;
case 3:
fragment = new GalleryFragment();
gaFragmentStack.add(gallery);
break;
case 4:
fragment = new RecognitionFragment();
gaFragmentStack.add(recognition);
break;
case 5:
fragment = new MapsFragment();
gaFragmentStack.add(maps);
break;
case 6:
fragment = new TagLocateFragment();
gaFragmentStack.add(tagAndLocate);
break;
case 7:
fragment = new GamesPuzzlesFragment();
gaFragmentStack.add(gamesAndPuzzle);
break;
case 8:
fragment = new BackupFragment();
gaFragmentStack.add(backup);
break;
default:
fragment = new HomeFragment();
break;
}
if (drawItemId == 9) {
FirebaseAuth.getInstance().signOut();
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
intent = new Intent(MainMenuActivity.this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_gaFragments, fragment);
transaction.commit();
return false;
}
});
我只想根据条件隐藏一些抽屉物品。有没有办法做到这一点?任何帮助表示赞赏。
答案 0 :(得分:0)
无法从抽屉中隐藏特定项目。您可以通过删除所有项目(removeAllItems() 和 removeAllStickyFooterItems())并有选择地再次添加它们来解决此问题。下面是 Kotlin 的示例代码,您可以使用滑块内的 IF 语句处理此问题。应用 {...}
fun refreshMenuItems() {
slider.apply {
removeAllItems()
removeAllStickyFooterItems()
if (isLoggedUser == false) {
addItems(
PrimaryDrawerItem().apply {
nameText = "Login"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "Register"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
} else {
addItems(
PrimaryDrawerItem().apply {
nameText = "My profile"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "My reservations"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
addStickyDrawerItems(
PrimaryDrawerItem().apply {
nameText = "Log out"
isSelectable = false
identifier = 100
}
)
}
}
}
例如,您可以从 onResume() 方法中调用此方法
override fun onResume() {
super.onResume()
changeDrawerUserName()
refreshMenuItems()
}