【问题标题】:How to put many recyclerview in a fragment如何将许多recyclerview放在一个片段中
【发布时间】:2020-03-20 18:04:57
【问题描述】:

希望您编码愉快!我一直在尝试向我的 HomeFragment 添加两个回收器视图,第二个(category_recyclerView)不加载任何数据,不知道为什么!我已尝试多次更改适配器并再次重写代码,但我没有看到错误,所以我需要您的帮助,谢谢!

recyclerViewCategory = (RecyclerView) view.findViewById(R.id.recycler_category);
    recyclerViewCategory.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager1;
    layoutManager1 = new LinearLayoutManager(getActivity());
    recyclerViewCategory.setLayoutManager(layoutManager1);

这个适配器

 FirebaseRecyclerOptions<Products> options1 = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(ProductsRef, Products.class)
            .build();
    FirebaseRecyclerAdapter<Products, CategoryViewHolder> adapter1 = new
            FirebaseRecyclerAdapter<Products, CategoryViewHolder>(options1) {
        @Override
        protected void onBindViewHolder(@NonNull CategoryViewHolder holder, int position, @NonNull Products model) {
                    Picasso.get().load(model.getImage()).into(holder.imageCircleCategory);
                    holder.categoryName.setText(model.getCategory());
        }

        @NonNull
        @Override
        public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_layout, parent, false);
            CategoryViewHolder holder = new CategoryViewHolder(view);
            return holder;
        }
    };
    recyclerViewCategory.setAdapter(adapter1);
    adapter1.startListening();

这是我的 XML recyclerview(两者)

 <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_category"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:orientation="horizontal"
    />
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_menu1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/recycler_category"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2"
    />

两者都在同一个片段中,第一个 Recycler_menu1 工作正常,但我不明白为什么另一个没有

这是 XML 的更新

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/category_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/categories"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@android:color/black"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</LinearLayout>
<LinearLayout
    android:id="@+id/category_linear2"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Products"
        android:layout_marginTop="20dp"
        android:layout_margin="10dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_menu1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:spanCount="2"
        />
</LinearLayout>

【问题讨论】:

  • 您是否尝试删除此行recyclerViewCategory.setHasFixedSize(true);
  • 听起来你在寻找视图类型

标签: java android firebase-realtime-database android-recyclerview


【解决方案1】:

在 XML 中,recycler_menu1 具有属性 android:layout_height="match_parent" 这可能会导致问题。另一个recyclerView可以隐藏在这个下。

【讨论】:

  • 在我将它们放入 LinyearLayout 之后,我已经成功地展示了这两个回收器,但是每当我在片段之间切换时,带有 layout_height wrap_content 的回收器就会消失,所以很奇怪,我正试图找到飞行的问题在 linyear 布局中包装内容和匹配父级之间,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2014-07-09
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多