Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器
Posted 万石谷,粒粒积累;千丈布,根根织成
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 万能适配器的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法使用 Xamarin.Forms PCL 项目中的 Devexpress.Xamarin.Android.Charts
抽屉布局在 Xamarin.Android.Support.Core.UI 和 Xamarin.Android.Support.V4 中都存在
如何使用 DependencyService 和接口委托将 xamarin.android 特定功能的方法传递给 xamarin.forms?