[英]android LinearLayoutManager scrollToPositionWithOffset not work if RecyclerView is too short
LinearLayoutManager.scrollToPositionWithOffset(pos, 0)
works great if the sum of RecyclerView
's all children's height is big than screen height. 如果
RecyclerView
的所有子项的高度之和大于屏幕高度LinearLayoutManager.scrollToPositionWithOffset(pos, 0)
效果很好。 But it does not work if the sum of RecyclerView
's all children's height is small than screen height. 但是,如果
RecyclerView
的所有子项的高度之和小于屏幕高度,那将不起作用。
Let's say I have an Activity
and a RecyclerView
as it's root view. 假设我有一个
Activity
和一个RecyclerView
作为其根视图。 RecyclerView
's width
and height
are both match_parent
. RecyclerView
的width
和height
均为match_parent
。 This RecyclerView
has 3 items and the sum of these 3 child view's height is small than screen height. 此
RecyclerView
有3个项目,这3个子视图的高度之和小于屏幕高度。 I want to hide first item when Activity
is onCreated
. 我想在
Activity
为onCreated
时隐藏第一项。 User will see second item at start. 用户将在开始时看到第二项。 If user scroll down, he still can see first item.
如果用户向下滚动,他仍然可以看到第一项。 So I call
LinearLayoutManager.scrollToPositionWithOffset(1, 0)
. 所以我叫
LinearLayoutManager.scrollToPositionWithOffset(1, 0)
。 But it won't work since the sum of RecyclerView
's all children's height is small than screen height. 但是,由于
RecyclerView
的所有子项的高度之和小于屏幕高度,所以它不起作用。
How can I make RecyclerView
scroll to specific position even though the sum of RecyclerView
's all children's height is small than screen height. 即使
RecyclerView
的所有子项的高度之和小于屏幕高度,如何使RecyclerView
滚动到特定位置。
Following is my code according to @Haran Sivaram's answer: 以下是根据@Haran Sivaram的回答我的代码:
Item first = new Item();
Item second = new Item();
Item third = new Item();
List<Item> list = Arrays.asList(first, second, three);
adapter.add(list);
adapter.notifyDataSetChanged();
recyclerView.post(new Runnable() {
@Override
public void run() {
int sumHeight = 0;
for (int i = 0; i < recyclerView.getChildCount(); i++) {
View view = recyclerView.getChildAt(i);
sumHeight += view.getHeight();
}
if (sumHeight < recyclerView.getHeight()) {
adapter.addItem(new VerticalSpaceViewModel(recyclerView.getHeight() - sumHeight + recyclerView.getChildAt(0).getHeight()));
}
linearLayoutManager.scrollToPositionWithOffset(1, 0);
}
});
It worked. 有效。 But has some small issues.
但是有一些小问题。
What you need to do is to increase the height of the recycler view to a minimum height
which will allow scrolling and hide your first element (screen height + height of the first item)
. 您需要做的是将回收器视图的
minimum height
增加到minimum height
,这将允许滚动并隐藏您的第一个元素(screen height + height of the first item)
。 You can achieve this by adding a dummy element as the last element and setting it's height or you could also do this using padding/margins (Have not tried this). 您可以通过添加一个虚拟元素作为最后一个元素并设置其高度来实现此目的,或者也可以使用padding / margins来做到这一点(尚未尝试过)。
This also needs to be done dynamically once the view is drawn (You can do it statically if you are aware of the sizes of each item beforehand - I will not recommend this method though). 绘制视图后,还必须动态完成此操作(如果您事先知道每个项目的大小,则可以静态地进行操作-不过,我不建议您使用此方法)。
Use an onGLobalLayoutListner
to get a callback
once the view is drawn, do your measurements here and update the height. 绘制视图后,请使用
onGLobalLayoutListner
获取callback
,在此处进行测量并更新高度。 Now the scroll with offset should work fine. 现在,具有偏移量的滚动应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.