android recyclerview mutliple items |删除类型1,插入类型2

azpvetkf  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(177)

假设您有一个recylcerview,其中包含两种类型的项,并且它被排序。让我们调用type1:ecgview和type2:buttons,然后对recyclerview进行排序(首先是所有ecgview,然后是所有按钮)。现在我要做的是,单击一个按钮,从recyclerview中删除它,然后插入一个ecgview。它适用于我以下代码,但我必须重写回收器适配器,因此我失去了用户当前滚动的点,并且没有notifyitemremoved()/notifyiteminserted方法中的动画,但我想保留它们!

class LeadSelectionAdapter(private val listener: ReportListener, private val avaidable: ArrayList<String>, private val ecgs: ArrayList<ECG>, private val ecgCollectionView: ECGCollectionView, private val ecgSelectionView: Fragment) : RecyclerView.Adapter<LeadSelectionAdapter.LeadSelectionViewHolder>() {

private var count: Int = 0

class LeadSelectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var textView: TextView? = null
    var ecgView: LeadView? = null
    var button: Button? = null

    init {
        if(itemView.tag == "ITEM"){
            textView = itemView.lead_selection_leadnumber
            ecgView = itemView.lead_selection_view
        }else{
            button = itemView.lead_selection_button
        }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeadSelectionViewHolder {
    val itemView: View
    if(count < ecgs.size) {
        itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_leads, parent, false)
        itemView.tag = "ITEM"
    }else{
        itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_adds, parent, false)
        itemView.tag = "ADD"
    }
    count += 1
    return LeadSelectionViewHolder(itemView)
}

override fun onBindViewHolder(holder: LeadSelectionViewHolder, position: Int) {
    if(position < ecgs.size) {
        val currentItem = ecgs[position]

        holder.textView?.text = currentItem.lead.toString()
        holder.ecgView?.setECG(ecgs[position])
        holder.lottie?.pauseAnimation()
        holder.itemView.setOnClickListener {
            ecgCollectionView.setLead(currentItem.lead)
            ecgSelectionView.onDestroy()
        }
    }else{
        val cButton = holder.button!!

        cButton.text = avaidable[position - ecgs.size]
        cButton.setOnClickListener {
            avaidable.remove(cButton.text.toString())
            notifyItemRemoved(position)

            ecgs.add(ECG(Lead.I, FloatArray(0)))
            notifyItemInserted(ecgs.size - 1)

            resetAdapter(avaidable, ecgs) //I just do recyclerview.adapter = LeadAdapter(avaidable, ecgs)
        }
    }

}

override fun getItemCount() : Int {
    return ecgs.size + avaidable.size
}

}
available是我的按钮列表,ecgs是我的ecgview列表。但当我点击我的按钮,我得到另一个按钮插入(当我离开适配器重置)。。。关于我如何保持我的动画或我可以节省适配器重置不知何故?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题