RecyclerWheelPicker icon indicating copy to clipboard operation
RecyclerWheelPicker copied to clipboard

刚发现一个重复调用 scrollTargetPositionToCenter 时的问题

Open listenwinding opened this issue 3 years ago • 1 comments

问题前情提要:我们现在要做一个摇一摇的功能,实现随机选数,可能会多次调用 scrollTargetPositionToCenter 方法

情况复现:第一次随机出现的数时是正确的,后面再次出现的数就有问题了

原因:通过查看你的源码发现,你在处理滚动时,滚动距离的计算是 itemHeight * position,这样会存在一个问题,如果 在调用该方法之前 当前轮盘数据展示的不是第一条数据的话,这个 距离计算的结果就会有偏差。

listenwinding avatar Apr 08 '21 04:04 listenwinding

我目前这样改了一下

void scrollTargetPositionToCenter(final int position, int itemHeight) { if (position < 0) return;

    int curCenterPn=findCenterItemPosition();
    int distance = itemHeight * position;
    if(curCenterPn!=RecyclerView.NO_POSITION) {
        distance = itemHeight * (position-curCenterPn);
    }

    mRecyclerView.smoothScrollBy(0, distance);
}

listenwinding avatar Apr 08 '21 04:04 listenwinding