码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-12-28 23:39:55      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:enter   void   参数   targe   ast   his   ring   round   this   

第一步:创建 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 万能适配器

标签:enter   void   参数   targe   ast   his   ring   round   this   

原文地址:https://www.cnblogs.com/swjian/p/10193264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!