[英]Smoothscroll with RecyclerView put item on bottom of list
I want the user to be able to navigate to certain positions within a RecyclerView
. 我希望用户能够导航到RecyclerView
某些位置。 I can determine the position of the items and when I use recycleView.smoothScrollToPosition()
it scrolls to the correct item. 我可以确定项目的位置,当我使用recycleView.smoothScrollToPosition()
它会滚动到正确的项目。
The problem is this item is at the bottom of the screen. 问题是此项目位于屏幕底部。 How can I scroll so that the item appears at the top (it must be the first visible item). 如何滚动,使该项显示在顶部(必须是第一个可见项)。
Here is the code I use to setup the RecyclerView
: 这是我用来设置RecyclerView
的代码:
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(actionAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(InteractionTimelineActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setOnScrollListener(new RecyclerScrollListner());
And this is how I scroll: 这就是我的滚动方式:
if (recycleView != null) {
recycleView.smoothScrollToPosition(position + 1);
}
You have to calculate how many items fit on one screen and use that as offset when you scroll! 您必须计算一个屏幕上可以容纳多少个项目,并在滚动时将其用作偏移量! You can use the methods findFirstCompletelyVisibleItemPosition()
and findLastCompletelyVisibleItemPosition()
of the LayoutManager
to calculate the offset like this: 您可以使用LayoutManager
findFirstCompletelyVisibleItemPosition()
和findLastCompletelyVisibleItemPosition()
LayoutManager
来计算偏移量,如下所示:
int offset = getLayoutManager()).findLastCompletelyVisibleItemPosition()
- getLayoutManager()).findFirstCompletelyVisibleItemPosition();
And once you have the offset you can scroll like this: 一旦有了偏移量,就可以像这样滚动:
recycleView.smoothScrollToPosition(Math.min(position + offset + 1, adapter.getItemCount()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.