Home > Back-end >  How to reverse recyclerView items with CoordinatorLayout as root layout?
How to reverse recyclerView items with CoordinatorLayout as root layout?

Time:03-11

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.

CodePudding user response:

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);

CodePudding user response:

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

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

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

  • Related