gpt4 book ai didi

android - 如何在recyclerview上隐藏最后单击的项目的 View

转载 作者:行者123 更新时间:2023-12-02 12:56:10 28 4
gpt4 key购买 nike

ViewHolder:

class HomeViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val songName: TextView = view.findViewById(R.id.txtNameSong)
val songArtist: TextView = view.findViewById(R.id.txtNameArtist)
val cardContent: CardView = view.findViewById(R.id.cardContent)
val pauseButton: ImageView = view.findViewById(R.id.pauseButton)
}
适配器:
override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
val text = songInfoList[position]
holder.songName.text = text[0]
holder.songArtist.text = text[1]
val path = text[2]
holder.cardContent.setOnClickListener {
playMusic(path)
holder.pauseButton.visibility = View.VISIBLE
}
holder.pauseButton.setOnClickListener {
if (mediaPlayer.isPlaying) {
holder.pauseButton.setImageResource(R.drawable.play)
mediaPlayer.pause()
} else {
holder.pauseButton.setImageResource(R.drawable.pause)
mediaPlayer.start()
}
}
}
每当用户单击recyclerView上的项目并播放音乐时,就会出现 pauseButton
我想在用户单击新项目时为上次单击的 pauseButton消失,并为新条目显示 pauseButton。但是,我无法做到这一点。告诉我该怎么做。
每当我在recyclerView上单击新内容时, pauseButton就会出现在最新项目上,而消失在前面的项目上。

最佳答案

您需要在RecyclerView.Adapter中保持状态
内部适配器:

private var playingPosition: Int? = null
当发生点击事件时:
//Update previously position
playingPosition?.let {
models.get(playingPosition).playingState = false
notifyItemChanged(playingPosition)
}
//update new position
models.get(position).playingState = true
playingPosition = position
notifyItemChanged(position)
另外,您的模型应保持播放状态:
data class Model(...: Int, ...: String, ..., playingState: Boolean)
在onBindViewHolder内部:
if (model.playingState) view.visibility = View.VISIBLE else view.visibility = VIEW.GONE

关于android - 如何在recyclerview上隐藏最后单击的项目的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62515236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com