Xamarin-Android_BaseAdapter 简单的复用
Xamarin-Android_BaseAdapter 简单的复用
缘由:
本人是一枚 小菜 初学Xamarin-Android 正在学习ListView 控件 发现这个控件的自定义布局 用的那叫一个爽字了得。
但是每次用ListView的时候都要 继承一下BaseAdapter 这就不爽了。好气啊,我这个人非常的懒只好寻求改良之法。
写这篇博客在这里献丑甚是尴尬啊,还请园子里的诸位大咖多多指正。小弟不胜感激!【谢谢O(∩_∩)O哈!】
小弟学识浅薄仅仅做了以下的改良之处
- 抽取泛型 因为基本上的是大同小异的 只是 类型的不同 如这次是 Person 类型 下次可能就是 Student 类型的区别而已 所以就想到了泛型
- 将对 View 进行赋值的方法 使用委托进行替换。因为除了类型不同之外 也就这点不同了
下面是效果图 自己都感觉丑 丑哭了 (⊙o⊙)…

代码奉上
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);
}); }
}
}
学识浅薄,请多多指正,多多关照! 感觉自己好菜 掩面而逃。
Xamarin-Android_BaseAdapter 简单的复用的更多相关文章
- Xamarin + MvvmCross 简单事例 Part 2
MvvmCross 说起MvvmCross,要先说到Mvvm,Mvvm是Mvc框架的一种变形.对应的分别为Model.View和ViewModel层.三层之间的关系是这样的: Model层为数据层,实 ...
- 简单的复用accep
s = socket.socket() adress = ("192.168.15.102", 9999) s.bind(adress) s.listen() s.setblock ...
- Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍
原文 Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍 前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iO ...
- MaintainableCSS 《可维护性 CSS》 --- 复用篇
复用 通常,Harry Roberts 所说的 DRY (Don't repeat yourself) 经常被曲解成永远不要重复做通一件事. 但实际上这是不现实的,而且常常导致过分抽象,用太多的精力去 ...
- C#-Xamarin的Android项目开发(一)——创建项目
创建项目 使用Xamarin开发安卓项目,首先需要安装VS2017以上版本.因为VS2017以上的版本,可以直接创建Xamarin项目. 另外用Xamarin开发安卓项目,还需要使用Intel的CPU ...
- Xamarin移动开发的优点和缺点
在考虑iOS或Android应用程序开发时,我们大多数人会首先考虑Objective-C vs Swift和Java.作为本地技术堆栈,当涉及到iOS和Android应用程序开发时,它们自然是最常用的 ...
- Xamarin.Forms学习之初
微软的Build 2016结束的有段时间了,对于一个简单的小屌丝程序员--我来说,关注最大的无疑是Xamarin的免费(开源什么的让大神们上吧),内心激动啊.大会结束的周末我就迫不及待的安装了,然后. ...
- Visual Studio跨平台开发(2):Xamarin.iOS基本控制项介绍
前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iOS的专案目录架构以及基本控制项进行说明. 包含UIButton,UISlider,UISwitc ...
- 无辜的RAD(RAD是让你去创造和使用可复用的组件,不是让程序员“变白痴”)good
无辜的RAD 2005-3-21 说实话,RAD很无辜.从出生的那天其就被骂,天天被指着鼻子说“不就是拖个控件嘛”,就好像当年说学电脑“不就是插个鼠标嘛”.也怪程序员大都天性犯贱,就爱一遍又一便的写基 ...
随机推荐
- linux basic ------ shell
一般习惯把 shell 脚本语言和 shell 解释器统称为 shell,用 shell 脚本语言编写的程序简称脚本. shell 解释器 是用 c 语言写一个应用程序,它是用户使用 Unix / L ...
- linux command ------ source
source FileName 等效于. FileName,注 . 和 FileName 有空格 source命令也称为“点命令”,也就是一个点符号(.),作用是在当前bash环境下读取并执行File ...
- bzoj千题计划289:bzoj 2707: [SDOI2012]走迷宫
http://www.lydsy.com/JudgeOnline/problem.php?id=2707 dp[i] 表示从点i到终点的期望步数 dp[i]= Σ (dp[j]+1)/out[i] j ...
- UVA 12307 Smallest Enclosing Rectangle
https://vjudge.net/problem/UVA-12307 求覆盖所有点的最小矩形面积.周长 相当于求凸包的最小面积外接矩形.最小周长外接矩形 结论: 这个矩形一定有一条边和凸包上一条边 ...
- 转 -----那些年总也记不牢的IO
关于资源关闭: 一般情况下是:先打开的后关闭,后打开的先关闭 另一种情况:看依赖关系,如果流a依赖流b,应该先关闭流a,再关闭流b 例如处理流a依赖节点流b,应该先关闭处理流a,再关闭节点流b 当然完 ...
- [转载]AngularJS 开发者最常犯的 10 个错误
http://www.oschina.net/translate/top-10-mistakes-angularjs-developers-make
- 【转载】ssh(安全外壳协议)
http://baike.baidu.com/subview/16184/5909252.htm?fr=aladdin
- 数据结构笔记之跳表(SkipList)
一.跳表简述 跳表可以看做是一个带有索引的链表,在介绍跳表之前先看一下一个普通的链表,假如当前维护了一个有序的链表: 现在要在这个链表中查找128,因为事先不知道链表中数值的分布情况,我们只能从前到后 ...
- Django进阶之缓存和信号
一.缓存 简介 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者mem ...
- ASP.NET程序发布
详细流程请参考文章:https://www.cnblogs.com/wangjiming/p/6286045.html 主要补充个人操作过程中遇到的问题: 1)网站发布完成后,站点下没有aspnet_ ...