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

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

RecyclerView本身兩個(gè)常用的滑動(dòng)方法:

smoothScrollToPosition( int position )方法

smoothScrollBy( int dx, int dy )方法

smoothScrollToPosition( int position )方法可以滑動(dòng)到指定位置的Item,直到該Item完全可見(jiàn),也就是說(shuō)如果該Item本身就在RecyclerView可見(jiàn)范圍,那么RecyclerView將不會(huì)滑動(dòng),如果該Item不在可見(jiàn)范圍則滑動(dòng)到RecyclerView的第一個(gè)或者最后一個(gè)可見(jiàn)位置

smoothScrollBy( int dx, int dy )方法可以指定RecyclerView滑動(dòng)的偏移量,需要精確地計(jì)算得到偏移量,如果你想滑動(dòng)的Item不在可見(jiàn)范圍,或者布局很復(fù)雜,那么你將很難計(jì)算它的偏移量。

分析了以上兩個(gè)方法發(fā)現(xiàn)它們都不能滿足我們的需求,接下來(lái)我們分析下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設(shè)置的LayoutManager
mLayout.smoothScrollToPosition(this, mState, position);
}
其實(shí)RecyclerView實(shí)際調(diào)用的是LayoutManager中的代碼,這就給出一個(gè)解決的思路:我們可以自定一個(gè)LayoutManager,然后復(fù)寫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");
}

可以看到復(fù)寫LayoutManager中的smoothScrollToPosition(this, mState, position) 方法需要自己創(chuàng)建一個(gè)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 判斷滑動(dòng)方向的標(biāo)識(shí)(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 移動(dòng)偏移量
     */
    @Override
    public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
        return boxStart - viewStart;// 這里是關(guān)鍵,得到的就是置頂?shù)钠屏?    }
}

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

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

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

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

  • 主要是在使用 RecyclerView 過(guò)程中遇到的細(xì)碎問(wèn)題和解決方案。 簡(jiǎn)單使用 LinearLayoutMan...
    三流之路閱讀 4,168評(píng)論 0 5
  • 這篇文章分三個(gè)部分,簡(jiǎn)單跟大家講一下 RecyclerView 的常用方法與奇葩用法;工作原理與ListView比...
    LucasAdam閱讀 4,687評(píng)論 0 27
  • 一個(gè)女人最好的狀態(tài):眼里寫滿故事,臉上卻不見(jiàn)風(fēng)霜,每天化個(gè)淡妝,穿上喜歡的衣裳,?不羨慕誰(shuí),不嘲笑誰(shuí),也不依賴誰(shuí),...
    CZYYH閱讀 284評(píng)論 0 0
  • 今天外面想必也是冬日暖陽(yáng) 照得人心頭都亮堂堂的 一天無(wú)課 我也像大多無(wú)志青年學(xué)生一般 窩在寢室床上糜爛 一邊和張先...
    猶他夫人閱讀 179評(píng)論 0 0
  • 反復(fù)無(wú)常。我
    a05f63aaac2d閱讀 185評(píng)論 0 0

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