在MvxRecyclerView中为选定项设置动画

时间:2016-07-22 07:53:30

标签: android xamarin mvvmcross

我正在使用MvxRecyclerView,我想为列表中的选定项目设置动画。如何获取所选项目视图的引用?我应该使用TouchDelegate吗?

1 个答案:

答案 0 :(得分:9)

您应该可以通过RecyclerAdapter获取视图参考。

实施示例:

创建自定义MvxRecyclerAdapter以处理您想要的动画。

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

MvxRecyclerView

中实施适配器
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
recyclerView.Adapter = new SelectedAnimatorRecyclerAdapter((IMvxAndroidBindingContext)BindingContext);

enter image description here