Android's selector for selected item does not

2019-03-31 22:55发布

问题:

I have a listview, where I want to highlight selected items in a custom way. I'm setting every item properties in the adapter's getView method, including itemView.setSelected(true).

The main layout defines the listview in the following way:

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:listSelector="@drawable/list_selector" />

(Playing with choice mode does not help either).

The list_selector is an almost empty stub:

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

I don't need specific styles for listview as a whole, so I'd leave a default one, but according to this answer, we need a selector for a listview for item selector to work. Anyway, without the list_selector the problem remains.

The listview item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_background"
    android:orientation="vertical">

and it references the following listitem_background selector:

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

The problem is, that selected items do not have white background.

If I change android:state_selected="true" selector in the listitem_background to, for example, android:state_pressed="true", then the selector starts to work, that is item background becomes white if an item is touched.

So, I suppose, there is an error either in the selectors, or in the way how I select items.

I can write a workaround by setting background from Java or utilizing checkable states, but I want to understand and fix current problem with selectors. Thanks in advance.

回答1:

What about adding this line in your item view?

android:background="?android:attr/activatedBackgroundIndicator"

For instance:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewListRowOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" />
</LinearLayout>

Then the selector looks something like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">

<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selected" android:state_activated="true"/>
<item android:drawable="@drawable/list_default"/>

</selector>

And the list view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listViewMainFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:listSelector="@drawable/list_item_selector"
    android:choiceMode="singleChoice">
</ListView>

</LinearLayout>


回答2:

Try this one to change the selected list item.Put it inside the list view.

if (currentSelectedView != null && currentSelectedView != v) {
                unhighlightCurrentRow(currentSelectedView);
            }

            currentSelectedView = v;
            highlightCurrentRow(currentSelectedView);



private void unhighlightCurrentRow(View rowView) {
        rowView.setBackgroundColor(Color.TRANSPARENT);//to set a color for un-seclected row
    }

    private void highlightCurrentRow(View rowView) {
        rowView.setBackgroundResource(R.drawable.selector_img);//to set a color for seclected row
    }