XamarinAndroid组件教程RecylerView自定义适配器动画
如果RecyclerViewAnimators.Adapters命名空间中没有所需要的适配器动画,开发者可以自定义动画。此时,需要让自定义的动画继承AnimationAdapter抽象类。
【示例1-4】下面以RecylerViewAnimatorsAdapter项目为基础,在RecylerView的子元素进行滚动时,实现旋转适配器动画。具体的操作步骤如下:
(1)创建一个适配器文件,命名为RotationAdapter。
(2)打开RotationAdapter.cs文件,实现旋转动画。代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.Animation; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Support.V7.Widget; using Android.Views; using Android.Widget; using RecyclerViewAnimators.Adapters; namespace RecylerViewAnimatorsAdapter { public class RotationAdapter : AnimationAdapter { public RotationAdapter(RecyclerView.Adapter adapter) : base(adapter) { } protected override Animator[] GetAnimators(View view) { return new Animator[] { ObjectAnimator.OfFloat(view, "rotation", 0, 180, 0) }; } } }
(3)打开MainActivity.cs文件,在RecylerView的子元素进行滚动时添加旋转动画。代码如下:
…… using RecyclerViewAnimators.Adapters; using Android.Views.Animations; namespace RecylerViewAnimatorsAdapter { [Activity(Label = "RecylerViewAnimatorsAdapter", MainLauncher = true, Icon = "@mipmap/icon")] public class MainActivity : Activity { …… protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); …… var adapter = new DataAdapter(this, data.ToList()); var rotationAdapter = new RotationAdapter(adapter); recyclerView.SetAdapter(rotationAdapter); } } }
运行程序后,初始状态和图1.3一样。当滚动子元素后,会看到动画效果。