XamarinAndroid组件教程RecylerView适配器设置动画示例
【示例1-3】下面将在RecylerView的子元素进行滚动时,使用适配器动画。具体的操作步骤如下:
(1)创建一个名为RecylerViewAnimatorsAdapter的项目。
(2)将RecyclerViewAnimators.dll、Square.OkHttp.dll、Square.OkIO.dll、Square.Picasso.dll、Xamarin.Android.Arch.Core.Common.dll、Xamarin.Android.Arch.Lifecycle.Common.dll、Xamarin.Android.Arch.Lifecycle.Runtime.dll、Xamarin.Android.Support.Animated.Vector.Drawable.dll、Xamarin.Android.Support.Annotations.dll、Xamarin.Android.Support.Compat.dll、Xamarin.Android.Support.Core.UI.dll、Xamarin.Android.Support.Core.Utils.dll、Xamarin.Android.Support.Fragment.dll、Xamarin.Android.Support.Media.Compat.dll、Xamarin.Android.Support.v4.dll、Xamarin.Android.Support.v7.AppCompat.dll、Xamarin.Android.Support.v7.RecyclerView.dll和Xamarin.Android.Support.Vector.Drawable.dll库添加到RecylerViewAnimatorsAdapter项目的引用中。
(3)添加图片chip.jpg到RecylerViewAnimatorsAdapter项目的Resources下方的drawable文件夹中。
(4)创建一个xml文件,命名为layout_list_item。
(5)打开layout_list_item.cs文件,构建RecylerView的子元素。代码与RecylerViewAnimatorsItemAnimator项目一样。只不过需要将TextView的颜色设置为黑色。
(6)创建一个适配器文件,命名为DataAdapter。
(7)打开DataAdapter.cs文件,添加以下代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Android.App;using Android.Content;using Android.OS;using Android.Runtime;using Android.Views;using Android.Widget;using Square.Picasso;using Android.Support.V7.Widget;namespace RecylerViewAnimatorsAdapter{class DataAdapter : RecyclerView.Adapter{Context context;List<string> dataset;public DataAdapter(Context context, List<string> dataset){this.context = context;this.dataset = dataset;}//子元素的个数public override int ItemCount{get{return dataset.Count;}}//返回一个自定义的ViewHolderpublic override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType){var v = LayoutInflater.From(context).Inflate(Resource.Layout.layout_list_item, parent, false);return new ViewHolder(v);}//填充onCreateViewHolder()方法返回的ViewHolder中的控件public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position){var h = (ViewHolder)holder;Picasso.With(context).Load(Resource.Drawable.image).Into(h.Image);h.Text.Text = dataset[position];}private class ViewHolder : RecyclerView.ViewHolder{public ImageView Image { get; private set; }public TextView Text { get; private set; }public ViewHolder(View itemView): base(itemView){Image = itemView.FindViewById<ImageView>(Resource.Id.image);Text = itemView.FindViewById<TextView>(Resource.Id.text);}}}}
(8)打开Main.axml文件,构建RecyclerView。代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"><android.support.v7.widget.RecyclerViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>
(9)打开MainActivity.cs文件,在RecylerView滚动时使用适配器动画。代码如下:
using Android.App;using Android.Widget;using Android.OS;using Android.Support.V7.Widget;using System.Linq;using RecyclerViewAnimators.Adapters;using Android.Views.Animations;namespace RecylerViewAnimatorsAdapter{[Activity(Label = "RecylerViewAnimatorsAdapter", MainLauncher = true, Icon = "@mipmap/icon")]public class MainActivity : Activity{static readonly string[] data = {"Apple", "Ball", "Camera", "Day", "Egg", "Foo", "Google", "Hello", "Iron", "Japan", "Coke","Dog", "Cat", "Yahoo", "Sony", "Canon", "Fujitsu", "USA", "Nexus", "LINE", "Haskell", "C++","Java", "Go", "Swift", "Objective-c", "Ruby", "PHP", "Bash", "ksh", "C", "Groovy", "Kotlin","Chip", "Japan", "U.S.A", "San Francisco", "Paris", "Tokyo", "Silicon Valley", "London","Spain", "China", "Taiwan", "Asia", "New York", "France", "Kyoto", "Android", "Google", "C#","iPhone", "iPad", "iPod", "Wasabeef", "Xamarin", "South Africa", "Cape Town", "Microsoft"};protected override void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);SetContentView(Resource.Layout.Main);var recyclerView = FindViewById<RecyclerView>(Resource.Id.list);recyclerView.SetLayoutManager(new LinearLayoutManager(this));var adapter = new DataAdapter(this, data.ToList());var alphaAdapter = new AlphaInAnimationAdapter(adapter); //创建适配器动画var scaleAdapter = new ScaleInAnimationAdapter(alphaAdapter); //创建复合适配器动画scaleAdapter.SetFirstOnly(false); //不是显示一次动画效果scaleAdapter.SetInterpolator(new OvershootInterpolator()); //设置插值器recyclerView.SetAdapter(scaleAdapter); //设置适配器}}}
运行程序后,初始状态如图1.3所示。当滚动子元素后,会看到动画效果。