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

相关内容

热门资讯

我来教大家!pokemmo脚本... 这是一款非常优秀的wepoker透视底牌脚本 ia辅助检测软件,能够让你了解到wepoker透视底牌...
透视真的!pokemmo脚本最... 透视真的!pokemmo脚本最新版(透视)一贯有挂(详细辅助力荐教程)辅助器中分为三种模型:软件透明...
揭秘真相!红龙poker辅助器... 揭秘真相!红龙poker辅助器免费观看,wepokerplus辅助,详细教程(有挂方法)1、点击下载...
透视有挂!pokemmo辅助官... 透视有挂!pokemmo辅助官网(透视)切实是真的有挂(详细辅助科技教程)1、该软件可以轻松地帮助玩...
总算了解!aapoker发牌逻... 总算了解!aapoker发牌逻辑,拱趴大菠萝万能挂,力荐教程(有挂技巧)这是由厦门游乐互动科技有限公...
透视挂透视!德扑圈透视挂(透视... 透视挂透视!德扑圈透视挂(透视)确实存在有挂(详细辅助德州教程)1、玩家可以在软件透明挂俱乐部对游戏...
指导大家!wepoker透视辅... 指导大家!wepoker透视辅助下载,wepoker游戏的安装教程,靠谱教程(有挂软件);实战中需综...
透视科技!哈糖大菠萝破解器(透... 透视科技!哈糖大菠萝破解器(透视)一贯有挂(详细辅助攻略教程);1、系统规律教程、辅助透视等服务,为...
必备教程!菠萝辅助器免费版的特... 必备教程!菠萝辅助器免费版的特点,we poker辅助器下载,必胜教程(有挂攻略)是一款可以让一直输...
透视脚本!约局吧德州真的有透视... 透视脚本!约局吧德州真的有透视挂吗(透视)都是真的是有挂(详细辅助力荐教程)1、每一步都需要思考,不...