recycleView scrollToPosition 现象

来源:互联网 发布:淘宝客服务费结算方式 编辑:程序博客网 时间:2024/06/05 13:25

如果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

0 0