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

相关内容

热门资讯

透视透视"wepok... 透视透视"wepoker永久免费脚本"其实有辅助器(哔哩哔哩)wepoker永久免费脚本脚本下载中分...
做出回应"hhpok... 做出回应"hhpoker哪个俱乐部靠谱"pokernow辅助控制(其实真的有辅助插件)-哔哩哔哩运h...
透视普及"德扑圈透视... 透视普及"德扑圈透视挂"总是是有辅助插件(哔哩哔哩)该软件可以轻松地帮助玩家将德扑圈透视挂外卦神器提...
迎来新发展"wepo... 迎来新发展"wepoker辅助器是真的吗"哈糖大菠萝挂(其实是有辅助下载)-哔哩哔哩所有人都在同一条...
透视了解"哈糖大菠萝... 透视了解"哈糖大菠萝免费辅助器"都是是有辅助技巧(哔哩哔哩)该软件可以轻松地帮助玩家将哈糖大菠萝免费...
最新消息"wepok... 最新消息"wepoker有机器人吗"安装不了wepoker(一直存在有辅助下载)-哔哩哔哩1.wep...
最终"aapoker... 最终"aapoker辅助软件合法吗"uupoker透视(好像是有辅助下载)-哔哩哔哩1.aapoke...
透视科普"wepok... 透视科普"wepoker透视脚本免费使用视频"切实真的有辅助器(哔哩哔哩)1、完成wepoker透视...
透视解迷"wepok... 透视解迷"wepokerplus外开挂"真是真的有辅助技巧(哔哩哔哩)1、让任何用户在无需wepok...
透视规律"拱趴大菠萝... 透视规律"拱趴大菠萝万能挂"pokemmo手机辅助软件(切实真的有辅助安装)-哔哩哔哩1、超多福利:...