Navigation Drawer: Add Titles to Groups, Not Items

后端 未结 4 2045
梦如初夏
梦如初夏 2021-02-04 01:47

I have a standard Navigation Drawer, pre-created by Android Studio and want to populate it with number of groups. I started with this:



        
4条回答
  •  悲&欢浪女
    2021-02-04 02:08

    add xml class navigation_drawer_title:

    
    
    

    and change your navigationAdapter like this

    private static final int TYPE_HEADER = 0; 
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_SEPARATOR = 2;
        private static final int TYPE_TITLE = 3;
        @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
            {
                View v = null;
                switch (viewType)
                {
                    case TYPE_HEADER:
                        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_header, parent, false); //Inflating the layout
                        break;
                    case TYPE_ITEM:
                        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_item, parent, false); //Inflating the layout
                        break;
                    case TYPE_SEPARATOR:
                        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_separator, parent, false); //Inflating the layout
                        break;
    
                    case TYPE_TITLE:
                        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_drawer_title, parent, false); //Inflating the layout
                        break;
                }
                return new ViewHolder(v, viewType); // Returning the created object
    
            }
    

    and

    @Override
        public int getItemViewType(int position)
        {
            if (position == 0)
                return TYPE_HEADER;
            if (navMenuItems.get(position - 1).getItemType() == NavItemType.Group)
                return TYPE_SEPARATOR;
            if (navMenuItems.get(position - 2).getItemType() == NavItemType.Group)
                return TYPE_TITLE
    
            return TYPE_ITEM;
        }
    

提交回复
热议问题