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

相关内容

热门资讯

玩家必知教程作弊器方法!打两圈... 玩家必知教程作弊器方法!打两圈兴化麻将多功能作弊器方法,打两圈兴化麻将都是真的有作弊器方法1、在打两...
热门推荐作弊器app!乐夏多功... 热门推荐作弊器app!乐夏多功能作弊器app,乐夏竟然存在有作弊器app1、乐夏破解器简单,乐夏机器...
盘点一款作弊器教程!友乐广西麻... 盘点一款作弊器教程!友乐广西麻将多功能作弊器教程,友乐广西麻将切实是有作弊器教程盘点一款作弊器教程!...
专业讨论辅助器攻略!琼崖海南麻... 专业讨论辅助器攻略!琼崖海南麻将多功能辅助器攻略,琼崖海南麻将确实是有辅助器攻略1、这是跨平台的琼崖...
玩家实测辅助器软件!心悦辽宁麻... 玩家实测辅助器软件!心悦辽宁麻将多功能辅助器软件,心悦辽宁麻将总是真的有辅助器软件1、很好的工具软件...
揭秘攻略作弊器脚本!打两圈泰州... 揭秘攻略作弊器脚本!打两圈泰州麻将多功能作弊器脚本,打两圈泰州麻将切实真的有作弊器脚本该软件可以轻松...
指导大家辅助器技巧!福建众娱多... 指导大家辅助器技巧!福建众娱多功能辅助器技巧,福建众娱一贯是真的有辅助器技巧1、游戏颠覆性的策略玩法...
玩家必看秘籍开挂app!去玩麻... 玩家必看秘籍开挂app!去玩麻将多功能开挂app,去玩麻将一贯存在有开挂app运去玩麻将辅助工具,进...
分辨真假辅助脚本!城市打炸弹多... 分辨真假辅助脚本!城市打炸弹多功能辅助脚本,城市打炸弹都是真的是有辅助脚本1、全新机制【城市打炸弹a...
科技分享辅助教程!毳毳多功能辅... 科技分享辅助教程!毳毳多功能辅助教程,毳毳原来存在有辅助教程小薇(辅助器软件下载)致您一封信;亲爱毳...