xamarin android——数据绑定到控件(四)
本文为通过自定义列表适配器定义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——数据绑定到控件(四)的更多相关文章
- xamarin android——数据绑定到控件(二)
本示例为通过媒体内容提供器获取本机中的图片显示在Gallery中. 活动中简单的初始化代码 private void InitGallery() { Gallery gallery = FindVie ...
- xamarin android——数据绑定到控件(三)
如果当前活动中,只存在一个listview视图,可以借助ListActivity快速的实现一个列表,即当前Activity继承ListActivity.在OnCreate方法中简单的两行代码,就可以创 ...
- xamarin android——数据绑定到控件(一)
mono for android 中光标由ICursor 接口标识,该接口公开了操作结果数据集的所有方法.光标的使用非常消耗系统资源,所以不使用时应该光比光标.可以通过StartManagingCur ...
- 安卓控件 仪表盘控件 柱状图控件 曲线控件 xamarin.android 分类器 瓶子控件 报警控件 水箱控件 进度条控件等
本篇博客主要介绍一个控件库,HslControls.dll 的界面,这个控件库支持winform,winform的参考另一篇文章:https://www.cnblogs.com/dathlin/p/1 ...
- Xamarin.android 重写axml控件
https://www.cnblogs.com/lonelyxmas/p/5632694.html <Laco: 用来用引指定的控件 android:layout_widt ...
- Xamarin.Android DatePickerFragment 日期控件
MainActivity 代码: public class MainActivity : Activity { TextView _dateDisplay; Button _dateSelectBut ...
- xamarin.android 给View控件 添加数字提醒效果-BadgeView
本文代码从java项目移植到.net项目 java开源项目:https://github.com/jgilfelt/android-viewbadger using System; using S ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
随机推荐
- 利用ajax获取到的网页源码不能执行js代码
今天觉得我的博客中加载腾讯微博的速度很慢,所以就想改写为js,本来以为直接新建一个页面,把获取函数移到新的页面中,原来的页面只要使用xmlhttp去GET一下,然后把div的innerhtml属性等于 ...
- 使用proguard混淆android代码
当前是有些工具比方apktool,dextojar等是能够对我们android安装包进行反编译,获得源代码的.为了降低被别人破解,导致源代码泄露,程序被别人盗代替码,等等.我们须要对代码进行混淆,an ...
- 玩转Bash脚本:test測试语句
总第1篇test就是測试的意思,经常使用在流程控制语句中作为条件.以下做一下介绍. 关于真值 与其它语言不同,Bash(包含其它Shell)中,是用0表示真,非0表示假的.之所以用0表示成功,而不是1 ...
- Computer Science Theory for the Information Age-1: 高维空间中的球体
高维空间中的球体 注:此系列随笔是我在阅读图灵奖获得者John Hopcroft的最新书籍<Computer Science Theory for the Information Age> ...
- PHP: 深入pack/unpack 字节序
http://my.oschina.net/goal/blog/195749?p=1 目录[-] 写在前面的话 什么是字节序 MSB和LSB 大端序 小端序 网络字节序 主机字节序 总结 pack/u ...
- restful php
http://bbs.phpchina.com/thread-228725-1-1.html http://www.cnblogs.com/artech/p/3506553.html http://w ...
- C#开发---利用特性自定义数据导出到Excel
网上C#导出Excel的方法有很多.但用来用去感觉不够自动化.于是花了点时间,利用特性做了个比较通用的导出方法.只需要根据实体类,自动导出想要的数据 1.在NuGet上安装Aspose.Cells或 ...
- Algernon's Noxious Emissions POJ1121 zoj1052
One of the greatest alchemists of the lower Middle Renaissance, Algernon da Vinci (one of Leonardo's ...
- 杂乱无章之javascript(二)
1.浏览器与事件事件通常是由浏览器所产生,不同的浏览器会产生的事件也有所不同,即使同一浏览器不同版本所产生的事件也有不同.以下为HTML4.01中的事件 2.error事件:它可以调用一个错误处理函数 ...
- 用终端直接在桌面生成text文件
简单的两行命令: cd Desktop/ 按回车 touch 888.text 按回车就会在桌面生成名称为888的text文件 用途:做demo的时候可以加一个说明文档进去,这样下次可以很方便的查看