RecyclerView 内部的 RecyclerView 显示错误数据
RecyclerView inside RecyclerView displaying wrong data
我有一个适配器,其中包含另一个 recyclerview,当我将数据加载到该适配器时,我也为内部适配器加载数据,它已填充但未正确填充,当我滚动时它填充了项目(也错误地)但是当膨胀时不
class OrdersAdapter(private val context: Context) :
RecyclerView.Adapter<BaseViewHolder<*>>() {
private val innerAdapter by lazy { OrdersInnerAdapter(context) }
...
inner class MainViewHolder(itemView: View) : BaseViewHolder<Order>(itemView) {
override fun bind(item: Order, position: Int) {
itemView.user_name.text = item.userName
itemView.rv_order_items.layoutManager = LinearLayoutManager(context)
itemView.rv_order_items.adapter = innerAdapter
innerAdapter.setItems(item.cartList)
}
现在这是我的内部适配器
class OrdersInnerAdapter(val context: Context):RecyclerView.Adapter<BaseViewHolder<*>>(){
private var cartList = mutableListOf<Cart>()
fun setItems(cartList:MutableList<Cart>){
this.cartList = cartList
notifyDataSetChanged()
}
....
inner class OrdersInnerViewHolder(itemView: View):BaseViewHolder<Cart>(itemView){
override fun bind(item: Cart, position: Int) {
itemView.item_name.text = item.productName
itemView.item_qty.text = "Quantity: ${item.qty}"
itemView.item_price.text = "$${item.price.times(item.qty)}.00"
}
}
这是我的用例
但是内部适配器中的项目加载错误,知道吗?
class OrdersAdapter(private val context: Context) :
RecyclerView.Adapter<BaseViewHolder<*>>() {
inner class MainViewHolder(itemView: View) : BaseViewHolder<Order>(itemView) {
override fun bind(item: Order, position: Int) {
itemView.user_name.text = item.userName
itemView.rv_order_items.layoutManager = LinearLayoutManager(context)
val innerAdapter = OrdersInnerAdapter(context)
itemView.rv_order_items.adapter = innerAdapter
innerAdapter.setItems(item.cartList)
}
而且 OrdersInnerAdapter
的 setItems
中不需要 notifyDataSetChanged()
。它将是
fun setItems(cartList:MutableList<Cart>){
this.cartList = cartList // You can also move this to constructor to get rid of extra method.
}
我有一个适配器,其中包含另一个 recyclerview,当我将数据加载到该适配器时,我也为内部适配器加载数据,它已填充但未正确填充,当我滚动时它填充了项目(也错误地)但是当膨胀时不
class OrdersAdapter(private val context: Context) :
RecyclerView.Adapter<BaseViewHolder<*>>() {
private val innerAdapter by lazy { OrdersInnerAdapter(context) }
...
inner class MainViewHolder(itemView: View) : BaseViewHolder<Order>(itemView) {
override fun bind(item: Order, position: Int) {
itemView.user_name.text = item.userName
itemView.rv_order_items.layoutManager = LinearLayoutManager(context)
itemView.rv_order_items.adapter = innerAdapter
innerAdapter.setItems(item.cartList)
}
现在这是我的内部适配器
class OrdersInnerAdapter(val context: Context):RecyclerView.Adapter<BaseViewHolder<*>>(){
private var cartList = mutableListOf<Cart>()
fun setItems(cartList:MutableList<Cart>){
this.cartList = cartList
notifyDataSetChanged()
}
....
inner class OrdersInnerViewHolder(itemView: View):BaseViewHolder<Cart>(itemView){
override fun bind(item: Cart, position: Int) {
itemView.item_name.text = item.productName
itemView.item_qty.text = "Quantity: ${item.qty}"
itemView.item_price.text = "$${item.price.times(item.qty)}.00"
}
}
这是我的用例
但是内部适配器中的项目加载错误,知道吗?
class OrdersAdapter(private val context: Context) :
RecyclerView.Adapter<BaseViewHolder<*>>() {
inner class MainViewHolder(itemView: View) : BaseViewHolder<Order>(itemView) {
override fun bind(item: Order, position: Int) {
itemView.user_name.text = item.userName
itemView.rv_order_items.layoutManager = LinearLayoutManager(context)
val innerAdapter = OrdersInnerAdapter(context)
itemView.rv_order_items.adapter = innerAdapter
innerAdapter.setItems(item.cartList)
}
而且 OrdersInnerAdapter
的 setItems
中不需要 notifyDataSetChanged()
。它将是
fun setItems(cartList:MutableList<Cart>){
this.cartList = cartList // You can also move this to constructor to get rid of extra method.
}