skip to Main Content

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

2

Answers


  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);
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search