Navigation Drawer item background colour for selected item

后端 未结 13 1555
独厮守ぢ
独厮守ぢ 2020-11-28 23:54

I used Android Studio to implement the Navigation Drawer and I can\'t get the blue colour that is used to show which section we\'re currently in to change.

I\'ve tri

13条回答
  •  被撕碎了的回忆
    2020-11-29 00:32

    here is how i have done and it is working, the brief concept is maintain the position of selected item in adapter and call notifyDataSetChanged on calling notifyDatasetChanged the getView method is called again and in get view check the position on the selected position change the background view. Here is the code :

    Adapter of NavigationDrawer List View

    public class MenuAdapter extends BaseAdapter {
    
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private Context mContext;
    private String name;
    private int profile;
    private int mIcons[];
    private int selectedPosition = 0;
    private String mNavTitles[];
    private LayoutInflater mInflater;
    
    public MenuAdapter(String titles[], int icon[], String Name, int profile) {
        mNavTitles = titles;
        mIcons = icon;
        name = Name;
        this.profile = profile;
    }
    
    public MenuAdapter(String Titles[], int Icons[], Context mContext) {
        mNavTitles = Titles;
        mIcons = Icons;
        this.mContext = mContext;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        return mNavTitles.length;
    }
    
    @Override
    public Object getItem(int position) {
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        convertView = mInflater.inflate(R.layout.item_row, parent, false);
    
        TextView textView = (TextView) convertView.findViewById(R.id.rowText);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.rowIcon);
        final LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.outerLayout);
    
        imageView.setImageResource(mIcons[position]);
        textView.setText(mNavTitles[position]);
    
        if (position == selectedPosition)
            layout.setBackgroundColor(mContext.getResources().getColor(R.color.app_bg));
        else  
          layout.setBackgroundColor(mContext.getResources().getColor(R.color.normal_bg));
    
        return convertView;
    }
    
    public void setSelectedPosition(int position) {
    
        this.selectedPosition = position;
    
    }
    
    
    }
    

    in your activity do this

     private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) { 
            mMenuAdapter.setSelectedPosition(position - 1);
            mMenuAdapter.notifyDataSetChanged();
        }
    }
    

    if anyone still face any difficulty, feel free to ask.

提交回复
热议问题