LinearLayoutManager setReverseLayout() == true but items stack from bottom

杀马特。学长 韩版系。学妹 提交于 2019-11-26 09:01:31

问题


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.


回答1:


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



回答2:


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



回答3:


I found the effective method to solve this using xml:

app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:stackFromEnd="true"



回答4:


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"/>


来源:https://stackoverflow.com/questions/27727354/linearlayoutmanager-setreverselayout-true-but-items-stack-from-bottom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!