AndroidRecyclerViewLinearSmoothScrollerBUG-即使指定了要滚动到其他位置,它也会滚动到末尾
创始人
2024-10-09 17:32:27
0

在使用 LinearSmoothScroller 滚动 RecyclerView 时,出现这种问题是因为在其 computeScrollVectorForPosition() 方法中返回的向量不正确。所以我们需要重写该方法,以正确指示滚动方向。

下面是一个示例代码,可以在你的 RecyclerView.SmoothScroller 中调用,并返回正确的滚动向量:

public class CustomLinearSmoothScroller extends LinearSmoothScroller {  
    public CustomLinearSmoothScroller(Context context) {  
        super(context);  
    }  
  
    @Override  
    public PointF computeScrollVectorForPosition(int targetPosition) {  
        final int firstChildPos = getFirstChildPosition();  
  
        int direction = targetPosition < firstChildPos ? -1 : 1;  
        return new PointF(0, direction);  
    }  
  
    private int getFirstChildPosition(){  
        RecyclerView.LayoutManager layoutManager = getLayoutManager();  
  
        if (layoutManager == null || layoutManager.getChildCount() == 0) {  
            return RecyclerView.NO_POSITION;  
        }  
  
        View child = layoutManager.getChildAt(0);  
        return layoutManager.getPosition(child);  
    }  
}  

在你的 RecyclerView 中调用如下:

CustomLinearSmoothScroller scroller = new CustomLinearSmoothScroller(context);
scroller.setTargetPosition(targetPosition);
layoutManager.startSmoothScroll(scroller);

相关内容

热门资讯

有挂透视!pokernow辅助... 有挂透视!pokernow辅助工具,微乐手游微乐辅助免费,策略教程(真的有挂)-哔哩哔哩1、首先打开...
必备透视!wepoker插件功... 必备透视!wepoker插件功能辅助器,微信小程序微乐游戏辅助,阶段教程(存在有挂)-哔哩哔哩1、玩...
解迷透视!hhpoker有辅助... 解迷透视!hhpoker有辅助吗,微乐贵阳麻将辅助,举措教程(有挂透视)-哔哩哔哩hhpoker有辅...
详细透视!wepoker有没有... 详细透视!wepoker有没有辅助,微信微乐辅助器免费,模块教程(有挂秘笈)-哔哩哔哩1、任何wep...
关于透视!智星德州可以透视吗,... 关于透视!智星德州可以透视吗,微乐广西小程序脚本,阶段教程(有挂神器)-哔哩哔哩1、每一步都需要思考...
了解透视!wepoker有透视... 了解透视!wepoker有透视吗,2025微乐小程序黑科技,学习教程(有挂实锤)-哔哩哔哩1、上手简...
详情透视!德州透视插件,微乐房... 详情透视!德州透视插件,微乐房间辅助工具在哪下载,机巧教程(有挂方式)-哔哩哔哩1、德州透视插件模拟...
必备透视!cloudpoker... 必备透视!cloudpoker怎么开挂,微信小程序游戏充值破解,办法教程(有挂助手)-哔哩哔哩1、该...
普及透视!德州辅助工具到底怎么... 普及透视!德州辅助工具到底怎么样,微乐自建房辅助入口在哪里,技法教程(有挂实锤)-哔哩哔哩1、很好的...
曝光透视!德州局透视脚本下载安... 曝光透视!德州局透视脚本下载安装最新版本,微乐游戏小程序辅助器免费苹果版,法门教程(讲解有挂)-哔哩...