Java LinearLayoutManager setReverseLayout() == true 但项目从底部堆叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27727354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:55:29  来源:igfitidea点击:

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

javaandroidandroid-recyclerviewreverselayout-manager

提问by AndyRoid

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 setStackFromBottomto 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.

我试图设置setStackFromBottom为 false 但这没有做任何事情,反转项目顺序但仍然从顶部填充的最佳方法是什么?我应该改用自定义比较器类吗?我希望这比创建另一个类更容易。

采纳答案by petey

from the docs for setReverseLayout

来自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)

用于反转项目遍历和布局顺序。这与 RTL 视图的布局更改类似。当设置为 true 时,第一个项目在 UI 的末尾布局,第二个项目在它之前布局等等。对于水平布局,这取决于布局方向。设置为true时,如果RecyclerView为LTR,则从RTL布局,如果RecyclerView}为RTL,则从LTR布局。如果您正在寻找与 完全相同的行为setStackFromBottom(boolean),请使用setStackFromEnd(boolean)

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

因此,也尝试在 LinearLayoutManager 实例上使用setStackFromEnd(boolean)

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

回答by Reaz Murshed

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

接受的答案效果很好,当我遇到can not resolve method setReverseLayout错误时,我经历了困难才能使它起作用。

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

然后在寻找解决方案后,我发现那里有一个愚蠢的错误。我正在使用RecyclerView.LayoutManager而不是LinearLayoutManager.

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

所以我想消除这里的困惑我需要把它作为答案。

Do not use RecyclerView.LayoutManagerinstead of LinearLayoutManager

不要使用RecyclerView.LayoutManager代替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);

回答by Kirk_hehe

I found the effective method to solve this using xml:

我找到了使用 xml 解决此问题的有效方法:

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

回答by Teen Mutant Coder

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

我正在使用水平方向的 RecyclerView并且由于某些未知原因,当我尝试使用setReverseLayout(true).

but doing it via XML worked fine.

但是通过 XML 做它工作得很好。

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