0

How to reverse recyclerView items with CoordinatorLayout as root layout?

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_main_students"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        android:scrollbars="vertical"
         />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

and app:reverseLayout="true" not working for recyclerview.

Note: I want reverse recyclerView not reverese collection

hossein
  • 638
  • 1
  • 3
  • 14
  • This could help https://stackoverflow.com/questions/46168245/recyclerview-reverse-order – Zain Mar 10 '22 at 19:52

2 Answers2

1

solution 1- reverse list of input data

fun <T> reverseList(list: List<T>): ArrayList<T> {
return (list.indices)
    .map { i: Int -> list[list.size - 1 - i] }
    .toCollection(ArrayList())
}

follow- https://www.techiedelight.com/reverse-copy-list-kotlin/

Solution 2

LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLayoutManager);
RAMCODER
  • 61
  • 6
1

The best solution is to reverse the list. You can do this:

Collections.reverse(mList); // enter your list here

In your xml add this ->

app:stackFromEnd="true"

NOTE: if you are doing some operations with the adapter position and the list in the adapter, then first reverse and then pass it to the adapter. Or else you gotta face and issue

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38