首页 > 解决方案 > 如何将许多recyclerview放在一个片段中

问题描述

希望你编码愉快!我一直在尝试向我的 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>

标签: javaandroidfirebase-realtime-databaseandroid-recyclerview

解决方案


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


推荐阅读