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

xamarin android——数据绑定到控件(四)的更多相关文章

  1. xamarin android——数据绑定到控件(二)

    本示例为通过媒体内容提供器获取本机中的图片显示在Gallery中. 活动中简单的初始化代码 private void InitGallery() { Gallery gallery = FindVie ...

  2. xamarin android——数据绑定到控件(三)

    如果当前活动中,只存在一个listview视图,可以借助ListActivity快速的实现一个列表,即当前Activity继承ListActivity.在OnCreate方法中简单的两行代码,就可以创 ...

  3. xamarin android——数据绑定到控件(一)

    mono for android 中光标由ICursor 接口标识,该接口公开了操作结果数据集的所有方法.光标的使用非常消耗系统资源,所以不使用时应该光比光标.可以通过StartManagingCur ...

  4. 安卓控件 仪表盘控件 柱状图控件 曲线控件 xamarin.android 分类器 瓶子控件 报警控件 水箱控件 进度条控件等

    本篇博客主要介绍一个控件库,HslControls.dll 的界面,这个控件库支持winform,winform的参考另一篇文章:https://www.cnblogs.com/dathlin/p/1 ...

  5. Xamarin.android 重写axml控件

    https://www.cnblogs.com/lonelyxmas/p/5632694.html <Laco: 用来用引指定的控件            android:layout_widt ...

  6. Xamarin.Android DatePickerFragment 日期控件

    MainActivity 代码: public class MainActivity : Activity { TextView _dateDisplay; Button _dateSelectBut ...

  7. xamarin.android 给View控件 添加数字提醒效果-BadgeView

    本文代码从java项目移植到.net项目   java开源项目:https://github.com/jgilfelt/android-viewbadger using System; using S ...

  8. 五、Android学习第四天补充——Android的常用控件(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...

  9. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

随机推荐

  1. STL中heap算法(堆算法)

     ①push_heap算法 以下是push_heap算法的实现细节.该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端. template ...

  2. DataTable转换为List<Model>的通用类

    在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有模型类型检索方便. 所以很多人都是按照以下方式做的: // 获得查询结果DataTable dt = DbHelper ...

  3. iOS开发——实用篇Swift篇&保存图片到相册

    保存图片到相册 最近在深入的学习关于swift相关技术,虽然海做不出什么好的东西,但是感觉收获不少,相信总有一样能用到,所以就总结了一下,希望大家喜欢! 1.OC中的写法 在OC中,我们需要保存图片到 ...

  4. Linux系统如何平滑生效NAT-BUGFIX

    在< Linux系统如何平滑生效NAT>中,代码有两处问题.这只是目前发现的,没有发现的还有很多很多,这就是我为何不一开始把代码搞复杂的原因. 1.一个bug附带一个优化: 注意以下的代码 ...

  5. Intellij IDEA 使用Debug模式运行非常慢

    今天在用Debug的时候,idea运行非常慢,搜了一下有人说: 自己检查发现果然如此,把在方法前的断点去掉(移到方法体内),就正常了.

  6. oracle数据库元数据SQL查询

    oracle数据库经典SQL查询 .查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from ...

  7. Tick and Tick

    The three hands of the clock are rotating every second and meeting each other many times everyday. F ...

  8. Android快速开发框架ZBLibrary源码分享

    坐标标准库ZBLibrary,是一个MVP架构的Android快速开发框架,提供一套开发标准(UI,Data,Listener)以及模板和工具类并规范代码. 封装层级少,简单高效兼容性好.Androi ...

  9. 火狐restclient

    RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...

  10. thymleaf分支用法

    <div th:switch="${user.role}"> <p th:case="'admin'">User is an admin ...