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

发布于 2024-10-09 16:46:08 字数 1477 浏览 7 评论 0原文

我正在使用带有自定义适配器的 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"

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

应该做什么才能使可聚焦的背景可见并且单击响应都可以工作。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-10-16 16:46:08

与 android:focusable 一起,在 TextView 上使用它,

android:duplicateParentState="true"

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

along with android:focusable, use this on TextView

android:duplicateParentState="true"

Make sue your list items are focusable and clickable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文