您的位置:首页 > 运维架构

recycleView scrollToPosition 现象

2016-09-23 18:04 1096 查看
如果position对应的view已经显示出来了,那么是不会有滑动现象的

原因?

LinearLayoutManager类中的onLayouthildren方法,约483行

updateAnchorInfoForLayout(recycler, state, mAnchorInfo);


if (updateAnchorFromPendingData(state, anchorInfo)) {
if (DEBUG) {
Log.d(TAG, "updated anchor info from pending information");
}
return;
}


mReverseLayout

Defines if layout should be calculated from end to start.

就是说反过来的布局,默认是false的

// item is not visible.

if (mPendingScrollPositionOffset == INVALID_OFFSET) {

if (getChildCount() > 0) {

// get position of any child, does not matter

int pos = getPosition(getChildAt(0));

anchorInfo.mLayoutFromEnd = mPendingScrollPosition < pos

== mShouldReverseLayout;

}

就是说,如果第一个visible的position 是6,而我要移动到的是第10个,

如果不是反过来布局的,那么就从end开始

so, 调用带有offset的方法,但是参数传0,就可以了

补充:2017-1-4

如果activity是RelativeLayout,recyclerVuiew会两次调用child.Layout,并且当最后一页的时候,例如显示了3的一半,4,5,(6未显示完全),scrollToPositionWithOffset这个时候如果要滚动到3,理想中的位置是3在最顶部,然而,如果recycler below 一个view A,那么,3不会滚动到顶部,而是,距离顶部有一个viewA的距离,也就是说距离屏幕顶部有2个viewA的距离,解决办法 把activity由RelativeLayout改为LinearLayout

最后欢迎关注我的微信公众号:云端看大地

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: