【问题标题】:LinearLayoutManager setReverseLayout() == true but items stack from bottomLinearLayoutManager setReverseLayout() == true 但项目从底部堆叠
【发布时间】:2015-02-27 21:51:53
【问题描述】:

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

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

似乎也将物品设置为从底部堆叠

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

【问题讨论】:

    标签: java android android-recyclerview reverse layout-manager


    【解决方案1】:

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

    【讨论】:

    • 干得好,这很容易:)
    • 谢谢,分别尝试了这两种方法,甚至没有想过将它们一起使用以获得结果:)
    • 我收到can not resolve method setReverseLayout 错误。
    • @ReazMurshed 检查项目中使用的 sdk 版本(最低和目标)和依赖项(回收站视图支持库)。
    • 感谢您的评论。我的问题很愚蠢。我使用的是RecyclerView.LayoutManager 而不是LinearLayoutManager
    【解决方案2】:

    接受的答案效果很好,当我收到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;
    

    ...

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

    【讨论】:

    • 使用 LinearLayoutManager 代替 RecyclerView.LayoutManager 有什么缺点吗?
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      我正在使用具有 horizo​​ntal 方向的 RecyclerView,但由于某些未知原因,当我尝试使用 setReverseLayout(true) 反转布局时,接受的答案对我不起作用。

      但是通过 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"/>
      

      【讨论】:

        【解决方案5】:

        我遇到了这个错误,我的工作是假的

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

        要在我的应用中搜索,请搜索名称

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

        【讨论】:

          猜你喜欢
          • 2014-08-23
          • 1970-01-01
          • 1970-01-01
          • 2021-10-12
          • 2018-11-25
          • 1970-01-01
          • 2015-10-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多