XamarinAndroid組件教程RecylerView動畫組件使用動畫(3)
10年積累的成都網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有蒙城免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
(8)打開Main.axml文件,構(gòu)建主界面。代碼如下:
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#3DC49D" android:minHeight="?attr/actionBarSize"> android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:background="?attr/selectableItemBackground" android:padding="10dp" android:text="DEL"/> android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/del" android:layout_centerInParent="true" android:background="?attr/selectableItemBackground" android:padding="10dp" android:text="ADD"/> android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"/>
(9)打開MainActivity.cs文件,設(shè)置RecylerView子元素添加和刪除時(shí)的動畫效果。代碼如下:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.Widget;
using System.Linq;
using RecyclerViewAnimators.Animators;
using Android.Support.V7.App;
namespace RecylerViewAnimatorsItemAnimator
{
[Activity(Label = "RecylerViewAnimatorsItemAnimator", MainLauncher = true, Icon = "@mipmap/icon", Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity
{
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 toolbar = FindViewById
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayShowTitleEnabled(false);
var recyclerView = FindViewById
recyclerView.SetLayoutManager(new LinearLayoutManager(this)); //設(shè)置布局管理
var datalist = data.ToList
var adapter = new DataAdapter(this, datalist);
recyclerView.SetAdapter(adapter); //設(shè)置適配器
recyclerView.SetItemAnimator(new FlipInLeftYAnimator()); //設(shè)置動畫效果
//添加子元素
FindViewById(Resource.Id.add).Click += (sender, e) => {
adapter.Add("newly added item", 1);
};
//刪除子元素
FindViewById(Resource.Id.del).Click += (sender, e) => {
adapter.Remove(1);
};
}
}
}
運(yùn)行程序后,初始狀態(tài)如圖1.1所示。輕拍Add按鈕,實(shí)現(xiàn)子元素的添加,在添加子元素的時(shí)候會伴有指定動畫效果,如圖1.2所示。輕拍DEL按鈕,實(shí)現(xiàn)子元素的刪除,在子元素刪除的過程中也會伴有指定的動畫效果。