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分钟了解(线上德州aapok... 1分钟了解(线上德州aapoker)外挂透明挂辅助神器(透视)2025新版总结(2024已更新)(哔...
两分钟了解(德扑之星埋牌)黑科... 两分钟了解(德扑之星埋牌)黑科技透明挂辅助下载(透视)透明教程(2023已更新)(哔哩哔哩)是一款可...
十分钟了解(wePOKE)软件... 十分钟了解(wePOKE)软件透明挂辅助软件(透视)透明挂教程(2024已更新)(哔哩哔哩)是一款可...
9分钟了解(微扑克技术)软件透... 您好,微扑克技术这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游...
1分钟了解(Wepoke数据)... 1分钟了解(Wepoke数据)外挂智能ai辅助黑科技(透视)曝光教程(2026已更新)(哔哩哔哩);...
9分钟了解(智星德州)软件透明... 9分钟了解(智星德州)软件透明挂辅助工具(透视)切实教程(2024已更新)(哔哩哔哩);一、智星德州...
第2分钟了解(Wepoke必备... 第2分钟了解(Wepoke必备)黑科技透明挂辅助下载(透视)力荐教程(2020已更新)(哔哩哔哩)是...
八分钟了解(Wepoke私人局... 八分钟了解(Wepoke私人局)外挂智能ai辅助下载(透视)详细教程(2026已更新)(哔哩哔哩)是...
第二分钟了解(德扑之星app)... 第二分钟了解(德扑之星app)软件透明挂辅助安装(透视)AI教程(2021已更新)(哔哩哔哩)是一款...
八分钟了解(德州ai智能)黑科... 八分钟了解(德州ai智能)黑科技透明挂辅助工具(透视)扑克教程(2024已更新)(哔哩哔哩)是一款可...