11

To enable chat-style scrolling in a List View, we can use the following properties:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ....
    android:stackFromBottom="true"
    android:transcriptMode="normal" />

It is a simple and efficient way to create a chat. How can we do the same thing with a recycler view ? I did not find any simple solution.

Regards,

insane.bot
  • 193
  • 1
  • 2
  • 11

2 Answers2

19

RecyclerView has a stackFromEnd attribute.

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView" 
        android.support.v7.recyclerview:stackFromEnd ="true"/>

Or you can do it through code

mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • Yes. Thanks.It worked. This is the equivalent to stackFromBottom. But which property has the similiar effect of android:transcriptMode ? It guarantees that the listview only scrolls down if the user is looking to the last element of data set. – insane.bot Jun 04 '15 at 04:46
  • I think similar effect to transcriptMode can be achieved through code by using `setReverseLayout()`. Check out my edit. – Bidhan Jun 04 '15 at 04:49
  • 1
    Ok. Thank you. I will test it and will post here if it worked or not. – insane.bot Jun 04 '15 at 04:55
  • Only added `StackFromEnd` did the trick. If you reverse layout, you need to reverse your queries and everything – Pierre Jun 18 '19 at 06:30
  • Thanks a lot. Didn't know the "setStackFromEnd" this whole time. – thilina Kj May 28 '20 at 08:28
2

add these statements;

<android.support.v7.widget.RecyclerView
                    android:id="@+id/chat_list_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="false"
                    android:listSelector="@android:color/transparent"
                    android:paddingLeft="4dp"
                    android:paddingRight="4dp"
                    android:scrollbarStyle="outsideOverlay"
                    android:transcriptMode="normal" />

and add into layout manager

layoutManager.setStackFromEnd(true);
Abdul Rizwan
  • 3,904
  • 32
  • 31