XamarinAndroid組件教程RecylerView動畫組件使用動畫(2)

來源:互聯網
上載者:User

標籤:jpg   csharp   resource   insert   apt   void   nta   bsp   命名   

XamarinAndroid組件教程RecylerView動畫組件使用動畫(2)如果開發人員要為RecylerView的子項目添加動畫效果,需要使用RecyclerView類中的SetItemAnimator()方法,其文法形式如下:
public virtual void SetItemAnimator(Android.Support.V7.Widget.RecyclerView.ItemAnimator animator)

其中,animator參數指定一個動畫,這個動畫就是表1-1中列出的動畫類型。

【樣本1-1】下面將在RecylerView的子項目進行添加以及刪除時,實現子項目動畫。具體的操作步驟如下:(1)建立一個名為RecylerViewAnimatorsItemAnimator的項目。(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庫添加到RecylerViewAnimatorsItemAnimator項目的引用中。(3)添加圖片image.jpg到RecylerViewAnimatorsItemAnimator項目的Resources下方的drawable檔案夾中。(4)建立一個xml檔案,命名為layout_list_item。(5)開啟layout_list_item.cs檔案,構建RecylerView的子項目。代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="20dp"    android:orientation="vertical">    <ImageView        android:id="@+id/image"        android:layout_width="match_parent"        android:layout_height="100dp"        android:background="#11000000"        android:scaleType="centerCrop"/>    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:textSize="18sp" /></LinearLayout>

(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 RecylerViewAnimatorsItemAnimator{    public 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;            }        }        //返回一個自訂的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];        }        //刪除子項目        public void Remove(int position)        {            dataset.RemoveAt(position);            NotifyItemRemoved(position);        }        //添加子項目        public void Add(string text, int position)        {            dataset.Insert(position, text);            NotifyItemInserted(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);            }        }    }}

注意:開發人員只有調用NotifyItemRemoved()、NotifyItemInserted()、NotifyItemChanged()和NotifyItemMoved()方法,才可以觸發子項目動畫。

 

XamarinAndroid組件教程RecylerView動畫組件使用動畫(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.