RecyclerView制定位置滑动置顶,并实现头部悬停

本文介绍了一种实现RecyclerView中指定位置滚动置顶的方法,并实现了头部视图的悬停效果。通过重写OnScrollListener,可以精确控制列表的滚动行为。

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

前些天产品经理提出了这个需求,花了1天时间终于实现,但最终没有运用到app中,但效果不错,在此记录,以备以后需要。

指定位置滚动置顶

private void smoothMoveToPosition(final int position) {
        //获取第一个跟最后一个可见item
        int firstItem = reGoods.getChildLayoutPosition(reGoods.getChildAt(0));
        int lastItem = reGoods.getChildLayoutPosition(reGoods.getChildAt(reGoods.getChildCount() - 1));
        if (position < firstItem) {
            // 如果要跳转的位置在第一个可见项之前,则smoothScrollToPosition可以直接跳转
            reGoods.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            // 如果要跳转的位置在第一个可见项之后,且在最后一个可见项之前
            // smoothScrollToPosition根本不会动,此时调用smoothScrollBy来滑动到指定位置
            int movePosition = position - firstItem;
            if (movePosition >= 0 && movePosition < reGoods.getChildCount()) {
                int top = reGoods.getChildAt(movePosition).getTop();
                reGoods.smoothScrollBy(0, top);
            }
        } else {
            // 如果要跳转的位置在最后可见项之后,则先调用smoothScrollToPosition将要跳转的位置滚动到可见位置
            // 再通过onScrollStateChanged控制再次调用smoothMoveToPosition,进入上一个控制语句
            reGoods.smoothScrollToPosition(position);
            mShouldScroll = true;
            mToPosition = position;
        }
    }

重写OnScrollListener实现二次滚动和头部悬停

    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            //找到第二个item 离顶部距离 小于头部高度
            View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
            if (view != null) {
                //如果小于等于 view的高度
                if (view.getTop() <= mSuspensionHeight) {
                    //设置悬停view的位置
                    tvGroup.setY(-(mSuspensionHeight - view.getTop()));
                } else {
                    //否则设置悬停就在顶部
                    tvGroup.setY(0);
                }
            }
            //当前位置不等于第一个可见位置  更新悬停view
            if (mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()) {
                //当前位置为第一个可见位置
                mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();
                //头部悬停
                tvGroup.setY(0);
                if (!change) {
                    typesAdapter.setSelect(mCurrentPosition);
                }
                tvGroup.setText(goodsGroups.get(mCurrentPosition).getGroupName());
            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            //测量头部悬停view的高度
            mSuspensionHeight = tvGroup.getHeight();
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                //是否需要二次滚动
                if (mShouldScroll) {
                    mShouldScroll = false;
                    //重新滚动
                    smoothMoveToPosition(mToPosition);
                } else {
                    change = false;
                }

            }
        }
    };
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值