How to hide the view of last clicked item on the recyclerview

Viewed 413

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)
}

Adapter:

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()
         }
    }
}

The pauseButton appears whenever the user clicks the item on the recyclerView, and the music is played. I want to disappear pauseButton for the previous clicked when the user clicks on the new item and pauseButton appear for the new entry. But, I'm unable to do this. Tell me how to do this. Whenever I click on the new content on the recyclerView the pauseButton should appear for the latest item and disappear for the previous item.

2 Answers

You need to hold state in your RecyclerView.Adapter

Inside Adapter:

private var playingPosition: Int? = null

when a click event happens:

//Update previously position
playingPosition?.let {
    models.get(playingPosition).playingState = false
    notifyItemChanged(playingPosition)
}
//update new position
models.get(position).playingState = true
playingPosition = position
notifyItemChanged(position)

Also, your model should hold the playing state:`

data class Model(...: Int, ...: String, ..., playingState: Boolean)

Inside onBindViewHolder:

if (model.playingState) view.visibility = View.VISIBLE else view.visibility = VIEW.GONE

The current function is working for only one item of the recyclerview, so you can't do it here. If you want to do it, you can do like this. You can add isPlay value to SongInfoModel and when the play clicked you can set isPlay = false for all songs except current playing song.

so the code will be like this:

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();
    }
Related