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脚本辅... 透视工具!pokemmo脚本辅助下载,德扑之心免费透视,学习教程(一直存在有挂)-哔哩哔哩1)pok...
透视ai代打!wepoker轻... 透视ai代打!wepoker轻量版有透视吗,牵手游戏辅助,一贯是有挂(了解必备教程)-哔哩哔哩暗藏猫...
透视规律!wepoker有用吗... 透视规律!wepoker有用吗(透视)开挂辅助器(切实真的是有挂)-哔哩哔哩透视规律!wepoker...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载,哈糖大菠萝破解器,秘籍教程(确实真的有挂)-哔哩哔哩1、不需...
受玩家影响!aapoker插件... 受玩家影响!aapoker插件,白银胡乐辅助脚本最新版安装方法,本来有挂(关于必备教程)-哔哩哔哩亲...
透视稳定!pokemmo辅助器... 透视稳定!pokemmo辅助器手机版下载(透视)开挂辅助技巧(其实存在有挂)-哔哩哔哩1)pokem...
透视好牌!wepoker开辅助... 透视好牌!wepoker开辅助能查到吗,wepokerplus脚本,教材教程(果然存在有挂)-哔哩哔...
透视好牌!wpk辅助,兴动休闲... 透视好牌!wpk辅助,兴动休闲辅助,果然是真的挂(解密AI教程)-哔哩哔哩1、下载好兴动休闲辅助辅助...
透视输赢!wepoker好友房... 透视输赢!wepoker好友房开挂(透视)开挂辅助神器(果然是真的挂)-哔哩哔哩1、不需要AI权限,...
透视能赢!德扑圈透视挂,wep... 透视能赢!德扑圈透视挂,wepoker怎么设置房间,妙计教程(好像有挂)-哔哩哔哩1、在wepoke...