如何以编程方式滚动到回收器视图的底部?(复制)

时间:2022-08-25 07:39:27

This question already has an answer here:

这个问题已经有了答案:

I want to scroll to the bottom of a recycler view on click of a button, how do I do this?

我想滚动到底部的回收视图点击一个按钮,我怎么做?

2 个解决方案

#1


24  

You have to make use of LayoutManager for that. Follow the below steps.

你得利用LayoutManager。遵循以下步骤。

1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager

1).首先,在您的活动/片段中声明LayoutManager。例如,我选择了LinearLayoutManager

private LinearLayoutManager mLinearLayoutManager;

2). Initialise the LinearLayoutManager and set that to your RecyclerView

2).初始化LinearLayoutManager,并将其设置为可回收视图

mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);

3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.

3).在onClick按钮上,这样做可以滚动到回收视图的底部。

mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.

Hope this will help..!!

希望这将帮助. . ! !

#2


4  

You can use scrollToPosition() with the index of the last position.

您可以使用scrollToPosition()和上一个位置的索引。

#1


24  

You have to make use of LayoutManager for that. Follow the below steps.

你得利用LayoutManager。遵循以下步骤。

1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager

1).首先,在您的活动/片段中声明LayoutManager。例如,我选择了LinearLayoutManager

private LinearLayoutManager mLinearLayoutManager;

2). Initialise the LinearLayoutManager and set that to your RecyclerView

2).初始化LinearLayoutManager,并将其设置为可回收视图

mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);

3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.

3).在onClick按钮上,这样做可以滚动到回收视图的底部。

mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.

Hope this will help..!!

希望这将帮助. . ! !

#2


4  

You can use scrollToPosition() with the index of the last position.

您可以使用scrollToPosition()和上一个位置的索引。