Android TV如何在RecyclerView之间移动焦点

7
我正在构建一个Android TV应用程序,其布局如下所示: enter image description here 左边和右边的两个列表都是垂直LinearLayoutManagers的RecyclerViews,标题视图是静态的。使用D-PAD在一个列表内进行导航没有问题,但是从一个列表切换到另一个列表时会出现问题。例如,焦点从列表1的第5项移动到列表2的第5项。当列表2很短且少于5个项目时,它就会丢失焦点。
我希望保存最后聚焦的项目索引,这样当用户在列表1-列表2-列表1之间导航时,具有该索引的项目将再次获得焦点,并且还要防止视图失去焦点。是否有任何好的解决方案?
需要的行为: 用户导航到列表顶部并按“UP”键-焦点保持原样,不发生任何变化。 用户导航到列表底部并按“DOWN”键-焦点保持原样,不发生任何变化。 用户从列表1导航到列表2-以前在列表2中拥有焦点的项目获得焦点,如果之前没有任何项目聚焦,则项目0获得焦点。
主要布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/iv_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="false"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="27dp"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="48dp"
        android:layout_marginTop="27dp"
        android:orientation="horizontal"
        >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:orientation="horizontal"
                >

                <ImageView
                    android:id="@+id/iv_poster"
                    android:layout_width="@dimen/episodes_list_item_height"
                    android:layout_height="@dimen/episodes_list_image_width"
                    />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >

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

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        >

                        <TextView
                            android:id="@+id/tv_release_date"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left"
                            />

                        <TextView
                            android:id="@+id/tv_rating"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:drawableLeft="@drawable/ic_star_white_24dp"
                            android:drawablePadding="@dimen/view_padding_small"
                            />

                    </FrameLayout>

                    <TextView
                        android:id="@+id/tv_description"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        />
                </LinearLayout>

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_seasons_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:focusable="false"
                />

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_episodes_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:focusable="false"
            />

    </LinearLayout>

</FrameLayout>

用于列表的项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/seasons_list_item_height"
    android:orientation="horizontal"
    android:focusable="true"
    >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/tv_episodes_count"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:ems="10"
        />

</LinearLayout>

所以您想在您的两个 Recycler View 之间移动焦点,能否稍微解释一下? - Jay Rathod
我想要记住RecyclerView中最后一个聚焦的位置。当用户从列表1导航到列表2,然后返回列表1时,我希望恢复先前聚焦的列表1项目。对于列表2也是如此。此外,当列表1中的item1被聚焦时,当用户按下D-pad上的“向上”按钮时,我希望焦点不会移动,以此类推。 - Євген Гарастович
jaydroider,选择和焦点是不同的东西。我可以获取最后一个聚焦的位置,但找不到恢复它的方法。 - Євген Гарастович
你的意思是当你使用遥控器导航时,你遇到了这个问题吗? - Jay Rathod
我正在使用标准的Nexus Player遥控器(一个D-pad)。 - Євген Гарастович
显示剩余5条评论
3个回答

4
通过查阅Google的Leanback库源代码解决了我的问题。如果您遇到同样的困难,只需用这个包装您的RecyclerView,并不要忘记设置。
private boolean mPersistFocusVertical = false;

如果你想在水平搜索时保持焦点,就像在我的布局中一样。


你是如何使用这个私有类的?你能否给出一个解决方案的示例代码,你是指将其包装在xml中吗? - adrianrdzv
只需复制代码。它没有私有访问的依赖项。它对我有效。 - Євген Гарастович
链接无法使用(404)。请提供正确的链接,好吗? - BArtWell
1
这是来自Google的v17-leanback库中的PersistentFocusWrapper类。您可以搜索最新版本。现在它可以在此处找到:https://android.googlesource.com/platform/frameworks/support/+/master/v17/leanback/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java - Євген Гарастович
@ЄвгенГарастович,你有没有遇到过它崩溃的情况?我在这里找到了解决方法:https://dev59.com/55vga4cB1Zd3GeqP0VQk - BArtWell

0

将您的主布局修改如下。

对于方向导航,我们可以添加属性,例如nextFocusDown、nextFocusLeft、nextFocusRight、nextFocusUp,以根据我们的要求聚焦相关项目。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
    android:id="@+id/iv_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="27dp"
    android:layout_marginLeft="48dp"
    android:layout_marginRight="48dp"
    android:layout_marginTop="27dp"
    android:orientation="horizontal"
    >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="horizontal"
            >

            <ImageView
                android:id="@+id/iv_poster"
                android:layout_width="@dimen/episodes_list_item_height"
                android:layout_height="@dimen/episodes_list_image_width"
                />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                >

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

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:id="@+id/tv_release_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        />

                    <TextView
                        android:id="@+id/tv_rating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:drawableLeft="@drawable/ic_star_white_24dp"
                        android:drawablePadding="@dimen/view_padding_small"
                        />

                </FrameLayout>

                <TextView
                    android:id="@+id/tv_description"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_seasons_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:focusable="false"
            android:nextFocusDown="@+id/rv_episodes_list"
            />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_episodes_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:focusable="false"
        android:nextFocusUp="@+id/rv_seasons_list"
        />

</LinearLayout>

希望这能解决你的问题!!


谢谢您的建议,但这并没有帮助。我需要焦点在到达列表顶部/底部时保持不变。它并没有影响左/右移动焦点。 - Євген Гарастович
请把你的应用程序截图和相关行放在屏幕上。 - Jay Rathod
我已经使用了你的代码在这里创建了一个小测试项目:https://jgarin@bitbucket.org/jgarin/tv-test.git 当你按住D-pad的上/下键时,焦点会从一个列表移动到另一个列表,并且之前的焦点位置不会被保存/恢复。 - Євген Гарастович
尝试获取列表1的最后焦点位置,并存储在共享首选项中。 - Jay Rathod
获取和存储不是问题,我可以处理。但恢复焦点是个问题。有什么想法吗? - Євген Гарастович
可能是与 https://dev59.com/X14d5IYBdhLWcg3wFPM7 相关的重复问题,请查看。 - Jay Rathod

0
为了让我的列表记住焦点位置,我使用了Leanback库中的VerticalGridView而不是RecyclerView,效果非常完美。

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好的回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接