doorman doorman - 4 months ago 128
Android Question

Animate selected item in MvxRecyclerView

I am using MvxRecyclerView and I want to animate the selected item in the list. How can I get a reference to the selected item view? Should I use TouchDelegate?

Answer

You should be able to get the view reference through the RecyclerAdapter.


Implementation Example:

Create a custom MvxRecyclerAdapter to deal with your desired animation.

public class SelectedAnimatorRecyclerAdapter : MvxRecyclerAdapter
{
    public SelectedAnimatorRecyclerAdapter(IMvxAndroidBindingContext bindingContext)
          : base(bindingContext)
    {
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        base.OnBindViewHolder(holder, position);

        holder.ItemView.Click += (s, e) =>
        {
            SetAnimation(holder.ItemView);
        };
    }

    void SetAnimation(View viewToAnimate)
    {
        ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
        anim.Duration = 2000;
        viewToAnimate.StartAnimation(anim);
    }
}

Implement adapter in your MvxRecyclerView

var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
recyclerView.Adapter = new SelectedAnimatorRecyclerAdapter((IMvxAndroidBindingContext)BindingContext);

enter image description here