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

查看:171
本文介绍了如何使用D-pad导航在列表视图的行和后代之间切换(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.

在带有触摸屏的普通android设备上效果很好.

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.

当我使用D-pad导航列表视图时,只有整行可以成为焦点,而列表视图内部的imageview不能成为焦点.

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在listview行和listview内的项目之间移动焦点,同时保持listview和imageview可点击吗?

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

非常感谢!

推荐答案

我使用D-pad进行Listview工作,可以在列表项内切换焦点. 这就是我解决的方法. 首先,让您的列表视图可集中显示项目.

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方法中的onKeyDown()以控制D-pad的上,下,左,右键.

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将清除其焦点,并且listView再次获得焦点.

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);
}

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

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