美文网首页
RecyclerView获取不可见的View(亲测有效)

RecyclerView获取不可见的View(亲测有效)

作者: 咚咚_Coding | 来源:发表于2022-01-13 20:34 被阅读0次

    获取屏幕内可见的View

    recyclerView.getChildAt(position)   
    recyclerView.layoutManager?.findViewByPosition(pos)  
    recyclerView.findViewHolderForLayoutPosition(position)?.itemView
    

    获取超出屏幕的不可见View

    1、在你的mAdapter 中定义一个map
    2、在你的mAdapter 中重写onViewDetachedFromWindow
    class ComplaintDataSubmitAdapter() :BaseAdapter<MultiItemEntity, BaseViewHolder>(null) {
    val viewHolderMap=ArrayMap<Int,View>()
    
    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        viewHolderMap[holder.adapterPosition] = holder.itemView
    }
    

    Use 得到屏幕外的View--> child

    for (index in 0 until mAdapter.data.size) {
            val item = mAdapter.data[index]
            var child= recycleView.layoutManager?.findViewByPosition(index)
            if (child==null&&mAdapter.viewHolderMap.isNotEmpty()&&mAdapter.viewHolderMap.size>index){
                child=mAdapter.viewHolderMap[index]
    }
    

    在Activity退出清空map

    override fun onDestroy() {
      if (mAdapter.viewHolderMap.isNotEmpty()){
          mAdapter.viewHolderMap.clear()
        }
        super.onDestroy()
    }

    相关文章

      网友评论

          本文标题:RecyclerView获取不可见的View(亲测有效)

          本文链接:https://www.haomeiwen.com/subject/eehocrtx.html