RecyclerView Item置頂方法(點擊置頂、刷新置頂等)

RecyclerView本身兩個常用的滑動方法:

smoothScrollToPosition( int position )方法

smoothScrollBy( int dx, int dy )方法

smoothScrollToPosition( int position )方法可以滑動到指定位置的Item,直到該Item完全可見,也就是說如果該Item本身就在RecyclerView可見範圍,那麼RecyclerView將不會滑動,如果該Item不在可見範圍則滑動到RecyclerView的第一個或者最後一個可見位置

smoothScrollBy( int dx, int dy )方法可以指定RecyclerView滑動的偏移量,需要精確地計算得到偏移量,如果你想滑動的Item不在可見範圍,或者佈局很複雜,那麼你將很難計算它的偏移量。

分析了以上兩個方法發現它們都不能滿足我們的需求,接下來我們分析下smoothScrollToPosition( int position )方法的源碼
 

public void smoothScrollToPosition(int position) {
        if (mLayoutFrozen) {
            return;
        }
        if (mLayout == null) {
            Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " +
                    "Call setLayoutManager with a non-null argument.");
            return;
        }
        //mLayout就是初始化RecyclerView設置的LayoutManager
        mLayout.smoothScrollToPosition(this, mState, position);
    }

其實RecyclerView實際調用的是LayoutManager中的代碼,這就給出一個解決的思路:我們可以自定一個LayoutManager,然後複寫LayoutManager中的smoothScrollToPosition(this, mState, position) 方法,再看LayoutManager的smoothScrollToPosition方法
 

 /**
         * <p>Smooth scroll to the specified adapter position.</p>
         * <p>To support smooth scrolling, override this method, create your {@link SmoothScroller}
         * instance and call {@link #startSmoothScroll(SmoothScroller)}.
         * </p>
         * @param recyclerView The RecyclerView to which this layout manager is attached
         * @param state    Current State of RecyclerView
         * @param position Scroll to this adapter position.
         */
        public void smoothScrollToPosition(RecyclerView recyclerView, State state,
                int position) {
            Log.e(TAG, "You must override smoothScrollToPosition to support smooth scrolling");
        }

可以看到複寫LayoutManager中的smoothScrollToPosition(this, mState, position) 方法需要自己創建一個SmoothScroller,下面給出RecyclerView Item置頂的代碼

public class TopLayoutManager extends LinearLayoutManager {
 
    public TopLayoutManager(Context context) {
        super(context);
    }
 
    public TopLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }
 
    public TopLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
 
    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        RecyclerView.SmoothScroller smoothScroller = new TopSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
 
    private static class TopSmoothScroller extends LinearSmoothScroller {
 
        TopSmoothScroller(Context context) {
            super(context);
        }
 
        /**
         * 以下參數以LinearSmoothScroller解釋
         * @param viewStart RecyclerView的top位置
         * @param viewEnd RecyclerView的bottom位置
         * @param boxStart Item的top位置
         * @param boxEnd Item的bottom位置
         * @param snapPreference 判斷滑動方向的標識(The edge which the view should snap to when entering the visible
         *                       area. One of {@link #SNAP_TO_START}, {@link #SNAP_TO_END} or
         *                       {@link #SNAP_TO_END}.)
         * @return 移動偏移量
         */
        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
            return boxStart - viewStart;// 這裏是關鍵,得到的就是置頂的偏移量
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章