问题描述
我目前正在使用常规 RecyclerView
和 GridLayoutManager
和不同的 spanCount
取决于 viewType
安卓电视应用.一切正常,但我有两个问题:
I'm currently using a regular RecyclerView
with GridLayoutManager
with different spanCount
depeding on the viewType
for an Android TV app. All is working decent but I have 2 issues:
- 如果您长按 dpad 在项目之间快速滚动,有时焦点会丢失到不是 RecyclerView 子项的视图.
- 如何告诉
RecyclerView
将当前焦点视图保持在网格的中心?
- If you long press the dpad down to scroll fast between the items sometimes the focus is lost to a view that isn't a child of the RecyclerView.
- How can I tell the
RecyclerView
to keep the current focused view in the center of the grid?
似乎列出的问题是通过使用 LeanBack
库中的 VerticalGridView
修复的,但它使用的 LayoutManger
是内部的,并且没有t 支持spanCount
.
It seems that the issues listed are fixed by using the VerticalGridView
from LeanBack
library but the LayoutManger
that it uses is internal and doesn't support spanCount
.
推荐答案
Recycler 视图适用于 android TV.您可以包含的可能解决方案是:
Recycler view works fine with android TV.Possible solutions you can include are:
1.在view中添加focusable和focusableInTouchode.通过代码添加focusListner,每次点击view时请求焦点.
1.add focusable and focusableInTouchode to view.Add focusListner through the code and request focus each time when the view is clicked.
2.要保持 Recycler View 焦点项目在中心,你必须像这个例子一样覆盖布局管理器.
2.To keep Recycler View focused item in the centre you have to override layout manager just like this example.
或
使用 layoutManager.scrollToPositionWithOffset(position,offset) 其中以位置为中心的视图位置和偏移量是回收器视图宽度/2.
use layoutManager.scrollToPositionWithOffset(position,offset) where position-focused view position and offset is the recycler view width/2.
这篇关于Android TV RecyclerView 焦点交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!