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)附上博文鏈接!