android RecyclerView嵌套 RecyclerView 子item 和 父item点击事件如何处理

·  阅读 889
android RecyclerView嵌套 RecyclerView 子item 和 父item点击事件如何处理

前言

经常会遇到 列表嵌套列表的场景,那么父item 和 子item 会有点击重叠该怎么处理尼?

先上效果

iShot2022-03-22 12.48.04.gif

父adapter

open class StoreListAdapter(layoutResId: Int, data: MutableList<StoreGoodsBean>): BaseQuickAdapter<StoreGoodsBean, BaseViewHolder>(layoutResId, data) {
    //子adapter
    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
    }
}
复制代码

子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.id.cartGoodsDes, item.description)
        holder.setText(R.id.cartGoodsPrice, "¥${item.price}")
    }

}
复制代码

listener

open interface OnChildItemChildClickListener {
    fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, parentPosition: Int, position: Int)
}
复制代码

页面上监听

//点击店铺前的checkbox
cartGoodsListAdapter.setOnItemChildClickListener  { _, view, position ->
    when (view.id) {
        R.id.storeCheckBox -> checkAllByStore(position)
    }
}
//点击店铺中商品前的checkbox
cartGoodsListAdapter.setOnChildItemChildClickListener(object : OnChildItemChildClickListener {
    override fun onItemChildClick(
        adapter: BaseQuickAdapter<*, *>,
        view: View,
        parentPosition: Int,
        position: Int,
    ) {
        when (view.id) {
            R.id.goodsCheckBox -> checkGoods(parentPosition, position)
        }
    }
})
复制代码
分类:
Android
标签:
收藏成功!
已添加到「」, 点击更改