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 ...
随机推荐
- Flex4之元数据标签使用
Flex 元数据标签 1.[ArrayElementType] 使用ArrayElementType元数据标签可以让你定义数组元素的数据类型. 程序代码: [ArrayElementType(&quo ...
- Excel转换成PDF
public class Office2Pdf { public bool DOCConvertToPDF(string sourcePath, string targetPath) { //Stre ...
- 关于使用NotificationComat导致android2.3及以下版本无法显示自定义布局的解决方法.
大伙都知道 android-support-v4为我们提供了很多兼容的解决方案, 其中就有关于通知栏的. NotificationCompat, 顺利成章操刀显示通知. eg: Intent inte ...
- super 和this的用法
class Person { public static void prt(String s) { System.out.println(s); // 打印出来结果 } Person() { prt( ...
- myEclipse6.5与数据库(SQL Server2008)连接遇到的问题(自己总结的干货)<用SSH框架的时候,用servlet+javabean+jsp的时候>
昨天因为学习SSH框架的搭建,时隔一年又重新遇到了myEclipse连接数据库的问题.废话不多说,上干货 (以下全部按照我遇到的问题的顺序,也就是没有顺序,就是任性) 请注意:这是在myEclipse ...
- [改善Java代码]不要覆写静态方法
建议33: 不要覆写静态方法 我们知道在Java中可以通过覆写(Override)来增强或减弱父类的方法和行为,但覆写是针对非静态方法(也叫做实例方法,只有生成实例才能调用的方法)的,不能针对静态方法 ...
- POJ 1159 - Palindrome (LCS, 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 55018 Accepted: 19024 Desc ...
- sharepoint 中用自带的download.aspx实现文件的下载,中文文件名编码的问题
]中的路径绑定的是下载路径,用到了sharepoint中自带的download.aspx下载页面,只要将文件的URL赋值给sourceurl即可,但是我前台用的是<a>标签的href来导向 ...
- css 盒模型相关样式
话不多说,一切还是从最基础的说起. 盒的类型 1.盒的基本类型 在css中,用display定义盒的类型,一般分为block类型与inline类型. 例如div属于block类型,span属于in ...
- javascript+dom 做javascript图片库
废话不多说 直接贴代码 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...