47

This seems like it would be an easy solution, but it seems that setting

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;

.... // More code

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    // Add item decoration
    mRecyclerView.addItemDecoration(new SpacesItemDecoration(DIVIDER_SPACE));

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setReverseLayout(true); // THIS ALSO SETS setStackFromBottom to true
    mRecyclerView.setLayoutManager(mLayoutManager);

Seems to also set the items to stack from the bottom

I tried to set setStackFromBottom to false but that didn't do anything, what would be the best way to reverse the items order but still populate from the top? Should I use a Custom Comparator class instead? I was hoping this would be an easier process than creating another class.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
AndyRoid
  • 5,062
  • 8
  • 38
  • 73

5 Answers5

126

from the docs for setReverseLayout

Used to reverse item traversal and layout order. This behaves similar to the layout change for RTL views. When set to true, first item is laid out at the end of the UI, second item is laid out before it etc. For horizontal layouts, it depends on the layout direction. When set to true, If RecyclerView is LTR, than it will layout from RTL, if RecyclerView} is RTL, it will layout from LTR. If you are looking for the exact same behavior of setStackFromBottom(boolean), use setStackFromEnd(boolean)

So, try also using setStackFromEnd(boolean) on your LinearLayoutManager instance,

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
petey
  • 16,914
  • 6
  • 65
  • 97
  • Good job, that WAS easy :) – AndyRoid Jan 01 '15 at 00:11
  • Thanks, tried both methods separately, didn't even have a thought about using them together to get the result :) – AndroidEx Mar 18 '15 at 16:24
  • 1
    I'm getting `can not resolve method setReverseLayout` error. – Reaz Murshed May 23 '16 at 19:27
  • @ReazMurshed check your sdk version (min and target) and dependencies (recycler view support lib) used in your project. – petey May 24 '16 at 19:21
  • 4
    Thanks for the comment. My problem was a silly one. I was using `RecyclerView.LayoutManager` instead of `LinearLayoutManager`. – Reaz Murshed May 24 '16 at 19:23
  • This is good, but it doesn't scroll to end to show newest items – Elliptica May 03 '18 at 15:10
  • @Elliptica why would it? When calling `stackfromEnd(true)`, the list fills its content starting from the bottom of the view. If you would like to move the list position to the end of the list, there are ways to do that and has been questioned and answered in other posts. – petey May 03 '18 at 15:16
  • @petey @ReazMurshed if both `setStackFromEnd()` and `setReverseLayout()` are doing the same job then why use both of them? Is there any other reason to call both of these methods together? – Neeraj Sewani Jun 30 '18 at 19:35
  • after setting setStackFromEnd = true, setReverseLayout behavior stop working. Please help. – Shamsul Jul 05 '19 at 06:33
31

The accepted answer works well and I went through a hardship to make it work as I was getting can not resolve method setReverseLayout error.

Then after searching for solutions I found there was a silly mistake out there. I was using RecyclerView.LayoutManager instead of LinearLayoutManager.

So I thought to remove the confusions around here I need to put it as an answer.

Do not use RecyclerView.LayoutManager instead of LinearLayoutManager

// Declare your RecyclerView and the LinearLayoutManager like this 
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;

...

// Now set the properties of the LinearLayoutManager 
mLayoutManager = new LinearLayoutManager(MainActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

// And now set it to the RecyclerView
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(yourAdapter);
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 1
    is there any drawback in using LinearLayoutManager instead of RecyclerView.LayoutManager? – c-a Sep 23 '18 at 12:59
  • A `LinearLayoutManager` is a `RecyclerView.LayoutManager`. You don't use `RecyclerView.LayoutManager` directly unless you're making your own implementation of it. – afollestad Jan 10 '22 at 20:30
2

I found the effective method to solve this using xml:

app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:stackFromEnd="true"
Kirk_hehe
  • 449
  • 7
  • 17
0

I'm using a RecyclerView with horizontal orientation and for some unknown reason(s) the accepted answer didn't work for me when i tried to reverse the layout with setReverseLayout(true).

but doing it via XML worked fine.

                <androidx.recyclerview.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/recycler_view" 
                    android:orientation="horizontal"
                    app:reverseLayout="true"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
El Nuru
  • 106
  • 1
  • 6
0

I had this error, i work put in false

recyclerView = vista.findViewById(R.id.recyclerView);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setReverseLayout(false);
    mLayoutManager.setStackFromEnd(false);
    recyclerView.setLayoutManager(mLayoutManager);

For search in my app, search for name

Query busqueda = databaseReference.child("Productos").orderByChild("pTitle").startAt(txtSearch.getText().toString());
            busqueda.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    postModelList.clear();
                    for(DataSnapshot ds: dataSnapshot.getChildren()){
                        PostModel postModel = ds.getValue(PostModel.class);
                        postModelList.add(postModel);
                        postAdapter = new PostAdapter(getContext(), postModelList);
                        recyclerView.setAdapter(postAdapter);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
Roel Leal
  • 49
  • 2