Sha*_*dul 5 android listview background click focusable
我使用ListView与自定义适配器它只有一个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"
/>
Run Code Online (Sandbox Code Playgroud)
继承我的选择背景:
<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>
Run Code Online (Sandbox Code Playgroud)
问题是当我设置TextView属性时:
android:clickable="true"和android:focusable="true"
然后,我会看到我的焦点版本的背景,但设置这些会导致列表项不再响应点击和长按.删除这两个属性后,所有ListItem都会响应点击和长按.
应该做些什么才能使可聚焦背景可见并且点击响应两者都起作用.
我试过打电话getListView().setItemsCanFocus(true)但问题仍然存在.
与 android:focusable 一起,在 TextView 上使用它
android:duplicateParentState="true"
Run Code Online (Sandbox Code Playgroud)
确保您的列表项可聚焦且可点击。