XamarinAndroid組件教程RecylerView適配器設(shè)置動(dòng)畫(huà)示例
【示例1-3】下面將在RecylerView的子元素進(jìn)行滾動(dòng)時(shí),使用適配器動(dòng)畫(huà)仑濒。具體的操作步驟如下:
(1)創(chuàng)建一個(gè)名為RecylerViewAnimatorsAdapter的項(xiàng)目撑教。
(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庫(kù)添加到RecylerViewAnimatorsAdapter項(xiàng)目的引用中。
(3)添加圖片chip.jpg到RecylerViewAnimatorsAdapter項(xiàng)目的Resources下方的drawable文件夾中荣刑。
(4)創(chuàng)建一個(gè)xml文件馅笙,命名為layout_list_item。
(5)打開(kāi)layout_list_item.cs文件厉亏,構(gòu)建RecylerView的子元素董习。代碼與RecylerViewAnimatorsItemAnimator項(xiàng)目一樣。只不過(guò)需要將TextView的顏色設(shè)置為黑色爱只。
(6)創(chuàng)建一個(gè)適配器文件皿淋,命名為DataAdapter。
(7)打開(kāi)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 dataset;
?public DataAdapter(Context context, List dataset)
?{
?this.context = context;
?this.dataset = dataset;
?}
?//子元素的個(gè)數(shù)
?public override int ItemCount
?{
?get
?{
?return dataset.Count;
?}
?}
?//返回一個(gè)自定義的ViewHolder
?public 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(Resource.Id.image);
?Text = itemView.FindViewById(Resource.Id.text);
?}
?}
?}
}
(8)打開(kāi)Main.axml文件沥匈,構(gòu)建RecyclerView。代碼如下:
?android:orientation="vertical"
?android:layout_width="match_parent"
?android:layout_height="match_parent"
?android:background="#FFFFFF">
?android:id="@+id/list"
?android:layout_width="match_parent"
?android:layout_height="match_parent"/>
(9)打開(kāi)MainActivity.cs文件忘渔,在RecylerView滾動(dòng)時(shí)使用適配器動(dòng)畫(huà)。代碼如下:
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(Resource.Id.list);
?recyclerView.SetLayoutManager(new LinearLayoutManager(this));
?var adapter = new DataAdapter(this, data.ToList());
?var alphaAdapter = new AlphaInAnimationAdapter(adapter);?//創(chuàng)建適配器動(dòng)畫(huà)
?var scaleAdapter = new ScaleInAnimationAdapter(alphaAdapter);?//創(chuàng)建復(fù)合適配器動(dòng)畫(huà)
?scaleAdapter.SetFirstOnly(false);?//不是顯示一次動(dòng)畫(huà)效果
?scaleAdapter.SetInterpolator(new OvershootInterpolator());?//設(shè)置插值器
?recyclerView.SetAdapter(scaleAdapter);??//設(shè)置適配器
?}
?}
}
運(yùn)行程序后缰儿,初始狀態(tài)如圖1.3所示畦粮。當(dāng)滾動(dòng)子元素后,會(huì)看到動(dòng)畫(huà)效果。