Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器
第一步:创建 layout1.axml 来展示列表详细内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/img"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginLeft="20dp" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textColor="#000000"/>
</LinearLayout>
第二步:在 Main.axml 添加 ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
android:id="@+id/left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:text="DrawerLayout" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
第三步:创建 SimpleAdapter 数据适配器
using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System;
using Android.Runtime; namespace SimpleAdapterDemo
{
[Activity(Label = "SimpleAdapterDemo", MainLauncher = true)]
public class MainActivity : Activity
{
private ListView listview_leftMenu;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); listview_leftMenu = FindViewById<ListView>(Resource.Id.left_menu); //创建数据适配器
SimpleAdapter content = new SimpleAdapter(
this,
GetData(),
Resource.Layout.layout1,
new string[] { "img", "name" },
new int[] { Resource.Id.img, Resource.Id.name });
//把数据绑定到list_member 这个listview上 listview_leftMenu.Adapter = content; listview_leftMenu.ItemClick += (s, e) =>
{
Listview_leftMenu_ItemClick(e.Position);
};
} private void Listview_leftMenu_ItemClick(int position)
{
Toast.MakeText(this, list[position]["name"].ToString(), ToastLength.Short).Show();
} IList<IDictionary<string, Object>> list = new JavaList<IDictionary<string, Object>>();
private IList<IDictionary<string, Object>> GetData()
{
//map.put(参数名字,参数值) JavaDictionary<string, Object> map; map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息1");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息2");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息3");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息4");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息5");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息6");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息7");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息8");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息1");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息2");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息3");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息4");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息5");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息6");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息7");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); map = new JavaDictionary<string, Object>();
map.Add("name", "我的信息8");
map.Add("img", Resource.Drawable.icon_myinfo);
list.Add(map); return list;
}
}
}
效果:

最后附上源码地址:
链接: https://pan.baidu.com/s/1hs6NIEmqVXOq_bI9fjkl0Q
提取码: 67nk
Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器的更多相关文章
- android listview万能适配器
参考 Android 快速开发系列 打造万能的ListView GridView 适配器 Hongyang public class CommonViewHolder { private Con ...
- 打造android偷懒神器———RecyclerView的万能适配器
转载请注明出处谢谢:http://www.cnblogs.com/liushilin/p/5720926.html 很不好意思让大家久等了,本来昨天就应该写这个的,无奈公司昨天任务比较紧,所以没能按时 ...
- [置顶]
Xamarin android 调用Web Api(ListView使用远程数据)
xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...
- Xamarin.Android 入门之:Listview和adapter
一.引言 不管开发什么软件,列表的使用是必不可少的,而本章我们将学习如何使用Xamarin去实现它,以及如何使用自定义适配器.关于xamarin中listview的基础和适配器可以查看官网https: ...
- 打造android偷懒神器———ListView的万能适配器
如果你去做任何一个项目,我相信你都会跟我有一样的经历,最最普遍的就是列表显示ListView,当然,写N个自定义的适配器也是情理之中.虽说程序员本身就是搬砖,做这些枯燥无味的重复的事情也是理所当然,但 ...
- Android 快速开发系列 打造万能的ListView GridView 适配器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写 ...
- Android之ListView性能优化——一行代码绑定数据——万能适配器
如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有 ...
- 安卓开发笔记——打造万能适配器(Adapter)
为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...
- Android万能适配器Adapter-android学习之旅(74)
万能适配器的代码的github地址是https://github.com/fengsehng/CommonAdapter 万能适配器的代码的github地址是https://github.com/fe ...
随机推荐
- springboot无法加载oracle驱动终极解决
.ctrl+shirt+s .找到 Maven: com.oracle:ojdbc6: .找到classes 下的路径C:\Users\Administrator\.m2\repository\com ...
- 为什么禁止在 foreach 循环里进行元素的 remove/add 操作
首先看下边一个例子,展示了正确的做法和错误的错发: 这是为什么呢,具体原因下面进行详细说明: 1.foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数 ...
- AX_ClassTemplate
static void main(Args args) { THK_InterfaceDataExtract THK_InterfaceDataExtract; FormRun formRun = a ...
- spring Resource(转)
http://blog.csdn.net/u011225629/article/details/47143075
- Linux 网络编程 入门-常用函数
网络连接无外乎服务器和客户端两方面的编程. 对于服务器大致的流程是:1---调用socket函数创建套接字 2---调用bind函数分配IP地址和端口号 3---调用listsen函数将套接字转为可接 ...
- VUE 组件通信、传值
一.通过路由进行带参传值: 两个组件A和B,A组件通过query把orderId传递给B组件(触发事件可以是点击事件.钩子函数等) this.$router.push({path:'/componen ...
- MongoDB学习记录(三) - MongoDB的"增查改删"操作之"查"
查找使用的方法: db.collection.find() 查找所有文档 db.collection.find({})或者db.collection.find({}) 指定键值对 db.collect ...
- int main(int argc, char** argv) 以及CommandLineParser
参考链接: 关于int main(int argc, char** argv) http://blog.csdn.net/liuhuiyi/article/details/8239303 http:/ ...
- Java程序设计(第二版)复习 第二章
1.Java使用Unicode字符集,一般用16位二进制表示一个字符.且Java中午sizeof关键字,因为所有基本数据类型长度是确定的,不依赖执行环境. 2. Java变量在声明时并没有分配内存,真 ...
- 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)
传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...