2019-06-02 RecyclerView Item置頂?shù)膬?yōu)雅解決方案(點擊置頂、刷新置頂?shù)龋?/h2>

RecyclerView Item置頂這個需求之前也遇到過,當時實現(xiàn)的方式是計算布局高度、控件高度達到置頂效果,但是計算過程很繁瑣,而且不具備通用性,可能出現(xiàn)各種問題,今天給出的解決方案可以優(yōu)雅的實現(xiàn)置頂效果。

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不在可見范圍,或者布局很復雜,那么你將很難計算它的偏移量。

分析了以上兩個方法發(fā)現(xiàn)它們都不能滿足我們的需求,接下來我們分析下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實際調(diào)用的是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) 方法需要自己創(chuàng)建一個SmoothScroller,下面給出RecyclerView Item置頂?shù)拇a

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);
    }

    /**
     * 以下參數(shù)以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;// 這里是關(guān)鍵,得到的就是置頂?shù)钠屏?    }
}

}
流月菲: 確實有效,厲害了,我的用法是 LinearLayoutManager manager = new TopLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); 然后item點擊的時候調(diào)用: mRecyclerView.smoothScrollToPosition(position);

原文:https://blog.csdn.net/u014453811/article/details/54667052
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容