实现Recyclerview 滑动指定item到列表最前端

本文探讨了在Android中使用RecyclerView时,如何将特定Item滚动到列表顶部的两种方法。第一种方法通过判断目标Item的位置并采取不同滚动策略实现;第二种方法则通过继承LinearSmoothScroller并重写相关方法来简化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在百度上面看到两种实现方式

第一种是将目标item分为是否在第一个可见的item之前(即是否已经被滑动到屏幕上方),或是在第一个item之后,最后一个item之前,或在最后一个可见的item之后(这种方式我并没有去做过)

item在第一个可见item之前,直接用smoothScrollToPosition,则当该item移动到可见范围时,它就在RecyclerView顶部
item在可见范围内,即在第一个可见item之后,最后一个可见item之前,那么这时scrollToPosition失效,需要手动计算该item的view距离顶部的距离,用scrollBy自行移动到置顶位置
item在最后一个可见item之后,用smoothScrollToPosition滑动到可见范围 (此时该item在最后一个位置),再获取该item的view,计算到顶部距离,再监听RecyclerView的滑动,对其进行二次滑动到顶部

贴上该方法主要的实现代码
@From:https://blog.csdn.net/weixin_39428125/article/details/89032646

//标记是否需要二次滑动
private boolean shouldMove;
//需要滑动到的item位置
private int mPosition;

/**
 * RecyclerView滑动到指定item函数
 */
private void smoothMoveToPosition(RecyclerView recyclerView, final int position) {
    // 获取RecyclerView的第一个可见位置
    int firstItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
    // 获取RecyclerView的最后一个可见位置
    int lastItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(mRecyclerView.getChildCount() - 1));
    if (position < firstItem) {
        // 指定item在第一个可见item之前
        recyclerView.smoothScrollToPosition(position);
    } else if (position <= lastItem) {
        // 指定item在可见范围内,即在第一个可见item之后,最后一个可见item之前
        int position = position - firstItem;
        if (position >= 0 && position < recyclerView.getChildCount()) {
            // 计算指定item的view到顶部的距离
            int top = recyclerView.getChildAt(position).getTop();
            // 手动滑动到顶部
            recyclerView.smoothScrollBy(0, top);
        }
    } else {
        // 指定item在最后一个可见item之后,用smoothScrollToPosition滑动到可见范围
        // 再监听RecyclerView的滑动,对其进行二次滑动到顶部
        recyclerView.smoothScrollToPosition(position);
        mPositon = position;
        shouldMove = true;
    }
}

…………

/**
 * 监听RecyclerView的滑动,对需要进行二次滑动的item进行滑动
 **/
 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if ( shouldMove && RecyclerView.SCROLL_STATE_IDLE == newState) {
                    shouldMove = false;
                    smoothMoveToPosition(mRecyclerView, mPosition);
                }
            }
        });

第二种方式就比较的简单,继承LinearSmoothScroll,然后重写里面的getHorizontalSnapPreference(),getVerticalSnapPreference()
这两个方法即

public class TopSmoothScroller extends LinearSmoothScroller {
    public TopSmoothScroller(Context context) {
        super(context);
    }              
   @Override
    protected int getHorizontalSnapPreference() {
        return SNAP_TO_START;
    }
    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;  // 将子view与父view顶部对齐
    }

然后在使用的地方

TopSmoothScroller topSmoothScroller = new TopSmoothScroller(GridViewActivity.this);
            topSmoothScroller.setTargetPosition(5);
            gridView.getLayoutManager().startSmoothScroll(topSmoothScroller);

就可实现将目标item显示在列表的最前方

此文仅为作为笔记
From https://blog.csdn.net/weixin_39428125/article/details/89032646

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值