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 简单的复用的更多相关文章

  1. Xamarin + MvvmCross 简单事例 Part 2

    MvvmCross 说起MvvmCross,要先说到Mvvm,Mvvm是Mvc框架的一种变形.对应的分别为Model.View和ViewModel层.三层之间的关系是这样的: Model层为数据层,实 ...

  2. 简单的复用accep

    s = socket.socket() adress = ("192.168.15.102", 9999) s.bind(adress) s.listen() s.setblock ...

  3. Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍

    原文 Visual Studio跨平台开发实战(2) - Xamarin.iOS基本控制项介绍 前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iO ...

  4. MaintainableCSS 《可维护性 CSS》 --- 复用篇

    复用 通常,Harry Roberts 所说的 DRY (Don't repeat yourself) 经常被曲解成永远不要重复做通一件事. 但实际上这是不现实的,而且常常导致过分抽象,用太多的精力去 ...

  5. C#-Xamarin的Android项目开发(一)——创建项目

    创建项目 使用Xamarin开发安卓项目,首先需要安装VS2017以上版本.因为VS2017以上的版本,可以直接创建Xamarin项目. 另外用Xamarin开发安卓项目,还需要使用Intel的CPU ...

  6. Xamarin移动开发的优点和缺点

    在考虑iOS或Android应用程序开发时,我们大多数人会首先考虑Objective-C vs Swift和Java.作为本地技术堆栈,当涉及到iOS和Android应用程序开发时,它们自然是最常用的 ...

  7. Xamarin.Forms学习之初

    微软的Build 2016结束的有段时间了,对于一个简单的小屌丝程序员--我来说,关注最大的无疑是Xamarin的免费(开源什么的让大神们上吧),内心激动啊.大会结束的周末我就迫不及待的安装了,然后. ...

  8. Visual Studio跨平台开发(2):Xamarin.iOS基本控制项介绍

    前言 在上一篇文章中, 我们介绍了Xamarin 以及简单的HelloWorld范例, 这次我们针对iOS的专案目录架构以及基本控制项进行说明. 包含UIButton,UISlider,UISwitc ...

  9. 无辜的RAD(RAD是让你去创造和使用可复用的组件,不是让程序员“变白痴”)good

    无辜的RAD 2005-3-21 说实话,RAD很无辜.从出生的那天其就被骂,天天被指着鼻子说“不就是拖个控件嘛”,就好像当年说学电脑“不就是插个鼠标嘛”.也怪程序员大都天性犯贱,就爱一遍又一便的写基 ...

随机推荐

  1. Zookeeper可视化工具

    zkui 简介 zkui它提供了一个管理界面,可以针对zookeepr的节点值进行CRUD操作,同时也提供了安全认证. 下载安装 项目地址 下载 $ git clone https://github. ...

  2. jdk1.8中获取项目绝对路径和项目路径

    request.getSession().getServletContext().getRealPath("")  获取项目的绝对路径,含着项目的名称. request.getSe ...

  3. ngx_lua_API 指令详解(六)ngx.thread.spawn、ngx.thread.wait、ngx.thread.kill介绍

    摘要:通过lua-nginx-module中的ngx.thread同时执行多个任务. ngx_lua中访问多个第三方服务 ngx_lua中提供了ngx.socket API,可以方便的访问第三方网络服 ...

  4. html5 canvas在线文本第二步设置(字体边框)等我全部写完,我会写在页面底部

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 理解 Memory barrier(内存屏障)【转】

    转自:http://name5566.com/4535.html 参考文献列表:http://en.wikipedia.org/wiki/Memory_barrierhttp://en.wikiped ...

  6. MCM/ICM2018美国大学生数学建模大赛D题翻译

    MCM/ICM2018美国大学生数学建模大赛D题翻译 2018年ICM问题D: 非使用汽油并在使用电力行驶的汽车(电量非空的) 由于环境和经济的原因,全球都在减少使用化石燃料,包括汽车汽油. 无论是受 ...

  7. activiti helloworld

    activiti helloworld activiti的入门实践文章,重点在于动手做,要解决的是怎么做的问题.只有知道了怎么做后,才具有实际动手能力,才算对这门技术有一个初步掌握:至于更深入细化的知 ...

  8. javaweb笔记七

    过滤器:是一个web中间组件,用于拦截从客户端发送给服务器的请求和响应.当客户端向服务器发出请求时,服务器会查看是否有过滤器和该请求匹配,如果有,则交给过滤器执行,业务操作后,可以将请求继续向目标资源 ...

  9. Java编程的逻辑 (72) - 显式条件

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  10. .NetCore Cap 结合 RabbitMQ 实现消息订阅

    开源分布式消息框架 Cap 可以在GitHub上拉也可以通过nuget添加 上一篇博文写了 Windows RabbitMQ的安装使用 Cap支持事务,通过捕获数据库上下文连接对象实现 消息事务,消息 ...