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 ...
随机推荐
- Java中的Integer和int
Java中的Integer是引用类型,而int是基本类型.Integer是int的包装器类型. java中的基本类型有布尔类型boolean;字符类型char;整数类型byte,int,long,sh ...
- spring事务相关
在 SPRING 中一共定义了六种事务传播属性 PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPOR ...
- Linux无法正常连接服务器,无法连接上 127.0.0.1:8989 (127.0.0.1)。 - connect (111: 拒绝连接)
最近修改了下电脑的hosts文件,电脑就突然不能连接下载更新的服务器了,但是浏览器还能正常上网,这让我很是难受啊!!! 错误现象如下: 错误:1 http://archive.ubuntukylin. ...
- guns开源项目数据库切换为oracle
本次使用oracle版本 11.2.0.1.0 1.guns-core 修改pom.xml 文件引入oracle驱动 <dependency> <groupId>com.ora ...
- affiliate的使用方式
什么是affiliate https://www.zhihu.com/question/24262490 通俗的理解就是,我们寻找合作伙伴,让合作伙伴帮忙做宣传,我们会根据他们的宣传力度发放相关的奖励 ...
- python3 爬取搜狗微信的文章
目标地址:http://weixin.sogou.com/weixin? 这个地址是搜狗微信的文章搜索,可以搜索到微信的文章,而我们目标就是这些文章内容 这个url经过测试,当我们没登陆微信只能看到1 ...
- CentOS 7.6下解决登录MySQL时,ERROR 1045 (28000): Access denied for user root@localhost (using password: YES
https://blog.csdn.net/sinat_35406909/article/details/79763782
- 线程中的setDaemon方法
setDaemon方法必须在start方法前定义.t1.setDaemon(True),该语句的意思是:将主线程A设置为子线程t1的守护线程.也就是在执行程序时,t1会随着主线程A的退出而退出,不论t ...
- pycharm clion phpstorn全家桶激活码(可以用到2019年4月)
SXXI7H41YN-eyJsaWNlbnNlSWQiOiJTWFhJN0g0MVlOIiwibGljZW5zZWVOYW1lIjoicGF5bmUgd2FuZyIsImFzc2lnbmVlTmFtZ ...
- Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...