我有一个按钮可以停止和启动回收器 View 的自动滚动。它确实工作正常,但是当我手动滚动然后单击按钮开始自动滚动时,它确实从头开始。我怎样才能让它继续在该位置滚动?
这是启动自动滚动的函数
public void startAutoScroll() {
final RecyclerViewAdapter myadapter = new RecyclerViewAdapter(this, lstUserCard);
final int speedScroll = 2400;
final Handler handler = new Handler();
if (timer == null && timerTask == null) {
timer = new Timer();
timerTask = new TimerTask() {
int count = 0;
boolean flag = true;
@Override
public void run() {
if (count < myadapter.getItemCount()) {
if (count == myadapter.getItemCount() - 1) {
flag = false;
} else if (count == 0) {
flag = true;
}
if (flag) count++;
else count--;
recyclerView.smoothScrollToPosition(count);
handler.postDelayed(timerTask, speedScroll);
}
}
};
handler.postDelayed(timerTask, speedScroll);
}
}
希望得到一些建议,提前致谢。
最佳答案
每次启用/禁用自动滚动时,您都需要获取索引并恢复索引。
获取当前顶部可见行索引;
int index = linearLayoutManager.findFirstVisibleItemPosition();
View v = linearLayoutManager.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - linearLayoutManager.getPaddingTop());
将recyclerview恢复到该索引;
linearLayoutManager.scrollToPositionWithOffset(top, 0);
关于java - Recyclerview 继续滚动到位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62388811/