xamarinandroid Components Tutorial Recylerview animation components using animations (2)If the developer wants to animate the child elements of Recylerview, the Setitemanimator () method in the Recyclerview class needs to be used in the following syntax:
public virtual void Setitemanimator (Android.Support.V7.Widget.RecyclerView.ItemAnimator animator)
Where the animator parameter specifies an animation, which is the type of animation listed in table 1-1.
Under example 1-1, the child element animation is implemented when the child elements of Recylerview are added and deleted. The following steps are as follows: (1) Create a project named 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 and Xamarin.Android.Support.Vector.Drawable.dll libraries added to Recylerviewanimatorsitemanimat Or in a reference to a project. (3) Add picture Image.jpg to the Drawable folder under resources of the Recylerviewanimatorsitemanimator project. (4) Create an XML file named Layout_list_item. (5) Open the Layout_list_item.cs file and build the child elements of the Recylerview. The code is as follows:
<?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) Create an adapter file named DataAdapter.
(7) Open the DataAdapter.cs file and add the following code:
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; Construction method Public DataAdapter (context context, list<string> DataSet) {This.context = context; This.dataset = DataSet; }//number of child elements public override int ItemCount {get {return DataSet. Count; }}//Returns a custom 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); The}//fill Oncreateviewholder () method returns the control in the 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]; }//delete child element public void Remove (int position) {DataSet. RemoveAt (position); notifyitemremoved (position); }//Add child element 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); } } }}
Note: Developers can only invoke notifyitemremoved (), notifyiteminserted (), notifyitemchanged (), and notifyitemmoved () methods to animate child elements.
Xamarinandroid Components Tutorial Recylerview animation components using animations (2)