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有没有透视软件挂”开挂辅助脚本(透视)介绍教程是一款可以让一直输的玩家,...
开挂辅助下载"epo... epoker透视是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微...
让我来分享经验“wepoker... 让我来分享经验“wepoker辅助器免费下载”开挂辅助平台(透视)线上教程是一款可以让一直输的玩家,...
开挂辅助工具"wep... wepokerplus透视脚本免费是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需...
今日科普“wepoker技巧攻... 您好:wepoker技巧攻略的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
开挂辅助脚本"aap... 开挂辅助脚本"aapoker辅助怎么用"开挂(透视)辅助工具(竟然有挂);aapoker辅助怎么用A...
分享个大家“wpk开挂挂是真的... 您好:wpk开挂挂是真的吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
开挂辅助神器"wep... 开挂辅助神器"wepoker辅助工具"开挂(透视)辅助软件(有挂攻略);打开点击测试直接进入微信(1...
一分钟揭秘“wepoker透视... wepoker透视是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我...
开挂辅助插件"soh... 您好:sohoo poker辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...