VerticalGridView 的焦点处理

Shi*_*hiv 3 gridview focus android-tv leanback

这里 VerticalGridView(VG) 将填充一些数据。VG 通过向上/向下按 D-PAD 键获得焦点,工作正常。但问题是,当焦点位于网格中的第 0 项并且用户按下 D-PAD 键时,焦点必须设置为 VG 外部的视图之一,即 ll_exit(xml 文件中提到的 id),但当前焦点不是去上述的观点。请让我知道如何解决这个问题。

我尝试设置 android:nextFocusUp="@id/ll_exit" 但它没有移动并尝试在活动级别实现 onKeyUp 方法,如下所示

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
                Log.d(TAG, "onKeyUp: " + event.getAction());
                GenresFragment fragment = (GenresFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_genre);
                int pos = fragment.getVerticalGridView().getSelectedPosition();
                if (pos == 0) {
                    fragment.getLlExit().requestFocus();
                }
                return true;
            default:
                return super.onKeyUp(keyCode, event);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但这里的问题是,当我位于 VG 中的第一个项目并突然按下向上键按钮时,它会转到我需要聚焦的视图。这是xml文件中的ll_exit视图

<b>main_layout.xml</b>

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_toolbar"
            android:layout_width="@dimen/select_genre_fragment_width"
            android:layout_height="@dimen/select_genre_toolbar_height"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_all_channels"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <LinearLayout
                android:id="@+id/ll_genres"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <LinearLayout
                android:id="@+id/ll_exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <android.support.v17.leanback.widget.VerticalGridView
            android:id="@+id/vg_genres"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ll_toolbar"
            android:nextFocusUp="@id/ll_exit" />
    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

小智 7

尝试将以下 XML 属性添加到 VG

app:focusOutEnd="true"
app:focusOutFront="true"
Run Code Online (Sandbox Code Playgroud)

这个对我有用 !