【问题标题】:Multiple recycler views with different layouts具有不同布局的多个回收站视图
【发布时间】:2019-10-19 19:50:40
【问题描述】:

我在动态创建具有不同布局的 recyclerView 时遇到问题。我想让一些对象处于水平方向并保持垂直。两者都是recyclerView。 下面的代码,我有什么:

parentLayout = new RelativeLayout(getContext());

RelativeLayout.LayoutParams layoutParams = new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
RelativeLayout.LayoutParams.MATCH_PARENT);

for (Map.Entry<Integer, ArrayList<Object>> entry : items.entrySet()) {
adapter = new CustomAdapter(getContext(), entry.getValue())
View view = inflater.inflate(R.layout.recycler, container, false);
if (view instanceof RecyclerView) {
      recyclerView = (RecyclerView) view;
      CustomLayout cl = new CustomLayout();
      LinearLayoutManager lm = new LinearLayoutManager(getContext());
      recyclerView.setAdapter(adapter);
      recyclerView.setItemAnimator(null);
      recyclerView.setLayoutManager(ifSomething ? cl : lm);
    }
   parentLayout.addView(view, layoutParams);
}

此时,我收到多个recycleView重叠,我只想将它们添加到另一个之下。

【问题讨论】:

  • 如果你只想要一个低于前一个,使用垂直线性布局而不是相对布局
  • 我需要创建带有 wrap_content 高度的 recyclerView,它只适用于相对布局

标签: java android layout android-recyclerview android-relativelayout


【解决方案1】:

嗯,我不认为你想实现多个 recyclerviews。您只需要实现一个recyclerview 并创建2 个viewholders 一个具有水平线性布局和一个具有垂直线性布局。将适配器添加到您的 recyclerview 中,该适配器管理列表中的项目并将它们转换为您的 recyclerview 的正确视图。 Recyclerviews 仅用于显示包含多个项目的列表。并不是真的要显示为多个视图。

请按照此链接中的步骤操作:https://developer.android.com/guide/topics/ui/layout/recyclerview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多