preface
If a list is nested within a list, the parent item and the child item will overlap.
The first effect
Father adapter
open class StoreListAdapter(layoutResId: Int, data: MutableList<StoreGoodsBean>): BaseQuickAdapter<StoreGoodsBean, BaseViewHolder>(layoutResId, data) {// Subadapter private lateInit var goodsListAdapter: StoreGoodsListAdapter private var mOnChildItemChildClickListener: OnChildItemChildClickListener? = null override fun convert(holder: BaseViewHolder, item: StoreGoodsBean) { goodsListAdapter = StoreGoodsListAdapter(R.layout.fragment_cart_item_goods, arrayListOf()) goodsListAdapter.addChildClickViewIds(R.id.goodsCheckBox) goodsListAdapter.setOnItemChildClickListener { _, view, position -> var index = this.data.indexOfFirst { v -> v.storeCode == item.storeCode } this.setOnChildItemChildClick(view, index, position) } holder.setText(R.id.storeName, item.storeName) holder.getView<CheckBox>(R.id.storeCheckBox).isChecked = item.check!! holder.getView<RecyclerView>(R.id.goodsItemList).run { layoutManager = LinearLayoutManager(this.context) adapter = goodsListAdapter isNestedScrollingEnabled = false } goodsListAdapter.setList(item.goodsList) } protected open fun setOnChildItemChildClick(v: View, parent: Int, position: Int) { mOnChildItemChildClickListener?.onItemChildClick(this, v, parent, position) } open fun setOnChildItemChildClickListener(listener: OnChildItemChildClickListener) { this.mOnChildItemChildClickListener = listener } }Copy the code
The child adapter
class StoreGoodsListAdapter(layoutResId: Int, data: MutableList<CartGoodsBean>): BaseQuickAdapter<CartGoodsBean, BaseViewHolder>(layoutResId, data) { private var imageLoader: ImageLoader = CoilUtil.getImageLoader() override fun convert(holder: BaseViewHolder, item: CartGoodsBean) { holder.getView<ImageView>(R.id.cartGoodsImg).load(item.imgUrl, imageLoader ) { crossfade(true) placeholder(R.drawable.default_img) error(R.drawable.default_img) } holder.getView<CheckBox>(R.id.goodsCheckBox).isChecked = item.check!! Holder.settext (R.i.C.artGoodsdes, item.description) Holder.settext (R.I.C.artGoodspRice, "${item.price}")}}Copy the code
listener
open interface OnChildItemChildClickListener {
fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, parentPosition: Int, position: Int)
}
Copy the code
On-page listening
/ / click on the shop before the checkbox cartGoodsListAdapter. SetOnItemChildClickListener {_, the view, Position -> when (view.id) {r.id.storecheckBox -> checkAllByStore(position)} cartGoodsListAdapter.setOnChildItemChildClickListener(object : OnChildItemChildClickListener { override fun onItemChildClick( adapter: BaseQuickAdapter<*, *>, view: View, parentPosition: Int, position: Int, ) { when (view.id) { R.id.goodsCheckBox -> checkGoods(parentPosition, position) } } })Copy the code