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