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

Posted 嘴巴吃糖了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android RecyclerView嵌套 RecyclerView 子item 和 父item点击事件如何处理相关的知识,希望对你有一定的参考价值。

前言

经常会遇到 列表嵌套列表的场景,那么父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)
        
    
) 

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

需要的小伙伴直接点击文末小卡片免费领取哦,以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持,需要的自己领取)

Android学习PDF+架构视频+面试文档+源码笔记

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT大厂面试题(有解析)

领取地址:

以上是关于android RecyclerView嵌套 RecyclerView 子item 和 父item点击事件如何处理的主要内容,如果未能解决你的问题,请参考以下文章

嵌套在 BottomSheet 中的 RecyclerView(水平)防止垂直滚动

android中RecyclerView嵌套RecyclerView中,如何为内层RecyclerView设值的问题

Android番外篇 NestedScrollView嵌套RecyclerView

android recyclerView嵌套progressbar

android中RecyclerView嵌套问题中,内层RecyclerView区域无法响应Item点击事件

Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决