如何隐藏 recyclerview 上最后单击项目的视图
How to hide the view of last clicked item on the recyclerview
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
目前的功能只适用于recyclerview 的一项,所以你不能在这里做。
如果你想这样做,你可以这样做。
您可以将 isPlay
值添加到 SongInfoModel,当点击播放时,您可以为除当前播放歌曲之外的所有歌曲设置 isPlay = false
。
所以代码将是这样的:
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
}
if(songInfoList[position].getIsPlaying){
holder.pauseButton.setVisibility(View.VISIBLE)
}else{
holder.pauseButton.setVisibility(View.GONE)
}
holder.pauseButton.setOnClickListener {
if (mediaPlayer.isPlaying) {
holder.pauseButton.setImageResource(R.drawable.play)
mediaPlayer.pause()
} else {
holder.pauseButton.setImageResource(R.drawable.pause)
mediaPlayer.start()
}
setPlayingSong(position)
}
}
public void setPlayingSong(int pos){
for (int i = 0; i < songInfoList.size; i++){
songInfoList.get(i).setIsPlaying(false);
}
songInfoList.get(pos).setIsPlaying(true);
notifyDataSetChanged();
}
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
目前的功能只适用于recyclerview 的一项,所以你不能在这里做。
如果你想这样做,你可以这样做。
您可以将 isPlay
值添加到 SongInfoModel,当点击播放时,您可以为除当前播放歌曲之外的所有歌曲设置 isPlay = false
。
所以代码将是这样的:
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
}
if(songInfoList[position].getIsPlaying){
holder.pauseButton.setVisibility(View.VISIBLE)
}else{
holder.pauseButton.setVisibility(View.GONE)
}
holder.pauseButton.setOnClickListener {
if (mediaPlayer.isPlaying) {
holder.pauseButton.setImageResource(R.drawable.play)
mediaPlayer.pause()
} else {
holder.pauseButton.setImageResource(R.drawable.pause)
mediaPlayer.start()
}
setPlayingSong(position)
}
}
public void setPlayingSong(int pos){
for (int i = 0; i < songInfoList.size; i++){
songInfoList.get(i).setIsPlaying(false);
}
songInfoList.get(pos).setIsPlaying(true);
notifyDataSetChanged();
}