带有 ImageButton 的 Android 自定义 ListView 没有获得焦点 [英] Android custom ListView with ImageButton is not getting focus

查看:17
本文介绍了带有 ImageButton 的 Android 自定义 ListView 没有获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android TV 开发一个应用.我有一个 listviewImageButton + Textviews 作为孩子.由于电视不接受触摸事件,而是使用 D-PAD 或轨迹球遥控器,我必须将注意力集中在所有可点击的项目上,以便用户知道焦点在哪里.但问题是如果我添加

I am developing an app for android TV. I have a listview with an ImageButton + Textviews as children. As TV does not take touch events but uses D-PAD or trackball remote 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

android:descendantFocusability="blocksDescendants"

在自定义列表行布局中,但列表行中的 ImageButton 将不会获得焦点.如果我删除上面的代码,则 ImageButton 获得焦点,但我无法单击列表行.

in the custom list row layout but ImageButton in the list row will not get focus. And if I remove above code then ImageButton gets focus but I can't click list row.

我还添加了 setItemsCanFocus对于我的 ListView 但没有运气,没有任何改变.我阅读了很多文件,但我找不到这个问题的正确答案.我想同时关注列表行和 ImageButton.有没有可能,如果有人知道请帮我...

Also I added setItemsCanFocus for my ListView but no luck, nothing changed. I read many documents but i could not find a proper answer for this problem. I want focus on both List row as well as ImageButton. Is it possible, if any one knows please help me...

推荐答案

我必须将注意力集中在所有可点击的项目上,以便用户知道焦点在哪里.但问题是如果我添加

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

视图由于行文件中有多个视图而挂断,要获得点击您必须在行 xml 的主布局中再添加一个属性.

View getting hang up due to multiple views inside row file, to get click You have to add one more property to your main layout of row xml.

android:descendantFocusability="blocksDescendants

但是当我写上面的属性时,我无法聚焦在我的行文件中的 ImageButton.

But when I write above property then I'm not able to focus ImageButton which is in my row file.

我只是尝试了一种方法来实现对 ImageButtonlistview 的关注.

I just tried one way to acheive to get foucus on ImageButton as well as on listview.

要专注于 ImageButton:

  1. 创建一个选择器文件.
  2. 应用于图像按钮.
  3. android:focusableInTouchMode="true" 添加到 ImageButton.
  1. Create one selector file.
  2. Applied on Imagebutton.
  3. Added android:focusableInTouchMode="true" to ImageButton.

要专注于列表视图:

  1. android:descendantFocusability="blocksDescendants 添加到行 xml 中的主布局.
  1. Add android:descendantFocusability="blocksDescendants to main-layout within row xml.

现在当你点击你的 ImageButton 时,它会以你想要的颜色为焦点,即使它也会专注于列表视图点击.

Now when you click on your ImageButton it will focus with your desire color even it will focus on listview click too.

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 模拟器中测试,运行良好.

Tested in S3 emulator, works well.

这篇关于带有 ImageButton 的 Android 自定义 ListView 没有获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