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

相关内容

热门资讯

透视阶段!wepoker私人局... 透视阶段!wepoker私人局透视(德普之星介绍)好像真的是有辅助技巧(哔哩哔哩)1、下载好wepo...
透视要领!德普之星辅助器怎么用... 透视要领!德普之星辅助器怎么用(WePoKer黑科技)确实真的是有辅助软件(哔哩哔哩)1、全新机制【...
透视窍要!hardrock透视... 您好,hardrock透视工具这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
透视绝活!hhpoker德州透... 透视绝活!hhpoker德州透视挂(AApoker插件)确实是有辅助插件(哔哩哔哩)1、起透看视 h...
透视指南书!hhpoker买挂... 透视指南书!hhpoker买挂(WePoKer助手)一贯是有辅助神器(哔哩哔哩)1、hhpoker买...
透视攻略!wepoker究竟有... 透视攻略!wepoker究竟有没有透视(HHpoker有用)果然存在有辅助攻略(哔哩哔哩)wepok...
透视窍要!wepoker免费脚... 透视窍要!wepoker免费脚本咨询(WePoKer工具)都是存在有辅助插件(哔哩哔哩)1、wepo...
透视阶段!hhpoker开挂教... 透视阶段!hhpoker开挂教程(WePoKer app)竟然真的有辅助神器(哔哩哔哩)hhpoke...
透视办法!wpk辅助软件(WP... 透视办法!wpk辅助软件(WPK透视版)切实是真的有辅助技巧(哔哩哔哩)1、wpk辅助软件免费脚本咨...
透视积累!德州hhpoker是... 透视积累!德州hhpoker是真的吗(WePoKer辅助器)切实是有辅助脚本(哔哩哔哩)1、德州hh...