JoKr
JoKr

Reputation: 5256

Make Recycler View show rows from bottom

I saw somewhere method to make RecyclerView show ViewHolders from bottom to top. Now, i can't find it anywhere (after half of hour going through RecyclerView, RecyclerAdapter, LayoutManager...).

Upvotes: 55

Views: 53634

Answers (8)

la niina
la niina

Reputation: 11

it took me hours upon hours to fix it on my side , basically you have to

Find the position of the message with the latest date and time before updating the adapter ,

then update the adapter after scroll to bottom . the Scroll will be instant just like in whatsapp etc

 val latestMessagePosition = liveMessagesData.size - 1
                    messagesAdaptor.setData(liveMessagesData)
                    if (latestMessagePosition >= 0) {
                        recyclerViewMessages.scrollToPosition(latestMessagePosition)
                    }

Upvotes: 0

Anter
Anter

Reputation: 3625

You can achieve this task by adding two lines to the xml code.

app:stackFromEnd="false"
app:reverseLayout="true"

This will work as all chat apps.

Upvotes: 22

AbhinayMe
AbhinayMe

Reputation: 2547

Here is the solution in Kotlin

val llm = LinearLayoutManager(this)
llm.stackFromEnd = true     // items gravity sticks to bottom
llm.reverseLayout = false   // item list sorting (new messages start from the bottom)

rv_chat_history.layoutManager = llm

Or if you like the apply method:

recycler.apply { 
    layoutManager = LinearLayoutManager(this).apply { 
        stackFromEnd = true
        reverseLayout = false
    }
}

Upvotes: 23

xFactor
xFactor

Reputation: 1

In my case, it was achieved by setting stackFromEnd = true, reverseLayout = false on the LinearLayoutManager and setting scrollToPosition(index_of_last_element)

recyclerView?.apply {
 layoutManager = LinearLayoutManager(context).apply {
   stackFromEnd = true
   reverseLayout = false
 }
}

recyclerView?.scrollToPosition(equations.size - 1)

View Animation

Upvotes: 0

Biplob Das
Biplob Das

Reputation: 3094

This is working for me in kotlin

recyclerView?.scrollToPosition(mArrayList.size - 1)

Upvotes: -1

Ankit Petkar
Ankit Petkar

Reputation: 1

Solution on your query:


LinearLayoutManager layoutManager=
                new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,true);
                layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager); 

In above code recyclerView is the id of RecyclerView and layoutManager is object of LinearLayoutManager.

Upvotes: 0

Ramy Sabry
Ramy Sabry

Reputation: 176

If you are using LinearLayoutManager make third param (reverseLayout) false

LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);

Upvotes: 6

AndroidEx
AndroidEx

Reputation: 15824

Is it LinearLayoutManager.setStackFromEnd(true) you are looking for?

Edit

Turns out LinearLayoutManager.setReverseLayout(true) does the trick. Either way, the reader may want to try each of the methods and the combination of both to get the needed effect.

Upvotes: 114

Related Questions