2019-06-02 RecyclerView Item置顶的优雅解决方案(点击置顶、刷新置顶等)

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方法
/**
*

Smooth scroll to the specified adapter position.


*

To support smooth scrolling, override this method, create your {@link SmoothScroller}
* instance and call {@link #startSmoothScroll(SmoothScroller)}.
*


* @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;// 这里是关键,得到的就是置顶的偏移量
    }
}

}
流月菲: 确实有效,厉害了,我的用法是 LinearLayoutManager manager = new TopLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); 然后item点击的时候调用: mRecyclerView.smoothScrollToPosition(position);

原文:https://blog.csdn.net/u014453811/article/details/54667052
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(2019-06-02 RecyclerView Item置顶的优雅解决方案(点击置顶、刷新置顶等))