Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have a focusable element in the list item contents (or the views for each grid item), the selector isn't drawn</p> <p>For example, if the template for your list items is:</p> <pre><code>&lt;LinearLayout android:orientation="horizontal" ... &gt; &lt;CheckBox android:id="@+id/checkBox" ... /&gt; &lt;TextView android:id+"@+id/textView" ... /&gt; &lt;/LinearLayout&gt; </code></pre> <p>then ListView will not draw a selector for each ListView item, because the CheckBox is, by default, focusable. </p> <p>You can either provide a background on each item that changes with selection state, or disable focus on all focusable elements (which in turn requires you to write a fairy fancy adapter to check the checkboxes when selection state changes. </p> <p>eg: </p> <pre><code>&lt;CheckBox android:id="@+id/checkBox" android:focusable="false" ... /&gt; </code></pre> <p>will cause an enclosing ListView to start drawing the selector again.</p> <p>Example of a stateful drawable: </p> <p>drawable/my_list_selector_bg.xml:</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/list_background_pressed" android:state_pressed="true" /&gt; &lt;item android:drawable="@drawable/list_background_focused" android:state_focused="true" /&gt; &lt;item android:drawable="@android:color/transparent" /&gt; &lt;/selector&gt; </code></pre> <p>You then apply that to the background of each view returned by your adapter:</p> <pre><code>&lt;LinearLayout android:orientation="horizontal" android:background="@drawable/my_list_selector_bg" ... &gt; </code></pre> <p>Either approach will work.</p> <p>The list adpater classes (GridView, ListView, &amp;c) call hasFocusable() on each view returned by the adapter, and disable selection drawing if hasFocusable() returns true. Fortunately, they also replicate the selection/focus/pressed/active state to the currently focused or selected adapter item, so you can draw it yourself if you want.</p>
 

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