RecyclerView采用StaggeredGridLayoutManager布局之后, 如果在滚动之后要保持item焦点位置相对不变, 只需要简单的代码处理就行了, 在滚动前记忆span位置, 滚动后通过遍历可视区域item, 定位相应span位置的view, 然后请求焦点, 大致如下
// 成员变量
int mSpanPosition = -1;
// 滚动之前记忆item相对位置
View focusChild = getFocusedChild();
StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) focusChild.getLayoutParams();
mSpanPosition = lp.getSpanIndex();
// 滚动之后找到对应item进行requestFocus即可, firstPos为第一个可视item位置, SPAN_SIZE为spanCount
for (int i = firstPos; i < firstPos + SPAN_SIZE; i++) {
View item = getLayoutManager().findViewByPosition(i);
StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) item.getLayoutParams();
if (lp.getSpanIndex() == mSpanPosition) {
focusView = item;
break;
}
}
我要评论