本文为通过自定义列表适配器定义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所对应的视图。