如何在列表视图的行和它的后代之间使用方向键导航切换(Google-TV 支持) [英] How to use D-pad navigate switch between listview's row and it's decendants (Google-TV support)

查看:16
本文介绍了如何在列表视图的行和它的后代之间使用方向键导航切换(Google-TV 支持)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,每个列表项内都有一个图像视图,当用户单击该图像视图时,它会弹出一个菜单.

I have a listview which has an imageview inside each list item, when user click on that imageview, it will pop-up a menu.

它在带有触摸屏的普通安卓设备上运行良好.

It works very well on a normal android device with a touch screen.

但现在我想支持google-tv,哪个应用应该由D-pad控制.

But now I want to support google-tv, which app should be control by D-pad.

当我使用方向键浏览列表视图时,只有整行可以成为焦点,我无法让列表视图中的图像视图成为焦点.

When I use D-pad to navigate through listview, only the entire row can be focus, I can't get the imageview inside listview be focus.

我尝试将 android:focusable="true" 添加到 imageview,但它导致整个 listview 无法接收 onItemClick 事件.

I try to add android:focusable="true" to imageview, but it cause the entire listview can't receive onItemClick event.

有谁知道如何使用 d-pad 在列表视图行和列表视图内的项目之间移动焦点也保持列表视图和图像视图可点击?

Does anyone know how to move the focus between listview rows and item inside listview using d-pad also keeps listview and imageview clickable?

非常感谢!

推荐答案

我的 Listview 与 D-pad 一起工作,它可以在列表项内切换焦点.这就是我解决它的方法.首先,让您的列表视图可以聚焦项目.

I got my Listview work with D-pad which can switch focus inside a list item. This is how I solve it. First, let your list view is item focusable.

注意:如果您稍后尝试在代码中将 ItemsCanFocus 设置为 false,您会发现即使您再次设置为 true,您的列表项也无法再获得焦点,所以不要这样做.

NOTE: If you trying to set ItemsCanFocus to false later in your code, your will find your list item can't get focus anymore even if your set back to true again, so don't do that.

mDpadListView.setItemsCanFocus(true);

然后您需要一个字段来跟踪当前选择了哪个列表项.这里我把ViewHolder放在Adapter中listItem的标签中.

Then you need a field to keep tracking which list item is current selected. Here I put the ViewHolder in the listItem's tag in Adapter.

mDpadListView.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        if (view.getTag() != null) {
            DpadListAdapter.ViewHolder holder = (ViewHolder) view.getTag();
            if (holder.shortCut != null && holder.shortCut.isShown()) {
                currentSelectView = view;
            } else {
                currentSelectView = null;
            }
        } else {
            currentSelectView = null;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

第三,覆盖Activity Method中的onKeyDown()来控制方向键的上下左右键.

Third, override onKeyDown() in Activity Method to control up, down, left, right key for D-pad.

当用户在 D-pad 上按下右键时,我让 listview 到 clearFoucs() 并让里面的 ImageView 获得焦点.

When user press right button on D-pad, I let the listview to clearFoucs() and let the ImageView inside to obtain focus.

当用户向上、向下或向左按下时,列表项中的 ImageView 将清除其焦点,并且列表视图再次获得焦点.

When user press up, down or left, the ImageView in list item will clear it's focus and the listView obtain focus again.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (currentSelectView != null) {
            DpadListAdapter.ViewHolder holder = 
                (ViewHolder)      currentSelectView.getTag();
            mDpadListView.clearFocus();
            holder.shortCut.setFocusable(true);
            holder.shortCut.requestFocus();
            return true;
        }
        break;
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_DPAD_DOWN:
        if (currentSelectView != null) {
            DpadListAdapter.ViewHolder holder = 
                    (ViewHolder) currentSelectView.getTag();
            if (holder.shortCut.hasFocus()) {
                holder.shortCut.clearFocus();
                holder.shortCut.setFocusable(false);
                mDpadView.requestFocus();
                return true;
            }
        }
        break;
    default:
        break;
    }
    return super.onKeyDown(keyCode, event);
}

这篇关于如何在列表视图的行和它的后代之间使用方向键导航切换(Google-TV 支持)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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