• XamarinAndroid组件教程RecylerView动画组件使用动画(3)


    XamarinAndroid组件教程RecylerView动画组件使用动画(3)

    (8)打开Main.axml文件,构建主界面。代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
                  android:orientation="vertical"
    
                  android:layout_width="match_parent"
    
                  android:layout_height="match_parent">
    
      <android.support.v7.widget.Toolbar
    
          android:id="@+id/tool_bar"
    
          android:layout_width="match_parent"
    
          android:layout_height="wrap_content"
    
          android:background="#3DC49D"
    
          android:minHeight="?attr/actionBarSize">
    
          <RelativeLayout
    
              android:layout_width="match_parent"
    
              android:layout_height="wrap_content">
    
              <TextView
    
                  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"/>
    
              <TextView
    
                  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"/>
    
          </RelativeLayout>
    
      </android.support.v7.widget.Toolbar>
    
      <android.support.v7.widget.RecyclerView
    
          android:id="@+id/list"
    
          android:layout_width="match_parent"
    
          android:layout_height="match_parent"/>
    
    </LinearLayout>
    

      

    (9)打开MainActivity.cs文件,设置RecylerView子元素添加和删除时的动画效果。代码如下:

    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<Android.Support.V7.Widget.Toolbar>(Resource.Id.tool_bar);
    
                SetSupportActionBar(toolbar);
    
                SupportActionBar.SetDisplayShowTitleEnabled(false);
    
                var recyclerView = FindViewById<RecyclerView>(Resource.Id.list);
    
                recyclerView.SetLayoutManager(new LinearLayoutManager(this));              //设置布局管理
    
                var datalist = data.ToList<string>();
    
                var adapter = new DataAdapter(this, datalist);
    
                recyclerView.SetAdapter(adapter);                                                                        //设置适配器
    
                recyclerView.SetItemAnimator(new FlipInLeftYAnimator());                        //设置动画效果
    
                //添加子元素
    
                FindViewById(Resource.Id.add).Click += (sender, e) => {
    
                    adapter.Add("newly added item", 1);
    
                };
    
                //删除子元素
    
                FindViewById(Resource.Id.del).Click += (sender, e) => {
    
                    adapter.Remove(1);
    
                };
    
            }
    
        }
    
    }
    

      

    运行程序后,初始状态如图1.1所示。轻拍Add按钮,实现子元素的添加,在添加子元素的时候会伴有指定动画效果,如图1.2所示。轻拍DEL按钮,实现子元素的删除,在子元素删除的过程中也会伴有指定的动画效果。

     

    图1.1  初始状态                          图1.2  添加数据

  • 相关阅读:
    这几个C++的坑,一旦踩中了,加班是肯定避免不了了!
    2020年11月编程语言排行榜:Java再掉一位排名,真的要跌落神坛了吗?
    自学编程的朋友想要了解怎么快速入门,我想给你这5个建议!
    来看看这些获奖的C语言程序!一个比一个秀,这就是强者的世界吗!
    【每天学点新知识】Linux操作系统下C语言多线程同步使用指南!
    听说IT行业只有高智商的人才能进得去,而且以男性居多,还必须专业对口?
    假如C++是一只箭,你会用它来射哪只雕?
    程序员常见的口头禅,最后一个笑掉大牙,网友:真实的一批!
    Python基础语法
    Python—“helloworld”
  • 原文地址:https://www.cnblogs.com/daxueba-ITdaren/p/9049843.html
Copyright © 2020-2023  润新知