Navigation Drawer item background colour for selected item

后端 未结 13 1384
独厮守ぢ
独厮守ぢ 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:25

    I have implement drawer menu with custom adapter class. May be it will help someone Drawer List

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/menu_item_color"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:fadingEdge="none"
        />
    

    drawer_list_item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="@drawable/menu_selector"
    >
    
    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:src="@drawable/ic_menu_home" />
    
    <TextView
        android:id="@+id/lblName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:layout_toRightOf="@+id/imgIcon"
        android:minHeight="48dp"
        android:textColor="@color/menu_txt_color" />
    

    menu_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <!-- selected -->
    <item android:drawable="@color/menu_item_active_color" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@color/menu_item_active_color" android:state_pressed="true"/>
    <item android:drawable="@color/menu_item_active_color" android:state_activated="true"/>
    <item android:drawable="@color/menu_item_active_color" android:state_checked="true"/>
    <item android:drawable="@color/menu_item_active_color" android:state_selected="true"/>
    
    <item android:drawable="@color/menu_item_color" android:state_activated="false"/>
    

    Add this on item click listner of listview yourlistview.setItemChecked(position, true);

    0 讨论(0)
  • 2020-11-29 00:25

    I know its too late but I have solved this issue in my app.

    Pls dont think it is silly, just simply change the position of "state_pressed" to top.

    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/list_item_bg_selected" android:state_activated="true"/>
    
    0 讨论(0)
  • 2020-11-29 00:27

    To solve this problem:

    1- You don't need android:listSelector under your ListView.

    2- Open (or Create) styles.xml under (res/values).

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
    </style>
    

    3- Under res/drawable folder create drawer_list_selector.xml file

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/light_gray_color" />
        <item android:state_activated="true" android:drawable="@drawable/red_color" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    

    4- Under res/drawable create red_color.xml / light_gray_color.xml (or any other name) and add your desired Hex color:

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#C8FF0000"/>
    </shape>
    

    5- Open your project AndroidManifest.xml and add android:theme tag (if not exist)

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    

    Reference / Credit: Changing Navigation Drawer Selected Item Color from default blue

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 00:33
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:drawable="@color/list_item_back_pressed" android:state_pressed="true" />
        <item  android:state_activated="true"><color android:color="@color/primary_blue"/></item>
       <item android:drawable="@color/list_item_back_normal"/>
    
    </selector>
    
    0 讨论(0)
  • 2020-11-29 00:36

    In addition to providing a custom selector drawable for the listSelector, you should also set the background resource of the list item to a similar selector drawable that has different drawables for the different states.

    0 讨论(0)
提交回复
热议问题