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

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

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 主要是在使用 RecyclerView 过程中遇到的细碎问题和解决方案。 简单使用 LinearLayoutMan...
    三流之路阅读 3,981评论 0 5
  • 这篇文章分三个部分,简单跟大家讲一下 RecyclerView 的常用方法与奇葩用法;工作原理与ListView比...
    LucasAdam阅读 4,426评论 0 27
  • 一个女人最好的状态:眼里写满故事,脸上却不见风霜,每天化个淡妆,穿上喜欢的衣裳,​不羡慕谁,不嘲笑谁,也不依赖谁,...
    CZYYH阅读 247评论 0 0
  • 今天外面想必也是冬日暖阳 照得人心头都亮堂堂的 一天无课 我也像大多无志青年学生一般 窝在寝室床上糜烂 一边和张先...
    犹他夫人阅读 164评论 0 0
  • 反复无常。我
    a05f63aaac2d阅读 172评论 0 0