Hide a Navigation Drawer Menu Item - Android

后端 未结 8 656
春和景丽
春和景丽 2021-01-30 06:31

I have a navigation drawer. When an event is called, I want to hide one of my navigation menu item for user. How can I do that?

protected void onCreate(Bundle sa         


        
相关标签:
8条回答
  • 2021-01-30 06:46
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    
    Menu menu =navigationView.getMenu();
    
    MenuItem target = menu.findItem(R.id.nav_target);
    
    target.setVisible(false);
    
    0 讨论(0)
  • 2021-01-30 06:47

    You can delete the second item of listview via following code:

    navDrawerItems.remove(2);
    ((BaseAdapter)adapter).notifyDataSetChanged();
    
    0 讨论(0)
  • 2021-01-30 06:48

    Hide/Show Navigation item

    Make changes in your NavDrawerListAdapter as follow,

    public class MyListAdapter extends BaseAdapter {
    
        Context context;
        ArrayList<ItemListModel> listMenuItem;
        ArrayList<ItemListModel> listMenuItem1;
        int hideViewIndex = -1;
    
        public MyListAdapter(Context context, ArrayList<ItemListModel> listMenuItem) {
            this.context = context;
            this.listMenuItem = listMenuItem;
            listMenuItem1=new ArrayList<ItemListModel>(listMenuItem);
        }
    
        @Override
        public int getCount() {
            return listMenuItem.size();
        }
    
        @Override
        public Object getItem(int position) {
            return listMenuItem.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        private class ViewHolder
        {
            private ImageView imgLogo;
            private TextView txtItemName;
        }
    
        public void hideView(int pos)
        {
            listMenuItem.remove(pos);
            notifyDataSetChanged();
        }
    
        public void showView(int pos)
        {
            listMenuItem.add(pos, listMenuItem1.get(pos));
            notifyDataSetChanged();
        }
    
        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder viewHolder = null;
            if(convertView == null)
            {
                LayoutInflater myLayoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = myLayoutInflater.inflate(R.layout.activity_item_list, null);
                viewHolder = new ViewHolder();
                viewHolder.imgLogo = (ImageView) convertView.findViewById(R.id.imgLogo);
                viewHolder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ItemListModel itemListModel = listMenuItem.get(position);
            viewHolder.imgLogo.setImageDrawable(itemListModel.getImageLogo());
            viewHolder.txtItemName.setText(itemListModel.getStrItemName());
    
            return convertView;
        }
    

    and call this method in activity as follow,

    adapter.hideView(2);
    //adapter.showView(2);
    
    0 讨论(0)
  • 2021-01-30 07:04

    This will work as setInvisibility(GONE):

     Menu nav_Menu = navigationView.getMenu();
     nav_Menu.findItem(R.id.nav_profile).setVisible(false);
     nav_Menu.findItem(R.id.menu_login).setVisible(true);
     nav_Menu.findItem(R.id.menu_logout).setVisible(false);
     nav_Menu.findItem(R.id.menu_regsitration).setVisible(true);
    
    0 讨论(0)
  • 2021-01-30 07:04

    NavigationView navigationView;

        mapIntentActivity = getIntent();
        navItemString = mapIntentActivity.getStringExtra("success");
    
        if(navItemString != null) {
    
            navigationView = (NavigationView) findViewById(R.id.nav_view);
            Menu nav_Menu = navigationView.getMenu();
            nav_Menu.findItem(R.id.nav_car).setVisible(false);
    
        }
    
    0 讨论(0)
  • 2021-01-30 07:05

    Kotlin version

     val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView
     val nav_Menu: Menu = navigationView.getMenu()
     nav_Menu.findItem(R.id.nav_stock).setVisible(false)
    
    0 讨论(0)
提交回复
热议问题