XamarinAndroid元件教程RecylerView介面卡設定動畫示例

大學霸發表於2018-06-13

XamarinAndroid元件教程RecylerView介面卡設定動畫示例

【示例1-3】下面將在RecylerView的子元素進行滾動時,使用介面卡動畫。具體的操作步驟如下:

1)建立一個名為RecylerViewAnimatorsAdapter的專案。

2)將RecyclerViewAnimators.dllSquare.OkHttp.dllSquare.OkIO.dllSquare.Picasso.dllXamarin.Android.Arch.Core.Common.dllXamarin.Android.Arch.Lifecycle.Common.dllXamarin.Android.Arch.Lifecycle.Runtime.dllXamarin.Android.Support.Animated.Vector.Drawable.dllXamarin.Android.Support.Annotations.dllXamarin.Android.Support.Compat.dllXamarin.Android.Support.Core.UI.dllXamarin.Android.Support.Core.Utils.dllXamarin.Android.Support.Fragment.dllXamarin.Android.Support.Media.Compat.dllXamarin.Android.Support.v4.dllXamarin.Android.Support.v7.AppCompat.dllXamarin.Android.Support.v7.RecyclerView.dllXamarin.Android.Support.Vector.Drawable.dll庫新增到RecylerViewAnimatorsAdapter專案的引用中。

3)新增圖片chip.jpgRecylerViewAnimatorsAdapter專案的Resources下方的drawable資料夾中。

4)建立一個xml檔案,命名為layout_list_item

5)開啟layout_list_item.cs檔案,構建RecylerView的子元素。程式碼與RecylerViewAnimatorsItemAnimator專案一樣。只不過需要將TextView的顏色設定為黑色。

6)建立一個介面卡檔案,命名為DataAdapter

7)開啟DataAdapter.cs檔案,新增以下程式碼:


點選(此處)摺疊或開啟

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using Square.Picasso;
  12. using Android.Support.V7.Widget;
  13. namespace RecylerViewAnimatorsAdapter
  14. {
  15.     class DataAdapter : RecyclerView.Adapter
  16.     {
  17.         Context context;
  18.         List<string> dataset;
  19.         public DataAdapter(Context context, List<string> dataset)
  20.         {
  21.             this.context = context;
  22.             this.dataset = dataset;
  23.         }
  24.         //子元素的個數
  25.         public override int ItemCount
  26.         {
  27.             get
  28.             {
  29.                 return dataset.Count;
  30.             }
  31.         }
  32.         //返回一個自定義的ViewHolder
  33.         public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
  34.         {
  35.             var v = LayoutInflater.From(context).Inflate(Resource.Layout.layout_list_item, parent, false);
  36.             return new ViewHolder(v);
  37.         }
  38.         //填充onCreateViewHolder()方法返回的ViewHolder中的控制元件
  39.         public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
  40.         {
  41.             var h = (ViewHolder)holder;
  42.             Picasso.With(context).Load(Resource.Drawable.image).Into(h.Image);
  43.             h.Text.Text = dataset[position];
  44.         }
  45.         private class ViewHolder : RecyclerView.ViewHolder
  46.         {
  47.             public ImageView Image { get; private set; }
  48.             public TextView Text { get; private set; }
  49.             public ViewHolder(View itemView)
  50.                 : base(itemView)
  51.             {
  52.                 Image = itemView.FindViewById<ImageView>(Resource.Id.image);
  53.                 Text = itemView.FindViewById<TextView>(Resource.Id.text);
  54.             }
  55.         }
  56.     }
  57. }

8)開啟Main.axml檔案,構建RecyclerView。程式碼如下:


點選(此處)摺疊或開啟

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.               android:orientation="vertical"
  4.               android:layout_width="match_parent"
  5.               android:layout_height="match_parent"
  6.               android:background="#FFFFFF">
  7.   <android.support.v7.widget.RecyclerView
  8.        android:id="@+id/list"
  9.        android:layout_width="match_parent"
  10.        android:layout_height="match_parent"/>
  11. </LinearLayout>

9)開啟MainActivity.cs檔案,在RecylerView滾動時使用介面卡動畫。程式碼如下:


點選(此處)摺疊或開啟

  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Android.Support.V7.Widget;
  5. using System.Linq;
  6. using RecyclerViewAnimators.Adapters;
  7. using Android.Views.Animations;
  8. namespace RecylerViewAnimatorsAdapter
  9. {
  10.     [Activity(Label = "RecylerViewAnimatorsAdapter", MainLauncher = true, Icon = "@mipmap/icon")]
  11.     public class MainActivity : Activity
  12.     {
  13.         static readonly string[] data = {
  14.             "Apple", "Ball", "Camera", "Day", "Egg", "Foo", "Google", "Hello", "Iron", "Japan", "Coke",
  15.             "Dog", "Cat", "Yahoo", "Sony", "Canon", "Fujitsu", "USA", "Nexus", "LINE", "Haskell", "C++",
  16.             "Java", "Go", "Swift", "Objective-c", "Ruby", "PHP", "Bash", "ksh", "C", "Groovy", "Kotlin",
  17.             "Chip", "Japan", "U.S.A", "San Francisco", "Paris", "Tokyo", "Silicon Valley", "London",
  18.             "Spain", "China", "Taiwan", "Asia", "New York", "France", "Kyoto", "Android", "Google", "C#",
  19.             "iPhone", "iPad", "iPod", "Wasabeef", "Xamarin", "South Africa", "Cape Town", "Microsoft"
  20.         };
  21.         protected override void OnCreate(Bundle savedInstanceState)
  22.         {
  23.             base.OnCreate(savedInstanceState);
  24.             SetContentView(Resource.Layout.Main);
  25.             var recyclerView = FindViewById<RecyclerView>(Resource.Id.list);
  26.             recyclerView.SetLayoutManager(new LinearLayoutManager(this));
  27.             var adapter = new DataAdapter(this, data.ToList());
  28.             var alphaAdapter = new AlphaInAnimationAdapter(adapter); //建立介面卡動畫
  29.             var scaleAdapter = new ScaleInAnimationAdapter(alphaAdapter); //建立複合介面卡動畫
  30.             scaleAdapter.SetFirstOnly(false); //不是顯示一次動畫效果
  31.             scaleAdapter.SetInterpolator(new OvershootInterpolator()); //設定插值器
  32.             recyclerView.SetAdapter(scaleAdapter); //設定介面卡
  33.         }
  34.     }
  35. }

執行程式後,初始狀態如圖1.3所示。當滾動子元素後,會看到動畫效果。

1.3  初始狀態

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29597077/viewspace-2156085/,如需轉載,請註明出處,否則將追究法律責任。

相關文章