xamarin android——資料繫結到控制項(四)

來源:互聯網
上載者:User

標籤:des   android   c   style   class   blog   

本文為通過自訂欄表適配器定義ListView,以上文為基礎,基於ListActivity。

定義清單項目布局,包含一個圖片顯示,標題和描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="80dip">    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <LinearLayout        android:orientation="vertical"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip">        <TextView            android:id="@+id/title"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/description"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

為了使視圖顯示資料,必須自訂配接器。ListView每一個清單項目顯示的是自訂的Model資料,選擇繼承BaseAdapter<T>,T為自訂Model

model類內容

public class Model    {        public string Name {            get;            set;        }        public string Description {            get;            set;        }        public int Image {            get;            set;        }    }

適配器類需要實現兩個方法和兩個屬性:

Count屬性、T類型的this 屬性、GetItemId方法、GetView方法。

this屬性返回指定索引對應的對象資料。

自訂ModelListAdapter代碼:

class ModelListAdapter :BaseAdapter<Model>    {        public List<Model> Models {            get;            set;        }        public ModelListAdapter(List<Model> models)        {            Models = models;        }        #region implemented abstract members of BaseAdapter        public override long GetItemId (int position)        {            return position;        }        public override View GetView (int position, View convertView, ViewGroup parent)        {
       //從資料來源中擷取當前位置對應的對象 var item = Models [position];       //避免重複建立和銷毀清單項目視圖 if (convertView==null) { LayoutInflater inflater = Application.Context.GetSystemService ("layout_inflater") as LayoutInflater; convertView = inflater.Inflate (Resource.Layout.CustomItem,null); } var image = convertView.FindViewById<ImageView> (Resource.Id.image); var title = convertView.FindViewById<TextView> (Resource.Id.title); var description = convertView.FindViewById<TextView> (Resource.Id.description); image.SetImageResource (item.Image); title.Text = item.Name; description.Text = item.Description; return convertView; } public override int Count { get { return Models.Count; } } #endregion #region implemented abstract members of BaseAdapter public override Model this [int index] { get { return Models [index]; } } #endregion }

最後一步,將LiatActivity的ListAdapter賦值為我們自訂的適配器

var models = new List<Model>{                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon},                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon},                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon},                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon},                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon},                new Model(){ Name="Name",Description="Description",Image = Resource.Drawable.Icon}            };            this.ListAdapter = new ModelListAdapter (models);

自訂配接器,我們可以進行更靈活的處理。在GetView方法中執行更多的操作,如果只是進行簡單的自訂欄表樣式,可以通過SimpleAdapter快速完成操作。

IList<IDictionary<string,object>> items = new JavaList<IDictionary<string,object>> ();            var item1 = new JavaDictionary<string,object> ();            item1.Add ("name","show name");            item1.Add("description","description");            item1.Add ("image",Resource.Drawable.Icon);            var item2 = new JavaDictionary<string,object> ();            item2.Add ("name","show name");            item2.Add("description","description");            item2.Add ("image",Resource.Drawable.Icon);            items.Add (item1);            items.Add (item2);            this.ListAdapter = new SimpleAdapter (this,items,Resource.Layout.CustomItem,                new string[]{"name","description","image"},new int[]{Resource.Id.title,Resource.Id.description,Resource.Id.image});

items為定義的資料來源k,定義SimpleAdapter傳入參數即可。其中string[] 中定義的值必須等於Dictionary中key的值,string[] 和int[] 兩個參數表示from string[] to int[]。即每個string[]中的值作為key,取得Dictionary中的值,賦值給int[] 中id所對應的視圖。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.