You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android嵌套RecyclerView中子适配器全部数据集中展示在单个条目问题

问题原因

  • 父适配器history_adapter中将子RecyclerView的布局管理器、子数据列表、子适配器都声明为类全局成员,每次执行onBindViewHolder绑定父条目时,全局变量会被新的值覆盖,导致前序父条目的子列表引用被后续条目替换,所有子数据最终只会注入到最后一个绑定的父条目中。
  • 子数据的Firestore查询路径写死了固定订单ID OID1945395f651948f186,没有使用当前父条目对应的订单ID,导致所有父条目查询的是同一个订单的子数据。

修复方案

修改history_adapter.kt的onBindViewHolder逻辑,核心改动如下:

  1. 将子列表相关变量改为方法内局部变量,避免全局覆盖
  2. 修正Firestore查询路径,使用当前父条目对应的order_id
  3. 每次绑定新子数据前清空列表,避免复用错乱

修改后核心代码

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val orderId_TA : orderID_Am_ST = oidta[position]
    holder.slot_time_history.text = orderId_TA.slot_time
    holder.delivery_address_history.text = orderId_TA.deliver_address
    holder.totalamount_history.text = orderId_TA.total_ammount
    holder.order_ID_history.text = orderId_TA.order_id
    holder.order_date_history.text = orderId_TA.order_date
    holder.status_order.text = orderId_TA.status

    // 子列表相关变量改为局部变量,不要用类全局成员
    val layoutManager = LinearLayoutManager(context)
    holder.recyclerView.layoutManager = layoutManager
    holder.recyclerView.setHasFixedSize(true)

    val history_product = arrayListOf<Add_to_FireStore>()
    val history_product_Adapter = history_product_adapter(history_product, context)
    holder.recyclerView.adapter = history_product_Adapter
    holder.recyclerView.setRecycledViewPool(viewPool)

    db = FirebaseFirestore.getInstance()

    db.collection("customer/user/profile").document("${Fuser?.uid}").get().addOnCompleteListener {
        if(it.isSuccessful){
            val shop_name = it.result["shopname"]
            holder.shop_name_history.text = shop_name.toString()
            holder.phone_number_history.text = Fuser?.phoneNumber.toString()
        }
    }

    // 修正查询路径:替换固定订单ID为当前条目的order_id
    db.collection("Order Details1/${Fuser?.uid}/OrderID/${orderId_TA.order_id}/order").addSnapshotListener(object :
        EventListener<QuerySnapshot> {
        override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
            if(error != null){
                Log.e("Firestore error",error.message.toString())
                return
            }
            // 每次更新数据前先清空旧数据,避免复用条目时数据叠加
            history_product.clear()
            for(doc: DocumentChange in value?.documentChanges!!){
                if(doc.type == DocumentChange.Type.ADDED){
                    history_product.add(doc.document.toObject(Add_to_FireStore::class.java))
                }
            }
            history_product_Adapter.notifyDataSetChanged()
        }
    })

    // 剩余原有取消订单等逻辑保持不变即可
}

额外优化建议

  • 给Firestore的SnapshotListener设置生命周期绑定,在父条目被回收、页面销毁时解绑监听器,避免内存泄漏和无效回调。
  • 可以将子RecyclerView的LayoutManager初始化放到ViewHolder创建阶段,减少绑定过程中的重复创建开销。

内容的提问来源于stack exchange,提问作者Roheet Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:45:05