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

相关内容

热门资讯

透视智能ai!wepoker怎... 透视智能ai!wepoker怎么提高运气(透视)科乐填大坑技巧(都是是有辅助app)-哔哩哔哩1、上...
透视存在!hhpoker德州牛... 透视存在!hhpoker德州牛仔视频(透视)陕西辅助器具适用平台(切实是真的辅助下载)-哔哩哔哩1、...
最终!wepoker透视有没有... 最终!wepoker透视有没有(透视)凑一桌游戏关春天辅助(一直是真的辅助工具)-哔哩哔哩1、下载好...
截至目前!wepoker提高好... 截至目前!wepoker提高好牌率(透视)欢聚水鱼脚本(真是是有辅助下载)-哔哩哔哩1、欢聚水鱼脚本...
据通报!hhpoker有没有辅... 据通报!hhpoker有没有辅助(透视)凑一桌关春天辅助器在哪买(其实是有辅助修改器)-哔哩哔哩1、...
一直以来!hhpoker透视工... 一直以来!hhpoker透视工具(透视)浙江宝宝游戏辅助器(总是有辅助app)-哔哩哔哩1、浙江宝宝...
受玩家影响!aapoker插件... 受玩家影响!aapoker插件(透视)途游四川小程序有挂吗(确实是有辅助工具)-哔哩哔哩1、全新机制...
据通报!智星菠萝透视(透视)打... 据通报!智星菠萝透视(透视)打两圈泰兴辅助(真是有辅助app)-哔哩哔哩暗藏猫腻,小编详细说明打两圈...
一直以来!wepokerh5破... 一直以来!wepokerh5破解(透视)雀神麻雀科技公司(总是存在有辅助辅助器)-哔哩哔哩1、一直以...
记者获悉!拱趴大菠萝怎么开挂(... 记者获悉!拱趴大菠萝怎么开挂(透视)苹果广东雀神破解器(其实是有辅助软件)-哔哩哔哩1、许多玩家不知...