我正在为安卓电视开发一个应用程序。我有一个 listview
和一个 ImageButton
+ Textview
s 作为 child 。由于电视不接受触摸事件,而是使用 D-PAD 或轨迹球 Remote ,我必须将注意力集中在所有可点击的项目上,以便用户知道焦点在哪里。但问题是如果我添加,我可以专注于列表行
android:descendantFocusability="blocksDescendants"
在自定义列表行布局中,但列表行中的 ImageButton
不会获得焦点。
如果我删除上面的代码,那么 ImageButton
会获得焦点,但我无法单击列表行。
我还添加了 setItemsCanFocus
对于我的 ListView
但没有运气,没有任何改变。
我阅读了很多文件,但我找不到这个问题的正确答案。我想同时关注列表行和 ImageButton
。有没有可能,有知道的请帮帮我...
最佳答案
I have to get focus on all clickable items so that user knows where the focus is. But the problem is I can get focus on list row if I add
由于行文件中的多个 View 而导致 View 挂起,要获得点击您必须在行 xml 的主布局中再添加一个属性。
android:descendantFocusability="blocksDescendants
But when I write above property then I'm not able to focus ImageButton which is in my row file.
我只是尝试了一种方法来获得对 ImageButton
以及 listview
的关注。
要关注 ImageButton:
- 创建一个选择器文件。
- 应用于图像按钮。
- 将
android:focusableInTouchMode="true"
添加到 ImageButton。
专注于 Listview:
- 将
android:descendantFocusability="blocksDescendants
添加到行 xml 中的主布局中。
现在,当您单击 ImageButton 时,它会聚焦于您想要的颜色,即使它也会聚焦于 listview 点击。
Selecotor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@android:color/transparent"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@android:color/darker_gray"
/> <!-- focused -->
<item
android:drawable="@android:drawable/btn_star"
/> <!-- default -->
</selector>
在 S3 模拟器中测试,运行良好。
关于带有 ImageButton 的 Android 自定义 ListView 没有获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15249632/