Shift recyclerview focus to last element in the li

2020-06-30 05:07发布

I am using recyclerview to display messages in a chat window. However, when a new message is being edited, the recycler view focus should be on the last element. I expect there would be some way to shift the focus of a recyclerview to last element instead of first.

8条回答
Rolldiameter
2楼-- · 2020-06-30 05:37

use recyclerView.smoothScrollToPosition(position);

position is your index of last item inserted. This will move your the RecyclerView focus on the last element.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-06-30 05:40

You can use app:stackFromEnd="true" in your recycler view XML code like:

 <android.support.v7.widget.RecyclerView
  android:id="@+id/message_list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_above="@id/message_input"
  android:layout_alignParentTop="true"
  app:stackFromEnd="true" />
查看更多
何必那么认真
4楼-- · 2020-06-30 05:46

Use can add any of the two lines on your LinearLayoutManager object to scroll your recyclerview to the bottom ...

llm.setStackFromEnd(true);

OR

llm.setReverseLayout(true);
查看更多
倾城 Initia
5楼-- · 2020-06-30 05:54

You can simply use below code. Here recyclerView.getBottom() defines the position to be focus

recyclerView.smoothScrollToPosition(recyclerView.getBottom());
查看更多
老娘就宠你
6楼-- · 2020-06-30 05:55

There are 2 ways by which you can do.

1: When you are creating any LayoutManager for the RecyclerView, Make the last parameter in its constructor as true.

LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true);

2: Simply tell the RecyclerView to scroll to the last position.

recyclerView.smoothScrollToPosition(lastPosition);
查看更多
地球回转人心会变
7楼-- · 2020-06-30 06:00

Make Sure This position is the position of the item which you want to view.

recyclerView.smoothScrollToPosition(position);
查看更多
登录 后发表回答