【发布时间】: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