项目可聚焦时的 Android ListView,禁用点击

标签 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"

 />

这是我的选择器背景:

<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>

问题是当我设置 TextView 属性时: android:clickable="true"android:focusable="true"

然后我看到了我的焦点版本的背景,但设置这些会导致列表项不再响应点击和长按。删除这 2 个属性后,所有 ListItem 都会响应点击和长按。

应该怎么做才能使可聚焦背景可见并且点击响应都起作用。

我已经尝试调用 getListView().setItemsCanFocus(true) 但问题仍然存在。

最佳答案

连同 android:focusable,在 TextView 上使用它

android:duplicateParentState="true"

使您的列表项可聚焦且可点击。

关于项目可聚焦时的 Android ListView,禁用点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4569250/

相关文章:

CSS 包装器背景未出现

javascript - 在 jQuery 中应用 css 方法后,css 类/属性处于非事件状态

java - 无法删除旧的 javaCompile 操作,可能类名已更改

android - IabResult : Billing service unavailable on device.(响应:3:Billing Unavailable)

android - 按钮数组onclicklistener

android - 0dp 大小的可组合项甚至可以组合吗

c# - 为什么我无法将数据库值加载到 ListView 上?

Delphi - 使用 ListView 拖放

android - 当 ListView 中的 View 值更新时,适配器未更新

html - 有没有办法让子 div 的背景扩展到父 div 之外?