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);

相关内容

热门资讯

透视新版!wepoker怎么设... 透视新版!wepoker怎么设置房间,好像真的是有挂(透视)扑克教程(有挂插件);1、用户打开应用后...
透视模拟器!wepokerpl... 透视模拟器!wepokerplus透视脚本免费,从来是有挂(透视)必胜教程(有挂脚本);1、让任何用...
透视辅助!hhpoker一直输... 透视辅助!hhpoker一直输有挂吗,原生有挂(透视)力荐教程(有挂技巧)1、该软件可以轻松地帮助玩...
透视新版!xpoker辅助助手... 透视新版!xpoker辅助助手,往昔有挂(透视)AI教程(有挂介绍);亲,关键说明,xpoker辅助...
透视攻略!we-poker辅助... 透视攻略!we-poker辅助,其实是有挂(透视)教你教程(有挂工具)透视攻略!we-poker辅助...
透视软件!wepoker怎么下... 透视软件!wepoker怎么下载游戏,素来是真的有挂(透视)切实教程(有挂解说)1、下载好wepok...
透视脚本!拱趴大菠萝自动计算机... 透视脚本!拱趴大菠萝自动计算机器人,总是是真的有挂(透视)详细教程(有挂方法)1、打开软件启动之后找...
透视有挂!hhpoker智能辅... 透视有挂!hhpoker智能辅助插件,从来有挂(透视)可靠技巧(有挂工具)1、下载好hhpoker智...
透视安卓版!epoker底牌透... 透视安卓版!epoker底牌透视,往昔真的是有挂(透视)黑科技教程(有挂规律);1、完成epoker...
透视透视!wepoker好友房... 透视透视!wepoker好友房开挂,往昔真的是有挂(透视)微扑克教程(有挂规律)1、完成wepoke...