Cha*_*ham 5 android focus android-recyclerview
我有一个RecyclerView,通常有两列,最多八列.我们使用了很多D-PAD导航.滚动时我们遇到问题,焦点项目将从左向右跳跃.看照片:

我注意到,如果下一个出现的项目被缓存,滚动时没有焦点问题.我遇到的另一个问题是我的焦点项目可能出现在我的粘贴标题下方.这是不希望的.所以我觉得如果我滚动它就会有一个"阈值".这样,当焦点位于屏幕外的一个项目内时,它将滚动.这样,焦点永远不会位于最底层,也不会位于顶部.
考虑到这一点,我尝试了这种方法没有运气:
RecyclerView rv;
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!v.hasFocus()) {
Log.w(TAG, "View v did not have focus");
return;
}
final int index = rv.getChildPosition(v); //adapter pos
if(index == NO_POSITION) {
Log.w(TAG, "Recycler view did not have view");
return;
}
int position = rv.indexOfChild(v); // layout pos
int lastPos = rv.getChildCount(); // layout pos
int span = getLayoutManager().getSpanCount();
int threshold = 2 * span;
Log.d(TAG, String.format("Position: %1$d. lastPos: %2$d. span: %3$d. threshold: %4$d", position, lastPos, span, threshold));
if (position >= (lastPos - threshold)) {
//scroll down if possible
int bottomIndex = rv.getChildPosition(rv.getChildAt(lastPos));
if (bottomIndex < getItemCount()) {
//scroll down
int scrollBy = v.getHeight();
recycler.scrollBy(0, scrollBy);
Log.d(TAG, String.format("Scrolling down by %d", scrollBy));
}
} else if (position <= threshold) {
//scroll up if possible
int topIndex = rv.getChildPosition(rv.getChildAt(0));
if (topIndex > 0) {
//scroll up
int scrollBy = v.getHeight();
recycler.scrollBy(0, -scrollBy);
Log.d(TAG, String.format("Scrolling up by %d", -scrollBy));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我对滚动时如何管理焦点的任何想法持开放态度.
我向AOSP问题跟踪器报告了这个错误:问题190526
正如我可以从源看到的问题是,因为GridLayoutManager使用LinearLayoutManager的执行onFocusSearchFailed()当焦点接近的内部边界,被称为RecyclerView.LinearLayoutManager的实现只提供第一个/最后一个(取决于滚动方向)元素.因此焦点跳转到新行的第一个/最后一个元素.
| 归档时间: |
|
| 查看次数: |
10847 次 |
| 最近记录: |