XamarinAndroid組件教程RecylerView自定義適配器動畫

XamarinAndroid組件教程RecylerView自定義適配器動畫

若是RecyclerViewAnimators.Adapters命名空間中沒有所須要的適配器動畫,開發者能夠自定義動畫。此時,須要讓自定義的動畫繼承AnimationAdapter抽象類。ide

【示例1-4】下面以RecylerViewAnimatorsAdapter項目爲基礎,在RecylerView的子元素進行滾動時,實現旋轉適配器動畫。具體的操做步驟以下:動畫

(1)建立一個適配器文件,命名爲RotationAdapter。this

(2)打開RotationAdapter.cs文件,實現旋轉動畫。代碼以下:spa

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Android.Animation;

using Android.App;

using Android.Content;

using Android.OS;

using Android.Runtime;

using Android.Support.V7.Widget;

using Android.Views;

using Android.Widget;

using RecyclerViewAnimators.Adapters;

namespace RecylerViewAnimatorsAdapter

{

    public class RotationAdapter : AnimationAdapter

    {

        public RotationAdapter(RecyclerView.Adapter adapter) : base(adapter)

        {

        }

        protected override Animator[] GetAnimators(View view)

        {

            return new Animator[] { ObjectAnimator.OfFloat(view, "rotation", 0, 180, 0) };

        }

    }

}

(3)打開MainActivity.cs文件,在RecylerView的子元素進行滾動時添加旋轉動畫。代碼以下:code

……

using RecyclerViewAnimators.Adapters;

using Android.Views.Animations;

namespace RecylerViewAnimatorsAdapter

{

    [Activity(Label = "RecylerViewAnimatorsAdapter", MainLauncher = true, Icon = "@mipmap/icon")]

    public class MainActivity : Activity

    {

        ……

        protected override void OnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Main);

            ……

            var adapter = new DataAdapter(this, data.ToList());

            var rotationAdapter = new RotationAdapter(adapter);

            recyclerView.SetAdapter(rotationAdapter);

        }

    }

}

運行程序後,初始狀態和圖1.3同樣。當滾動子元素後,會看到動畫效果。繼承

相關文章
相關標籤/搜索