Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid's selector for selected item does not work
    primarykey
    data
    text
    <p>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 <code>getView</code> method, including <code>itemView.setSelected(true)</code>.</p> <p>The main layout defines the listview in the following way:</p> <pre><code>&lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="multipleChoice" android:listSelector="@drawable/list_selector" /&gt; </code></pre> <p>(Playing with choice mode does not help either).</p> <p>The <strong>list_selector</strong> is an almost empty stub:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:drawable="@android:color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>I don't need specific styles for listview as a whole, so I'd leave a default one, but according to <a href="https://stackoverflow.com/a/10501314/1102014">this answer</a>, we need a selector for a listview for item selector to work. Anyway, without the <strong>list_selector</strong> the problem remains.</p> <p>The listview item layout:</p> <pre><code>&lt;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"&gt; </code></pre> <p>and it references the following <strong>listitem_background</strong> selector:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_selected="true" android:drawable="@android:color/white" /&gt; &lt;item android:drawable="@android:color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>The problem is, that selected items do not have white background.</p> <p>If I change <code>android:state_selected="true"</code> selector in the <strong>listitem_background</strong> to, for example, <code>android:state_pressed="true"</code>, then the selector starts to work, that is item background becomes white if an item is touched.</p> <p>So, I suppose, there is an error either in the selectors, or in the way how I select items.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload