Shift recyclerview focus to last element in the list
Asked Answered
C

10

26

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.

Corrales answered 16/12, 2015 at 8:23 Comment(3)
use recyclerView.smoothScrollToPosition(position); position is your index of last item inserted.Jackofalltrades
you can add this as the answer as it solved my problem.Corrales
@VipulAsri please put your comment as answer as it solved the question. You would help other guysCalibre
J
51

use recyclerView.smoothScrollToPosition(position);

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

Jackofalltrades answered 17/12, 2015 at 5:28 Comment(0)
L
18

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);
Lactary answered 16/12, 2015 at 8:37 Comment(1)
it populates the list from bottom. this was not something i was looking for. Vipul's answer solved my problem. BTW,thanks.Corrales
I
12

Use

recyclerView.smoothScrollToPosition(position)
adapter.notifyDataSetChanged();

where position is the index of last element

recyclerView.getAdapter().getItemCount()-1

use this to get last element.

This worked for me .

Idyllic answered 17/7, 2017 at 18:6 Comment(1)
Best and must detailed answer.Supportable
W
2

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);
Wildlife answered 14/9, 2019 at 20:25 Comment(0)
B
2

use layoutManager.setStackFromEnd(true); instead of this.

Sample

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    RecyclerView recyclerView = findViewById(R.id.list);
    layoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(layoutManager);
Birdbath answered 18/3, 2020 at 5:43 Comment(0)
C
1

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

recyclerView.smoothScrollToPosition(position);
Crucifix answered 9/3, 2017 at 12:59 Comment(0)
C
1

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

recyclerView.smoothScrollToPosition(recyclerView.getBottom());
Comprehensive answered 14/10, 2019 at 10:26 Comment(0)
C
0

use the below code in Kotlin

binding.recyclerViewMessage.smoothScrollToPosition(if (list.size > 1) list.size - 1 else list.size);

this is working perfectly

Clausius answered 1/2, 2023 at 12:10 Comment(0)
T
0

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

Transcribe answered 19/10, 2023 at 10:7 Comment(0)
R
-1

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" />
Ranjiv answered 8/3, 2019 at 4:20 Comment(1)
use layoutManager.setStackFromEnd(true); instead of this.Birdbath

© 2022 - 2024 — McMap. All rights reserved.