我有一个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));
}
}
}
我对如何在滚动时管理焦点的任何想法都持开放态度。
intercepFocusSearch()
。感谢您发布这个错误! - Chad Bingham