效果演示
演示
NavigationView的布局文件
下面才是关键
1、app:headerLayout="@layout/layout_navigation_header"
这条代码的作用就是设置导航抽屉的样子
2、app:menu="@menu/navigation_main"
这条代码的作用是设置导航抽屉中每个Item
之后就是在Activity中具体引用了
private void initNavigationView() {
mNavigationView.setNavigationItemSelectedListener(this);
View headerView = mNavigationView.getHeaderView(0);
CircleImageView mUserAvatarView = (CircleImageView) headerView.findViewById(R.id.user_avatar_view);
TextView mUserName = (TextView) headerView.findViewById(R.id.user_name);
TextView mUserLevel = (TextView) headerView.findViewById(R.id.user_level);
String name = getSharedPreferences("user", MODE_PRIVATE).getString("username", "听雨喧");
userLevel = DbUtil.getUserLevel(this, name);
mUserLevel.setText("LV"+ userLevel);
//设置名字
mUserName.setText(name);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.item_exit:
//切换帐号
getSharedPreferences("user", MODE_PRIVATE).edit().putString("username","").commit();
startActivity(new Intent(this,LoginActivity.class));
finish();
break;
//主页
case R.id.item_home:
toggleDrawer();
changeFragmentIndex(item, 0);
boolean update = getSharedPreferences("book", MODE_PRIVATE).getBoolean("update", false);
//System.out.println("-0-----------------dianjile--item_home:"+update);
if(update){
homeFragment.reFreshData();
}
break;
//管理界面
case R.id.item_manager:
toggleDrawer();
if(userLevel<2){
ToastUtil.showLong(this,"抱歉,只有管理员才能进入");
}else {
changeFragmentIndex(item, 1);
}
break;
//收藏图书界面
case R.id.item_favourite:
toggleDrawer();
changeFragmentIndex(item, 2);
break;
//设置界面
case R.id.item_settings:
toggleDrawer();
changeFragmentIndex(item,3);
break;
case R.id.item_history:
toggleDrawer();
changeFragmentIndex(item, 4);
break;
case R.id.item_serach:
toggleDrawer();
changeFragmentIndex(item,5);
break;
}
return false;
}
作者:spark无与伦比