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

相关内容

热门资讯

wepoke有没有挂!手机云扑... wepoke有没有挂!手机云扑克辅助是真的假的,wpk俱乐部软件,2025新版教程(有挂攻略);人气...
aapoker透明挂!wpk德... aapoker透明挂!wpk德州辅助,(智星德州)原来真的是有挂(详细辅助技巧教程),支持语音通讯、...
wepoke辅助机器人!wep... wepoke辅助机器人!wepoke人有挂吗,德州之星有外挂,扑克教程(有挂辅助挂);无聊就玩这款德...
德扑之星有猫腻!微扑克软件发牌... 德扑之星有猫腻!微扑克软件发牌管理,(aa扑克)真是是真的有挂(详细辅助专业教程)1、这是跨平台的德...
德州ai辅助有用!impoke... 德州ai辅助有用!impoker有挂吗,wepoke用模拟器有有用,存在挂教程(有挂了解)1)德州a...
wpk提高胜率!wpk发牌算法... wpk提高胜率!wpk发牌算法,(nzt德州)竟然真的是有挂(详细辅助存在挂教程)科技教程也叫必备教...
wepoke辅助插件!wepo... wepoke辅助插件!wepoke辅助,德州之星可能有外挂,细节方法(有挂黑科技)1、构建自己的微扑...
wpk透视辅助测试!微扑克被系... wpk透视辅助测试!微扑克被系统制裁,(德州wpk)切实真的有挂(详细辅助实用技巧);玩家在wpk透...
we辅助poker德之星!we... we辅助poker德之星!wepoke有插件吗,wopoker手机辅助,技巧教程(有挂教学)是一款可...
德扑ai智能!wepoke软件... 德扑ai智能!wepoke软件透明挂存在吗,(AAPOKEr)其实存在有挂(详细辅助技巧教程);值得...