title: How to Add and Remove Animation RecyclerView's items
body: <p>I'm making a music app. I have a recyclerview and I am playing clicked item's music, at the same time animation works too. Question is whenever user change the recyclerview's item position I need to cancel first animation and start new one for new position. I mean every time will be just 1 animation exist.</p>
  <p><strong>My RecyclerViewAdapter</strong></p>
  <pre><code>class RecyclerViewAdapter(private val ModelList:List&lt;Musics&gt;,private val listener: 
  ClickListener) :RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt;(){
  
  
  
  class ViewHolder(var binding:GridListBinding):RecyclerView.ViewHolder(binding.root) {
  
  }
  
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
      val inflater = LayoutInflater.from(parent.context)
      val view = DataBindingUtil.inflate&lt;GridListBinding&gt;(inflater,R.layout.grid_list,parent,false)
      return ViewHolder(view)
  }
  
  override fun onBindViewHolder(holder: RecyclerViewAdapter.ViewHolder, position: Int) {
  
      holder.binding.musics = ModelList[position]
  
  
      holder.binding.constraint.setOnClickListener{
          listener.buttonClicked(holder.binding.constraint,ModelList[position],position)
      }
  }
  
  override fun getItemCount(): Int {
      return ModelList.size
  }
  
  }
  </code></pre>
  <p><strong>My ClickListener for adapter</strong></p>
  <pre><code>interface ClickListener {
  fun buttonClicked(view: View,model: Musics,position:Int)
  
  }
  </code></pre>
  <p><strong>grid_list for recyclerviewAdapter</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;layout
  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  &gt;
  &lt;data&gt;
  
      &lt;variable
          name=&quot;musics&quot;
          type=&quot;com.tunahan.musicplayer.model.Musics&quot; /&gt;
      &lt;variable
          name=&quot;listener&quot;
          type=&quot;com.tunahan.musicplayer.adapter.ClickListener&quot; /&gt;
  
  &lt;/data&gt;
  
   &lt;androidx.constraintlayout.widget.ConstraintLayout
  
  android:layout_width=&quot;125dp&quot;
  android:id=&quot;@+id/constraint&quot;
  android:layout_height=&quot;125dp&quot;
  android:layout_marginTop=&quot;10dp&quot;
  android:layout_marginStart=&quot;5dp&quot;
  tools:layout_editor_absoluteX=&quot;52dp&quot;
  tools:layout_editor_absoluteY=&quot;42dp&quot;
  &gt;
  
  
  &lt;ImageView
      android:id=&quot;@+id/image&quot;
      android:layout_width=&quot;0dp&quot;
      android:layout_height=&quot;0dp&quot;
      app:layout_constraintBottom_toTopOf=&quot;@+id/linearLayout&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;
      app:layout_constraintTop_toTopOf=&quot;parent&quot;
      android:src=&quot;@drawable/musical&quot;
      tools:ignore=&quot;ContentDescription&quot;
  
  
  
      /&gt;
  
  
  &lt;LinearLayout
      android:id=&quot;@+id/linearLayout&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;40dp&quot;
      android:layout_gravity=&quot;bottom&quot;
      android:orientation=&quot;horizontal&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
  
  
      &lt;TextView
          android:id=&quot;@+id/gridTextView&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;40dp&quot;
          android:layout_weight=&quot;1&quot;
          android:fontFamily=&quot;@font/allerta&quot;
          android:gravity=&quot;center&quot;
          android:paddingStart=&quot;5dp&quot;
          android:text=&quot;@{musics.filename}&quot;
          android:textColor=&quot;@color/black&quot;
          android:textSize=&quot;16sp&quot;
          tools:ignore=&quot;RtlSymmetry&quot; /&gt;
  
  
  
  
     &lt;/LinearLayout&gt;
  
  
     &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
     &lt;/layout&gt;
  </code></pre>
  <p><strong>And Pulse Animation for gridlist's imageView</strong></p>
  <pre><code>       scaleDown = ObjectAnimator.ofPropertyValuesHolder(
              view.image,
              PropertyValuesHolder.ofFloat(&quot;scaleX&quot;, 1.1f),
              PropertyValuesHolder.ofFloat(&quot;scaleY&quot;, 1.1f),
          )
          scaleDown.duration = 500
          scaleDown.repeatCount = ObjectAnimator.INFINITE
          scaleDown.repeatMode = ObjectAnimator.REVERSE
  
          scaleDown.start()
  </code></pre>
  <p><strong>ClickListener</strong></p>
  <pre><code>override fun buttonClicked(view: View, model: Musics, position: Int) {
  
      path = model.filepath
      nextMusic = position
      positionplus = position
      handler.removeCallbacks(runnable)
  
      saveduid = sharedPref.getInt(&quot;changed&quot;, model.uid)
      editor = sharedPref.edit()
      editor.putInt(&quot;changed&quot;, model.uid)
      editor.apply()
  
      editor2.putInt(&quot;positionplus&quot;, positionplus)
      editor2.apply()
  
  
  
  
  
      if (saveduid == model.uid) {
  
          if (mediaPlayer.isPlaying) {
              ForegroundService.startService(
                  this, path = path, action = &quot;ACTION_PAUSE&quot;,
                  positionplus, ModelList[positionplus].filename
              )
              scaleDown.cancel()
  
              binding.seekbartextend.text = createTimeLabel(mediaPlayer.duration)
  
              binding.playButton.setImageResource(R.drawable.ic_baseline_play_arrow_24)
  
          } else {
              ForegroundService.startService(
                  this, path = path, action = &quot;ACTION_PLAY&quot;,
                  positionplus,ModelList[positionplus].filename
              )
  
  
              binding.seekbartextend.text = createTimeLabel(mediaPlayer.duration)
  
              mediaPlayer.setOnCompletionListener {
                  nextButton()
              }
  
              binding.playButton.setImageResource(R.drawable.circledpause)
              runnable = Runnable {
                  binding.seekBar.max = mediaPlayer.duration
                  binding.seekBar.progress = mediaPlayer.currentPosition
                  binding.seekbarTextstart.text = createTimeLabel(mediaPlayer.currentPosition)
                  binding.seekbartextend.text = createTimeLabel(mediaPlayer.duration)
                  handler.postDelayed(runnable, 1000)
              }
              handler.post(runnable)
  
          }
  
  
      } else {
  
  
          ForegroundService.startService(
              this, path = path, action = &quot;ACTION_RESET&quot;,
              positionplus,ModelList[positionplus].filename
          )
          if (positionplus != position)
  
          scaleDown = ObjectAnimator.ofPropertyValuesHolder(
              view.image,
              PropertyValuesHolder.ofFloat(&quot;scaleX&quot;, 1.1f),
              PropertyValuesHolder.ofFloat(&quot;scaleY&quot;, 1.1f),
          )
          scaleDown.duration = 500
          scaleDown.repeatCount = ObjectAnimator.INFINITE
          scaleDown.repeatMode = ObjectAnimator.REVERSE
  
          scaleDown.start()
  
  
  
          binding.seekbartextend.text = createTimeLabel(mediaPlayer.duration)
  
          mediaPlayer.setOnCompletionListener {
              nextButton()
          }
          binding.playButton.setImageResource(R.drawable.circledpause)
          runnable = Runnable {
              binding.seekBar.max = mediaPlayer.duration
              binding.seekBar.progress = mediaPlayer.currentPosition
              binding.seekbarTextstart.text = createTimeLabel(mediaPlayer.currentPosition)
              binding.seekbartextend.text = createTimeLabel(mediaPlayer.duration)
              handler.postDelayed(runnable, 1000)
          }
          handler.post(runnable)
      }
  
  
  }
  </code></pre>