java - 如何将许多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>
解决方案
在 XML 中,recycler_menu1 具有android:layout_height="match_parent"
可能导致问题的属性。另一个 recyclerView 可以隐藏在这个下面。
推荐阅读
- angular - 高级 Angular:提取 Ng-Content 子项
- django - djangochannelsrestframework websocket 连接失败,错误 500
- aws-api-gateway - 我可以在使用 Amplify 创建 Rest API 时跳过生成 {proxy+}
- javascript - 在 NEXT.JS 和 MongoDB 中只返回单个对象而不是数组
- swift - 不允许使用 Swift POST 请求方法
- zsh - 如何用 ZSH 脚本替换 config 或 toml 文件中的某些文本
- c - c语言中读取文件大小的线程(互斥问题)
- javascript - Three.js 对象在事件上的缓慢动画,同时保持当前位置
- apache-camel - 在路由中使用相同的 clientId 时 PAHO MQTT 5 抛出异常
- azure-webjobs - 使 Azure Webjob 计时器触发器不作为单例运行