[英]Programmatically scroll a RecyclerView with GridLayoutManager
I have already used this: 我已经使用过:
mRecyclerView.getLayoutManager().scrollToPosition(0);
to programmatically scroll a RecyclerView
with a LinearLayoutManager
. 使用
LinearLayoutManager
以编程方式滚动RecyclerView
。
I was trying to do the same with a RecyclerView
with a GridLayoutManager
. 我试图对带有
GridLayoutManager
的RecyclerView
进行同样的操作。
The problem is that scrollToPosition does not really make sense because an item can be in the middle of a line. 问题在于,scrollToPosition并没有真正意义,因为项目可以位于一行的中间。 Anyway I tried that code and it didn't do anything.
无论如何,我尝试了该代码,但是它什么也没做。
Is there a way to programmatically scroll a RecyclerView
with a GridLayoutManager
? 有没有办法通过
GridLayoutManager
以编程方式滚动RecyclerView
?
Actually: 其实:
mRecyclerView.getLayoutManager().scrollToPosition(0);
works. 作品。 In my case it wasn't because I was calling it synchronously in the method:
就我而言,这不是因为我在方法中同步调用它:
public void onDestroyActionMode(ActionMode mode)
It was enough to post a runnable (without any delay) to the looper of the main thread. 将可运行的消息(没有任何延迟)发布到主线程的循环程序就足够了。
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mRecyclerView != null) {
mRecyclerView.scrollToPosition(0);
}
}
});
You can use method "scrollVerticallyBy" of GridLayoutManager 您可以使用GridLayoutManager的方法“ scrollVerticallyBy”
int scrollVerticallyBy (int dy,
RecyclerView.Recycler recycler,
RecyclerView.State state)
Scroll vertically by dy pixels in screen coordinates and return the distance traveled. 垂直滚动屏幕坐标中的dy像素,然后返回移动的距离。 The default implementation does nothing and returns 0.
默认实现不执行任何操作,并返回0。
Ref: scrollVerticallyBy 参考: scrollVerticallyBy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.