带有 ImageButton 的 Android 自定义 ListView 没有获得焦点

标签 android android-listview focus imagebutton

我正在为安卓电视开发一个应用程序。我有一个 listview 和一个 ImageButton + Textviews 作为 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:

  1. 创建一个选择器文件。
  2. 应用于图像按钮。
  3. android:focusableInTouchMode="true" 添加到 ImageButton。

专注于 Listview:

  1. 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/

相关文章:

ios - 有没有办法以编程方式将 iOS 设备的相机焦点设置为无限远?

android - 当一个 Activity 直接从一个通知启动时,你如何构建一个 Android 后台堆栈?

android - 褪色边缘仅在顶部和左侧起作用

java - Android ListView 不显示内容

android - 关于将信息从 ListView 保存到文件的建议

c# - 如何从另一个窗体将焦点设置到主窗体中的对象(文本框)(C#)

android - 在应用程序外显示 android 对话框

Android - 看起来像 'settings' 的 ListView

android - 如何清除堆栈中的所有 Activity ?

android - ListView 内的可聚焦 EditText