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

And*_*oid 39 java reverse android layout-manager android-recyclerview

这似乎是一个简单的解决方案,但似乎设置

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);
Run Code Online (Sandbox Code Playgroud)

似乎还要从底部设置要堆叠的项目

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

pet*_*tey 113

来自setReverseLayout的文档

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

所以,尝试在LinearLayoutManager实例上使用setStackFromEnd(boolean),

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的评论.我的问题很愚蠢.我使用`RecyclerView.LayoutManager`而不是`LinearLayoutManager`. (4认同)

Rea*_*hed 29

接受的答案很有效,我遇到了困难,因为我遇到了can not resolve method setReverseLayout错误.

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

所以我想要消除这里的混乱,我需要把它作为一个答案.

不要使用RecyclerView.LayoutManager而不是LinearLayoutManager

// Declare your RecyclerView and the LinearLayoutManager like this 
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
Run Code Online (Sandbox Code Playgroud)

...

// 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);
Run Code Online (Sandbox Code Playgroud)