XamarinAndroid组件教程设置自定义子元素动画(二)
(9)打开MainActivity.cs文件,为RecylerView的子元素设置添加和删除时的透明动画效果。代码如下:
……using Android.Views.Animations;namespace RecylerViewAnimatorsItemAnimator{ [Activity(Label = "RecylerViewAnimatorsItemAnimator", MainLauncher = true, Icon = "@mipmap/icon", Theme = "@style/AppTheme")] public class MainActivity : AppCompatActivity { …… protected override void OnCreate(Bundle savedInstanceState) {…… recyclerView.SetAdapter(adapter); var alphaAnimator = new AlphaAnimator(); alphaAnimator.SetInterpolator(new OvershootInterpolator()); recyclerView.SetItemAnimator(alphaAnimator); recyclerView.GetItemAnimator().AddDuration = 1200; recyclerView.GetItemAnimator().RemoveDuration = 1200; FindViewById(Resource.Id.add).Click += (sender, e) => { adapter.Add("newly added item", 1); };…… } }}
运行程序后,初始状态和图1.1一样。轻拍Add按钮,添加一个子元素。在添加子元素的时候,会伴有指定的动画效果。轻拍DEL按钮,删除一个子元素,同样会伴有指定的动画效果。