JoshuaMad
JoshuaMad

Reputation: 587

Correct way of implementing fade in/out animation for recyclerview

I was wondering, what's the correct way of implementing fade in/out animation for recyclerview items (onClick event) using MVP pattern.

Well, my approach was following:

  1. User touches item

[In my VIEW] adapter.notifyItemChanged(position: Int)

  1. onBindViewHolder() reacts and this is where I set my animations:

    override fun itemSelected(selected: Boolean) { if (selected) { notSoSpecialView.visibility = View.GONE specialView.startAnimation(AnimationUtils.fadeIn()) specialView.visibility = View.VISIBLE } else { notSoSpecialView.visibility = View.VISIBLE specialView.startAnimation(AnimationUtils.fadeOut() specialView.visibility = View.GONE } }

Well, it works as I expect when Item is clicked - it fades out/in correctly, but, when I fastly scroll down/up, views get rebinded and now, every view calls itemSelected(false) which makes unnecessary animations. How can I avoid this?

I did try to use specialView.clearAnimation(), but that did not work.

Upvotes: 0

Views: 3272

Answers (1)

tatocaster
tatocaster

Reputation: 331

You need ItemAnimator.

for example, you can refer this link on SO and also a very great resource from the Medium

Upvotes: 2

Related Questions