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;
        }
    }
}

效果:

Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器

最后附上源码地址:

  链接: https://pan.baidu.com/s/1hs6NIEmqVXOq_bI9fjkl0Q

  提取码: 67nk

Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器

上一篇:mapper.xml文件中标签没有提示的解决


下一篇:virtualenv and virtualenvwrapper