【问题标题】:In Recycler View, converting from a LinearLayoutManger to GridLayoutManager makes no difference在 Recycler View 中,从 LinearLayoutManger 转换为 GridLayoutManager 没有区别
【发布时间】:2020-10-16 15:02:26
【问题描述】:

我有一个使用 recyclerView 和 LinearLayoutManager 的 Android 应用程序。我的印象是,将 LinearLayoutManager 更改为 GridLayoutManager(参见下面的注释行和下一行)只会给我一个网格。但是没有任何变化,我仍然看到线性列表,每行一项,一列。

    recyclerView = view.findViewById(R.id.item_setup_recycler_view);
    recyclerView.setHasFixedSize(true);
    //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    adapter = new ItemSetupRecyclerViewAdapter(getActivity(), kids);
    recyclerView.setAdapter(adapter);

在我看到网格之前,如果还有其他需要更改的地方,有人可以告诉我吗?

这是recyclerView的xml:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/feed_recycle_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:clipToPadding="false"
    android:paddingBottom="74dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView8"
    app:layout_constraintVertical_bias="0.0">

</androidx.recyclerview.widget.RecyclerView>

提前致谢

【问题讨论】:

  • 看看这个,可能你少了一步:stackoverflow.com/questions/40587168/…
  • 你能分享你的xml吗?
  • @ChintanSoni 我刚刚做了。谢谢
  • 看来你是为 item_setup_recycler_view 设置的,而不是你发布 feed_recycle_view 的 XML

标签: android android-recyclerview gridlayoutmanager


【解决方案1】:

这样使用。 在你的 .xml

 <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

在您的代码中。

GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);

gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

然后把它交给你的 recyclerView。

或者你可以用一行来做:

recyclerView.setLayoutManager( new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);)

【讨论】:

    【解决方案2】:

    您需要添加列表的方向和方向

    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2, RecyclerView.VERTICAL, false));
    

    【讨论】:

      【解决方案3】:

      感谢@Công Hải 的评论,我意识到我正在做的改变是唯一需要的改变。问题是我在我的应用程序中对错误的 recycleView 进行了更改。一旦我修改了正确的 recycleView,一切都按预期工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-12
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-31
        • 1970-01-01
        相关资源
        最近更新 更多