Simple reuse of Xamarin-Android_BaseAdapter, adapter Reuse

Source: Internet
Author: User

Simple reuse of Xamarin-Android_BaseAdapter, adapter Reuse
Simple reuse of Xamarin-Android_BaseAdapter

Reason:

I am a beginner in Xamarin-Android. I am studying the ListView control. I found that the custom layout of this control is called a good word.

But every time you use ListView, You have to inherit the BaseAdapter. So angry, I am very lazy and have to seek ways of improvement.

I am so embarrassed to write this blog here. I also ask you to correct me. Thank you! [Thank you, O (∩ _ ∩) O ha !]

 

The younger brother has made the following improvements:

  • The generic extraction is basically the same, but only the type is different. This is the Person type, and the next time it may be the difference of the Student type.
  • Use the delegate to replace the View Assignment Method. This is different except for the type.

The following is how I feel ugly and crying (⊙ o ⊙ )...

Code provisioning

Layout code of ListItem

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "horizontal" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: minWidth = "25px" 7 android: minHeight = "25px"> 8 <ImageView 9 android: layout_width = "89.5dp" 10 android: layout_height = "67.5dp" 11 android: id = "@ + id/imgHard"/> 12 <LinearLayout13 android: orientation = "vertical" 14 Android: minWidth = "25px" 15 android: minHeight = "25px" 16 android: layout_width = "200dp" 17 android: layout_height = "67.5dp" 18 android: id = "@ + id/linearLayout1"> 19 <TextView20 android: text = "Large Text" 21 android: textAppearance = "? Android: attr/textAppearanceLarge "22 android: layout_width =" 279.5dp "23 android: layout_height =" wrap_content "24 android: id = "@ + id/txtName"/> 25 <TextView26 android: text = "Small Text" 27 android: textAppearance = "? Android: attr/textAppearanceSmall "28 android: layout_width =" match_parent "29 android: layout_height =" wrap_content "30 android: id =" @ + id/txtDesc "31 android: textSize = "12dp"/> 32 </LinearLayout> 33 <Button34 android: text = "view information" 35 android: layout_width = "wrap_content" 36 android: layout_height = "67.5dp" 37 android: id = "@ + id/button1"/> 38 </LinearLayout>

 

 

Main Interface code

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent"> 6     <ListView 7         android:minWidth="25px" 8         android:minHeight="25px" 9         android:layout_width="match_parent"10         android:layout_height="match_parent"11         android:id="@+id/itemsPerson"12         android:divider="@android:color/white"13         android:dividerHeight="3dp"14         android:descendantFocusability="afterDescendants" />15 </LinearLayout>

 

