1. 程式人生 > >Xamarin.Android 使用 SimpleAdapter 打造 ListView 萬能介面卡

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