前言
经常会遇到 列表嵌套列表的场景,那么父item 和 子item 会有点击重叠该怎么处理尼?
先上效果
父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)
}
}
})
标签:Int,RecyclerView,item,position,android,holder,id,view From: https://blog.51cto.com/u_16163453/6534414