Nested RecyclerView需点击Search View才加载的问题求助
问题分析与解决方案
核心问题:异步数据加载后未通知Adapter更新
Firestore数据查询是异步操作,当数据加载完成并添加到prdModellist后,你没有通知ProductAdapter数据发生变化,导致子RecyclerView无法自动刷新。只有当界面因点击SearchView发生重绘时,才会触发RecyclerView重新绑定,此时数据已加载完成,才会显示出来。
具体修复步骤
1. 移除HomeFragment中的冗余代码
在onCreateView方法里,创建Adapter后执行的parentModelArrayList = new ArrayList<>();完全多余,还可能引发后续数据混乱,直接删除:
// 删除此行代码 parentModelArrayList = new ArrayList<>();
2. 子Adapter数据更新通知
在Firestore查询的onComplete回调中,添加完数据后立即调用productAdapter.notifyDataSetChanged(),通知子RecyclerView刷新数据:
修复「Tasty Snacks」查询回调:
if (task.isSuccessful()) { for (QueryDocumentSnapshot queryDocumentSnapshot: task.getResult()) { prdModellist.add(new ProductDataModel( (String) queryDocumentSnapshot.get("productimage"), (String) queryDocumentSnapshot.get("productname"), (String) queryDocumentSnapshot.get("description"), (Long) queryDocumentSnapshot.get("price"), (Long) queryDocumentSnapshot.get("discount") )); } // 通知子Adapter更新数据 productAdapter.notifyDataSetChanged(); }
修复「Personal Care」查询回调(同时修正拼写错误):
if (task.isSuccessful()){ // 修正:将Task.getResult()改为task.getResult() for (QueryDocumentSnapshot queryDocumentSnapshot: task.getResult()) { prdModellist.add(new ProductDataModel( (String) queryDocumentSnapshot.get("productimage"), (String) queryDocumentSnapshot.get("productname"), (String) queryDocumentSnapshot.get("description"), (Long) queryDocumentSnapshot.get("price"), (Long) queryDocumentSnapshot.get("discount") )); } // 通知子Adapter更新数据 productAdapter.notifyDataSetChanged(); }
3. 优化子RecyclerView初始化(可选但推荐)
每次onBindViewHolder都创建新的LinearLayoutManager和ProductAdapter会浪费性能,建议在ViewHolder初始化时一次性设置:
public class ViewHolder extends RecyclerView.ViewHolder { TextView title; MaterialButton viewAllBtn; RecyclerView prntRView; ProductAdapter productAdapter; ArrayList<ProductDataModel> prdModellist; public ViewHolder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.prntTitle); viewAllBtn = itemView.findViewById(R.id.viewAllBtn); prntRView = itemView.findViewById(R.id.prntRView); // 仅初始化一次子RecyclerView的布局管理器 RecyclerView.LayoutManager lm = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false); prntRView.setLayoutManager(lm); // 初始化数据列表和Adapter prdModellist = new ArrayList<>(); productAdapter = new ProductAdapter(itemView.getContext(), prdModellist); prntRView.setAdapter(productAdapter); } } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { ParentModel currentItem = parentModelList.get(position); holder.title.setText(currentItem.categoryText()); holder.viewAllBtn.setOnClickListener(v -> showLanding(currentItem.categoryText())); // 清空旧数据,避免复用ViewHolder时数据残留 holder.prdModellist.clear(); holder.productAdapter.notifyDataSetChanged(); FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance(); if (currentItem.categoryText().equals("Tasty Snacks")) { firebaseFirestore.collection(COLLECTION_NAME).whereEqualTo(CONDITION).get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { for (QueryDocumentSnapshot snapshot : task.getResult()) { holder.prdModellist.add(new ProductDataModel( (String) snapshot.get("productimage"), (String) snapshot.get("productname"), (String) snapshot.get("description"), (Long) snapshot.get("price"), (Long) snapshot.get("discount") )); } holder.productAdapter.notifyDataSetChanged(); } else { System.out.println(task.getException().getMessage()); } }); } else if (currentItem.categoryText().equals("Personal Care")) { firebaseFirestore.collection(COLLECTION).whereEqualTo(CONDITION2).get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { for (QueryDocumentSnapshot snapshot : task.getResult()) { holder.prdModellist.add(new ProductDataModel( (String) snapshot.get("productimage"), (String) snapshot.get("productname"), (String) snapshot.get("description"), (Long) snapshot.get("price"), (Long) snapshot.get("discount") )); } holder.productAdapter.notifyDataSetChanged(); } else { System.out.println(task.getException().getMessage()); } }); } }
额外注意事项
- 确保
COLLECTION_NAME、CONDITION等常量已正确定义,否则Firestore查询会失败。 - 可添加加载状态提示(如ProgressBar),提升用户体验。
- 建议提前在Fragment/ViewModel中加载所有分类数据,再传递给Adapter,减少重复查询,优化性能。
内容的提问来源于stack exchange,提问作者Vinay N
相关产品推荐
相关产品推荐

