Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set setStackFromEnd(true); in GridLayoutManager for Recyclerview?

I want Grid layout that loads data from last for that I used following code

GridLayoutManager recyclerViewLayoutManager = new GridLayoutManager(getActivity(), 3);
//       LinearLayoutManager recyclerViewLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewLayoutManager.setStackFromEnd(true);
lv_explore.setLayoutManager(recyclerViewLayoutManager);

it gives me error like this

java.lang.UnsupportedOperationException: GridLayoutManager does not support stack from end. Consider using reverse layout
at android.support.v7.widget.GridLayoutManager.setStackFromEnd(GridLayoutManager.java:107)
at in.co.ans.jalsounique.FragmentExplore.onCreateView(FragmentExplore.java:86)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

when I am using LinearLayoutManager it is working but I need grid view, please advise what should be better way?

like image 920
Jignesh Ansodariya Avatar asked Nov 08 '22 07:11

Jignesh Ansodariya


1 Answers

It's just what it says: setStackFromEnd is not supported. It's only for lists, not grids.

Consider using reverse layout

So the way you work around this is to call setReverseLayout(true) on your GridLayoutManager.

This means you will need to reverse the entire order of your list. If you are appending items meant to show up at the bottom, you will have to insert them at the beginning instead.

like image 127
kris larson Avatar answered Nov 15 '22 05:11

kris larson