Xamarin Android ListView Control uses

Source: Internet
Author: User

The ListView control is commonly used in projects, as shown in the following example:

Create the ListItem class, eg;

public class Coloritem    {public        string ColorName {get; set;}        public string Code {get; set;}        Public Android.Graphics.Color Color {get; set;}    }

Create Adapter class:

The adapter class needs to implement baseadapter and needs to be specified in GetView method to customize the ListItem page. Gets ListItem, in which the control value is given to the value.

 Public classColoradapter:baseadapter<coloritem>{List<ColorItem>items;        Activity context;  PublicColoradapter (Activity context, list<coloritem> items):Base()        {             This. Context =context;  This. Items =items; }         Public OverrideColoritem This[intPosition] =Items[position];  Public Override intCount =items.        Count;  Public Override LongGetitemid (intposition) {            returnposition; }         Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {            varitem =Items[position]; View View=Convertview; if(NULL==view) {View= Context. Layoutinflater.inflate (Resource.Layout.ListItem,NULL); } view. Findviewbyid<TextView> (Resource.Id.textView1). Text =item.            ColorName; View. Findviewbyid<TextView> (RESOURCE.ID.TEXTVIEW2). Text =item.            Code; View. Findviewbyid<ImageView>(Resource.Id.imageView1). SetBackgroundColor (item.            Color); returnview; }    }

Main.axml Add a ListView control. Background code is as follows;

 Public classmainactivity:activity {List<ColorItem> Coloritems =NewList<coloritem>(); ListView ListView=NULL; protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate); //Set Our view from the "main" layout resourceSetcontentview (Resource.Layout.Main); ListView= findviewbyid<listview>(Resource.Id.myListView); Coloritems.add (NewColoritem () {Color = Android.graphics.color.darkred,colorname="Dark Red", code="8b0000"}); Coloritems.add (NewColoritem () {Color = Android.Graphics.Color.SlateBlue, colorname ="Slate Blue", Code ="GA5ACD" }); Coloritems.add (NewColoritem () {Color = Android.Graphics.Color.ForestGreen, colorname ="Forest Green", Code ="228b22" }); Listview.adapter=NewColoradapter ( This, Coloritems); }    }

The results are as follows:

Xamarin Android ListView Control uses

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.