5

I am using a ListView with custom adapter it simply has one TextView

<TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/list_item_content"
   android:background="@drawable/list_item1"
   android:layout_width="fill_parent" android:layout_height=""wrap_content"

 />

Heres my selector background:

<selector 
 xmlns:android="http://schemas.android.com/apk/res/android">    

<item android:state_focused="true" android:state_enabled="false"
    android:state_pressed="true"
    android:drawable="@drawable/draw_list_item1" />
<item android:state_focused="true" android:state_enabled="false"
    android:drawable="@drawable/draw_list_item_focused" />

<item android:state_focused="true" android:state_pressed="true"
    android:drawable="@drawable/draw_list_item_selected" />
<item android:state_focused="false" android:state_pressed="true"
    android:drawable="@drawable/draw_list_item_selected" />

<item android:state_focused="true"
    android:drawable="@drawable/draw_list_item_focused" />

<item android:drawable="@drawable/draw_list_item1"/>

</selector>

The problem is when I am setting TextView properties: android:clickable="true" and android:focusable="true"

It is then I see my focused version of background, but setting these causes list items to no more respond to clicks and long clicks. When these 2 properties are removed all ListItems respond to clicks and long clicks.

What should be done which would make focusable background visible and click responds both to work.

I have tried calling getListView().setItemsCanFocus(true) but problems still persists.

Shardul
  • 786
  • 3
  • 11
  • 20

1 Answers1

3

along with android:focusable, use this on TextView

android:duplicateParentState="true"

Make sue your list items are focusable and clickable.

Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
  • Nope not working. All the items are in focused state now. ListView is the only view in my layout i.e. width and height set to fill_parent – Shardul Dec 31 '10 at 09:52
  • @Shardul: You mean you have multiple list items selected at once? – Sarwar Erfan Dec 31 '10 at 09:54
  • No when acitivity starts all items have background which resembles focused background specified in my selector. And still neither item is responding to clicks. – Shardul Dec 31 '10 at 09:58
  • Aren't you putting your TextView in a LinearLayout or RelativeLayout??? Seems like so. If you directly use the TextView as item template, all items gets focused state (using duplicateParentState) and, the second last item in your statelist is the first match and all is shown as selected. From SDK documentation "During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state." – Sarwar Erfan Dec 31 '10 at 12:58