问题描述
我使用以下布局构建Android TV应用程序:
左边和右边是带有垂直LinearLayoutManagers的RecyclerView,Header视图是静态的。使用D-PAD进行导航可以在一个列表中正常工作,但是从一个列表切换到另一个列表时会出现问题。焦点从列表1的项目5移动到列表2的项目5.当列表2很短时,有少于5个项目,它只是失去了焦点。
我想要最后聚焦项目索引保存,当用户导航list1-list2-list1时,使用该索引的项目再次获得焦点,并防止视图失去焦点。有没有什么好的解决方案?
所需的行为:
用户导航到列表的顶部,并按下向上 - 焦点停留在原来的位置,什么都没发生。
用户导航到列表的底部并按下下 - 焦点停留在原来的位置,没有任何反应。
用户从列表1导航到列表2 - 以前有焦点在列表2中的项目获得焦点或item0获得焦点,如果以前没有焦点的话。
主布局:
<?xml version =1.0encoding =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
>
android:id =@ + id / iv_background
android:layout_width =match_parent
android:layout_height =match_parent
android:focusable =false
/>
$ b $< 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
>
$ b $< 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
>
android:id =@ + id / iv_poster
android:layout_width =@ dimen / episodes_list_item_height
android:layout_height =@ dimen / episodes_list_image_width
/>
$ b $< 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 $ b $ android android:layout_height =wrap_content $ b 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.0encoding =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>
通过Google Leanback图书馆来源挖掘回答了我的付款人。
如果您面临同样的麻烦,只需将您的RecyclerViews与,不要忘记设置
pre $ private $ boolean mPersistFocusVertical = false;
如果您希望焦点在水平搜索时保持不变,就像在我的布局中一样。
I am building an Android TV app with the following layout:
Both lists on the left and on the right are RecyclerViews with vertical LinearLayoutManagers, Header view is static. Navigation with D-PAD works fine within one list, but when switching from one list to another there are issues. Focus moves from, say list1's item 5 to list2' item 5. When list 2 is short has less than 5 items, it just loses focus.
I want the last focused item index saved and when the user navigates list1-list2-list1 the item with that index to gain focus again and also prevent the views from losing focus. Is there any good solution to this?
Required behaviour:user navigates to the top of the list and presses "UP" - focus stays where it was, nothing happens.user navigates to the bottom of the list and presses "DOWN" - focus stays where it was, nothing happens.user navigates from list1 to list2 - the item, that previously had focus in list2, gains focus or item0 gains focus if none was focused previously.
main layout:
<?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>
item layout, used for both lists:
<?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>
Digging through Google's Leanback library sources answered my payers.If you face the same trouble, just wrap your RecyclerViews with this and don't forget to set
private boolean mPersistFocusVertical = false;
if you want the focus to be persisted when searching horizontally, like in my layout.
这篇关于Android TV在RecyclerView之间移动焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!