recycleView scrollToPosition 現象

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


最後歡迎關注我的微信公衆號:雲端看大地
這裏寫圖片描述

發佈了121 篇原創文章 · 獲贊 19 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章