以前,当我想将 ListView
滚动到所需位置时,我会使用
listView.setSelection(row);
ListView
将在没有任何动画的情况下滚动 - Android List View set default position without animation
现在,我想在 RecyclerView
上实现同样的效果。我尝试执行
((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(row, 0);
但是,有滚动动画,我想避免。
有什么方法可以在没有动画的情况下以编程方式执行滚动?
这就是我想要的动画 - https://youtu.be/OKsUKwBLoks
请注意,我已经尝试过以下方法。它们都会生成动画。
- scrollToPositionWithOffset
- 平滑滚动到位置
- 滚动到位置
最佳答案
我知道我回答这个问题有点晚了,但这可能对其他人有帮助。 尝试以下(至少我在使用时看不到任何动画):
Kotlin :
//-- Immediately jump to the position in RecyclerView without delay or animation.
mRecyclerView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
mRecyclerView.scrollToPosition(position)
mRecyclerView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
Java:
mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mRecyclerView.scrollToPosition(position);
mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
关于android - RecyclerView 滚动到没有动画的所需位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34117203/