简体   繁体   English

使用RecyclerView进行平滑滚动,将项目放在列表底部

[英]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.

相关问题 如何在适当的时间内通过两个项目之间的随机位置平滑回收Recyclerview - how to smoothscroll recyclerview by a random place between two item in a proper time NestedScrollView smoothScroll内部的RecyclerView - RecyclerView inside NestedScrollView smoothScroll 在RecyclerView的底部添加一个项目 - Adding an item to the bottom of a RecyclerView RecyclerView适合项目到底部 - RecyclerView fit item to bottom 每次将项目添加到列表时如何使RecyclerView不会滚动到底部 - How to make RecyclerView not scroll to the bottom each time an item is added to the list 在项目上单击RecyclerView的监听器 - 如何在回收站视图中使用项目的位置来平滑滚动到另一个列表视图? - On Item Click Listener for RecyclerView - How can I use the position of an item in a recycler view to smoothscroll to another listview? 将物品粘在RecyclerView的底部 - Have an item stick to the bottom of a RecyclerView recyclerView 列表中的最后一项 - The last item in the recyclerView list 将最后一项放在列表视图的底部 - Put the last item in the listview at the bottom 如何将最后一个文档添加到recyclerview的底部? - How to put the last document added to the bottom of recyclerview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM