Xamarin-Android_BaseAdapter 简单的复用

缘由:

  本人是一枚 小菜 初学Xamarin-Android  正在学习ListView 控件 发现这个控件的自定义布局 用的那叫一个爽字了得。

但是每次用ListView的时候都要 继承一下BaseAdapter 这就不爽了。好气啊,我这个人非常的懒只好寻求改良之法。

  写这篇博客在这里献丑甚是尴尬啊,还请园子里的诸位大咖多多指正。小弟不胜感激!【谢谢O(∩_∩)O哈!】

小弟学识浅薄仅仅做了以下的改良之处

  • 抽取泛型 因为基本上的是大同小异的 只是 类型的不同 如这次是 Person 类型 下次可能就是 Student 类型的区别而已 所以就想到了泛型
  • 将对 View 进行赋值的方法 使用委托进行替换。因为除了类型不同之外 也就这点不同了

下面是效果图 自己都感觉丑 丑哭了 (⊙o⊙)…

Xamarin-Android_BaseAdapter 简单的复用-LMLPHP


代码奉上

ListItem的布局代码

 <?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:minWidth="25px"
android:minHeight="25px">
<ImageView
android:layout_width="89.5dp"
android:layout_height="67.5dp"
android:id="@+id/imgHard" />
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="200dp"
android:layout_height="67.5dp"
android:id="@+id/linearLayout1">
<TextView
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="279.5dp"
android:layout_height="wrap_content"
android:id="@+id/txtName" />
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtDesc"
android:textSize="12dp" />
</LinearLayout>
<Button
android:text="查看信息"
android:layout_width="wrap_content"
android:layout_height="67.5dp"
android:id="@+id/button1" />
</LinearLayout>

主界面的代码

 <?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">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/itemsPerson"
android:divider="@android:color/white"
android:dividerHeight="3dp"
android:descendantFocusability="afterDescendants" />
</LinearLayout>

AllRoundBaseAdapter代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace AdapterStudy_20170301
{
public class AllRoundAdapter<T> : BaseAdapter<T>
{
#region 全局变量
/// <summary>
/// 上下文
/// </summary>
public Activity MyContext { get; set; } /// <summary>
/// 数据源
/// </summary>
public List<T> MyDataSource { get; set; } /// <summary>
/// Item 布局资源
/// </summary>
public int LayoutSource { get; set; } /// <summary>
/// 执行的委托
/// </summary>
Action<View, T> MyAction;
#endregion /// <summary>
/// 建立 AllRoundBaseAdapter
/// </summary>
/// <param name="actity">上下文</param>
/// <param name="listDataSource">数据源</param>
/// <param name="layoutSource">布局ID</param>
/// <param name="action">为布局赋值用到的方法</param>
public AllRoundAdapter(Activity actity, List<T> listDataSource, int layoutSource, Action<View, T> action)
{
this.MyContext = actity;
this.MyDataSource = listDataSource; this.LayoutSource = layoutSource; this.MyAction = action;
} public override T this[int position]
{
get
{
return MyDataSource[position];
}
} public override int Count
{
get
{
return this.MyDataSource.Count;
}
} public override long GetItemId(int position)
{
return position;
} public override View GetView(int position, View convertView, ViewGroup parent)
{
T t = this.MyDataSource[position]; View v = convertView;
if (v == null)
{
v = this.MyContext.LayoutInflater.Inflate(this.LayoutSource, null);
} if (this.MyAction != null)
{
this.MyAction.Invoke(v, t);
} return v;
}
}
}

MainActivity的代码

 using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Javax.Crypto; namespace AdapterStudy_20170301
{
[Activity(Label = "AdapterStudy_20170301", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{ private ListView lvPerson;
private List<Person> listPerson = new List<Person>(); protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); lvPerson = FindViewById<ListView>(Resource.Id.itemsPerson); //填充一些数据
this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
this.listPerson.Add(new Person("张飞", Resource.Drawable.person, "张翼德"));
this.listPerson.Add(new Person("刘备", Resource.Drawable.person, "刘玄德"));
this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长"));
this.listPerson.Add(new Person("关羽", Resource.Drawable.person, "关云长")); //设置表头表尾
View header = this.LayoutInflater.Inflate(Resource.Layout.HeaderLayout, null);
View footer = this.LayoutInflater.Inflate(Resource.Layout.FooterLayout, null); this.lvPerson.AddHeaderView(header);
this.lvPerson.AddFooterView(footer); //使用泛型的Adapter 进行赋值
lvPerson.Adapter = new AllRoundAdapter<Person>(this, listPerson, Resource.Layout.ListViewItemLayout,
(x, y) =>
{
x.FindViewById<TextView>(Resource.Id.txtName).Text = y.Name;
x.FindViewById<TextView>(Resource.Id.txtDesc).Text = y.Desc;
x.FindViewById<ImageView>(Resource.Id.imgHard).SetBackgroundResource(y.Image);
}); }
}
}

学识浅薄,请多多指正,多多关照! 感觉自己好菜 掩面而逃。

05-11 17:53