Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从RecyclerView中删除项目时出现奇怪的动画_Android_Kotlin_Android Recyclerview - Fatal编程技术网

Android 从RecyclerView中删除项目时出现奇怪的动画

Android 从RecyclerView中删除项目时出现奇怪的动画,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我有一个由val microphones:MutableList填充的recyclerView。行布局中有一个按钮,用于从列表中删除该项,我已设法使其发挥作用,但动画出错。看起来,当recyclerView更新时,它会删除列表中的最后一个项目,然后删除正确的项目,然后删除项目下方的整个列表将动画设置为正确的状态 以下是recyclerView适配器的相关部分: class RecyclerViewAdapter( val microphones: MutableList<Micro

我有一个由
val microphones:MutableList
填充的recyclerView。行布局中有一个按钮,用于从列表中删除该项,我已设法使其发挥作用,但动画出错。看起来,当recyclerView更新时,它会删除列表中的最后一个项目,然后删除正确的项目,然后删除项目下方的整个列表将动画设置为正确的状态

以下是recyclerView适配器的相关部分:

class RecyclerViewAdapter(
    val microphones: MutableList<Microphone>
): RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val cellForRow = LayoutInflater.from(parent.context).inflate(R.layout.favorites_row, parent, false)
    return ViewHolder(cellForRow)
}

override fun getItemCount(): Int {
    return microphones.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val microphone = microphones[position]

    holder.bind(microphone)

    holder.favoritesButton.setOnClickListener {
        microphones.remove(microphone)
        notifyItemRemoved(position)
    }
}

class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

    val modelText = view.findViewById<TextView>(R.id.model_textView)
    val brandText = view.findViewById<TextView>(R.id.brand_textView)
    val favoritesButton = view.findViewById<ImageButton>(R.id.btnFavorite)

    fun bind(
        microphone: Microphone,
    ) {

        modelText.text = microphone.model
        brandText.text = microphone.brand
        }
    }
}
class RecycleServiceAdapter(
val话筒:可变列表
):RecyclerView.Adapter(){
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val cellForRow=LayoutInflater.from(parent.context)。充气(R.layout.favorites_行,parent,false)
返回视图保持架(cellForRow)
}
重写getItemCount():Int{
返回式话筒。尺寸
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val话筒=话筒[位置]
固定器(话筒)
holder.favoritesButton.setOnClickListener{
麦克风。卸下(麦克风)
已删除的项目(位置)
}
}
类ViewHolder(val视图:视图):RecyclerView.ViewHolder(视图){
val modelText=view.findviewbyd(R.id.model\u textView)
val brandText=view.findViewById(R.id.brand\u textView)
val-favoritesButton=view.findViewById(R.id.btnFavorite)
有趣的绑定(
麦克风:麦克风,
) {
modelText.text=麦克风.model
brandText.text=麦克风.brand
}
}
}
以下是该问题的屏幕记录:

我相信问题可能在于您是如何从麦克风上取下物品的,您能给我们看看吗?除了提供的代码之外,我不确定您的意思<代码>麦克风是我的
麦克风
对象的可变列表,它从父片段继承而来。我使用onClick监听器删除了行中
ImageButton
的相应条目,并从onBindViewHolder的列表中删除了该条目。还有什么其他信息会有帮助?