سؤال

I am looking to implement a navigation drawer selector which shows the currently selected item at all times when the drawer is opened and that is retained even when the drawer is closed. Something like this:

enter image description here

I have used something like this but it only shows the selector when I click on the list item. This is in my res folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/list_activated_holo" />

</selector>

How do i attain the above shown implementation?Which state do i need to implement? Thanks

هل كانت مفيدة؟

المحلول

Have resolved same issue by setting color in getView() of ArrayAdapter that populates my list in NavigationDrawer:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(getActivity()).inflate(
                    R.layout.side_menu_list_item,
                    parent,
                    false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mCurrentSelectedPosition == position) {
            holder.title.setBackgroundResource(R.color.menu_list_separator_line);
        } else {
            holder.title.setBackgroundResource(R.color.app_background_light);
        }
        holder.title.setText(menuItems[position]);

        return convertView;
    }

نصائح أخرى

There's another state, "android:state_selected", that you must use. Use a state drawable for the background of your list item, and use a different state drawable for listSelector of your list:

list_row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_background"
    >
...
</LinearLayout>

listitem_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/android:transparent" />
    <item android:drawable="@drawable/listitem_normal" />
</selector>

layout.xml that includes the ListView:

...
<ListView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listitem_selector"
   />
...

listitem_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/listitem_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top