AllRoundBaseAdapter code

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 using Android. app; 7 using Android. content; 8 using Android. OS; 9 using Android. runtime; 10 using Android. views; 11 using Android. widget; 12 13 namespace AdapterStudy_2017030114 {15 public class AllRoundAdapter <T>: baseAdapter <T> 16 {17 # region global variable 18 /// <summary> 19 /// Context 20 /// </summary> 21 public Acti Export MyContext {get; set;} 22 23 // <summary> 24 // data source 25 /// </summary> 26 public List <T> MyDataSource {get; set;} 27 28 /// <summary> 29 // Item layout resource 30 /// </summary> 31 public int LayoutSource {get; set ;} 32 33 // <summary> 34 // delegate 35 /// </summary> 36 Action <View, T> MyAction; 37 # endregion38 39 // <summary> 40 // create AllRoundBaseAdapter41 // </summary> 42 // <param name = "actity"> context </par Am> 43 // <param name = "listDataSource"> data source </param> 44 // <param name = "layoutSource"> layout ID </param> 45 // /<param name = "action"> method used to assign values to the layout </param> 46 public AllRoundAdapter (Activity actity, list <T> listDataSource, int layoutSource, Action <View, T> action) 47 {48 this. myContext = actity; 49 this. myDataSource = listDataSource; 50 51 this. layoutSource = layoutSource; 52 53 this. myAction = action; 54} 55 56 pub Lic override T this [int position] 57 {58 get59 {60 return MyDataSource [position]; 61} 62} 63 64 public override int Count65 {66 get67 {68 return this. myDataSource. count; 69} 70} 71 72 public override long GetItemId (int position) 73 {74 return position; 75} 76 77 public override View GetView (int position, View convertView, ViewGroup parent) 78 {79 T = this. myDataSource [position]; 80 81 View v = convertV Iew; 82 if (v = null) 83 {84 v = this. MyContext. LayoutInflater. Inflate (this. LayoutSource, null); 85} 86 87 if (this. MyAction! = Null) 88 {89 this. MyAction. Invoke (v, t); 90} 91 92 return v; 93} 94} 95}

 

 

MainActivity code

1 using System; 2 using Android. app; 3 using Android. content; 4 using Android. runtime; 5 using Android. views; 6 using Android. widget; 7 using Android. OS; 8 using System. collections. generic; 9 using Javax. crypto; 10 11 namespace AdapterStudy_2017030112 {13 [Activity (Label = "AdapterStudy_20170301", MainLauncher = true, Icon = "@ drawable/icon")] 14 public class MainActivity: activity15 {16 17 private ListView lvPerson; 18 private List <Person> listPerson = new List <Person> (); 19 20 protected override void OnCreate (Bundle bundle) 21 {22 base. onCreate (bundle); 23 24 // Set our view from the "main" layout resource25 SetContentView (Resource. layout. main); 26 27 28 lvPerson = FindViewById <ListView> (Resource. id. itemsPerson); 29 30 // fill in some data 31 this. listPerson. add (new Person ("Zhang Fei", Resource. drawable. person, "Zhang yide"); 32 this. listPerson. add (new Person ("Liu Bei", Resource. drawable. person, "Liu Xuande"); 33 this. listPerson. add (new Person ("Guan Yu", Resource. drawable. person, ""); 34 this. listPerson. add (new Person ("Zhang Fei", Resource. drawable. person, "Zhang yide"); 35 this. listPerson. add (new Person ("Liu Bei", Resource. drawable. person, "Liu Xuande"); 36 this. listPerson. add (new Person ("Guan Yu", Resource. drawable. person, ""); 37 this. listPerson. add (new Person ("Zhang Fei", Resource. drawable. person, "Zhang yide"); 38 this. listPerson. add (new Person ("Liu Bei", Resource. drawable. person, "Liu Xuande"); 39 this. listPerson. add (new Person ("Zhang Fei", Resource. drawable. person, "Zhang yide"); 40 this. listPerson. add (new Person ("Liu Bei", Resource. drawable. person, "Liu Xuande"); 41 this. listPerson. add (new Person ("Guan Yu", Resource. drawable. person, ""); 42 this. listPerson. add (new Person ("Guan Yu", Resource. drawable. person, ""); 43 44 // set the header at the end of the table 45 View header = this. layoutInflater. inflate (Resource. layout. headerLayout, null); 46 View footer = this. layoutInflater. inflate (Resource. layout. footerLayout, null); 47 48 this. lvPerson. addHeaderView (header); 49 this. lvPerson. addFooterView (footer); 50 51 // use a generic Adapter to assign a value of 52 lvPerson. adapter = new AllRoundAdapter <Person> (this, listPerson, Resource. layout. listViewItemLayout, 53 (x, y) => 54 {55 x. findViewById <TextView> (Resource.Id.txt Name ). text = y. name; 56 x. findViewById <TextView> (Resource.Id.txt Desc ). text = y. desc; 57 x. findViewById <ImageView> (Resource. id. imgHard ). setBackgroundResource (y. image); 58}); 59 60} 61} 62}

 

 

Please give me more corrections and take care of me! I feel like I am hiding my food.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.