如果你项目一直用系统给你封装的BaseAdapter的话,那么我只想说,你不累么?
代码繁多还要写数据缓存,还不如自己动手写一个模板吧,这样后面项目就可以直接套用了,编写和执行效率大大提升啊。
BaseAdapter.java
001 |
import java.util.ArrayList;
|
002 |
import java.util.Collection;
|
003 |
import java.util.Collections;
|
004 |
import java.util.Comparator;
|
005 |
import java.util.List;
|
006 |
007 |
import android.content.Context;
|
008 |
import android.content.res.Resources;
|
009 |
import android.view.LayoutInflater;
|
010 |
import android.view.View;
|
011 |
import android.view.ViewGroup;
|
012 |
013 |
014 |
/** |
015 |
* 实现这个适配器模板会少很多代码
|
016 |
* @author leehom
|
017 |
* @param <T>
|
018 |
*/
|
019 |
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
|
020 |
021 |
protected List<T> list;
|
022 |
protected Context context;
|
023 |
protected LayoutInflater inflater;
|
024 |
private int layoutID;
|
025 |
026 |
public BaseAdapter(Context context, T[] ts, int layoutID) {
|
027 |
super ();
|
028 |
this .context = context;
|
029 |
this .list = new ArrayList<T>();
|
030 |
this .layoutID = layoutID;
|
031 |
Collections.addAll(list, ts);
|
032 |
this .inflater = LayoutInflater.from(context);
|
033 |
}
|
034 |
035 |
public BaseAdapter(Context context, List<T> list, int layoutID) {
|
036 |
super ();
|
037 |
this .layoutID = layoutID;
|
038 |
this .context = context;
|
039 |
this .list = new ArrayList<T>();
|
040 |
this .list.addAll(list);
|
041 |
this .inflater = LayoutInflater.from(context);
|
042 |
}
|
043 |
044 |
public Resources getResources() {
|
045 |
return context.getResources();
|
046 |
}
|
047 |
048 |
public synchronized void add(T object) {
|
049 |
list.add(object);
|
050 |
}
|
051 |
052 |
public synchronized void addAll(Collection<? extends T> collection) {
|
053 |
list.addAll(collection);
|
054 |
}
|
055 |
056 |
public synchronized void remove(T object) {
|
057 |
list.remove(object);
|
058 |
}
|
059 |
060 |
public synchronized void insert(T object, int index) {
|
061 |
list.add(index, object);
|
062 |
}
|
063 |
064 |
public synchronized void clear() {
|
065 |
list.clear();
|
066 |
}
|
067 |
068 |
public synchronized void sort(Comparator<? super T> comparator) {
|
069 |
Collections.sort(list, comparator);
|
070 |
}
|
071 |
072 |
@Override
|
073 |
public T getItem( int position) {
|
074 |
// TODO Auto-generated method stub
|
075 |
return list.get(position);
|
076 |
}
|
077 |
078 |
@Override
|
079 |
public int getCount() {
|
080 |
// TODO Auto-generated method stub
|
081 |
return list.size();
|
082 |
}
|
083 |
084 |
@Override
|
085 |
public long getItemId( int position) {
|
086 |
// TODO Auto-generated method stub
|
087 |
return position;
|
088 |
}
|
089 |
090 |
@Override
|
091 |
public View getView( int position, View convertView, ViewGroup parent) {
|
092 |
// TODO Auto-generated method stub
|
093 |
ViewHolder holder = null ;
|
094 |
if (convertView == null ) {
|
095 |
convertView = inflater.inflate(layoutID, null );
|
096 |
holder = new ViewHolder(convertView);
|
097 |
initView(holder);
|
098 |
convertView.setTag(holder);
|
099 |
} else {
|
100 |
holder = (ViewHolder) convertView.getTag();
|
101 |
}
|
102 |
setViewValue(holder, position);
|
103 |
return convertView;
|
104 |
}
|
105 |
106 |
public List<T> getList() {
|
107 |
return list;
|
108 |
}
|
109 |
|
110 |
/**
|
111 |
* 向ViewHolder类里面添加View
|
112 |
* @param holder
|
113 |
*/
|
114 |
public abstract void initView(ViewHolder holder);
|
115 |
116 |
/**
|
117 |
* 从ViewHolder获取对应ID的View设置其值
|
118 |
* @param holder
|
119 |
* @param t 数据对象
|
120 |
*/
|
121 |
public abstract void setViewValue(ViewHolder holder, int position);
|
122 |
123 |
} |
用的时候也是继承它,然后只需要初始化ID和设置Value就行了。例:
01 |
import java.lang.reflect.Field;
|
02 |
import java.lang.reflect.InvocationTargetException;
|
03 |
import java.lang.reflect.Method;
|
04 |
import java.util.ArrayList;
|
05 |
06 |
import android.annotation.SuppressLint;
|
07 |
import android.content.Context;
|
08 |
09 |
import com.fenjin.app.humiture.R;
|
10 |
import com.fenjin.app.item.DefaultItem;
|
11 |
12 |
@SuppressLint ( "DefaultLocale" )
|
13 |
public class DefaultAdapter extends BaseAdapter<DefaultItem> {
|
14 |
15 |
public DefaultAdapter(Context context, Object object) {
|
16 |
super (context, new ArrayList<DefaultItem>(),
|
17 |
R.layout.default_list_item_layout);
|
18 |
// TODO Auto-generated constructor stub
|
19 |
try {
|
20 |
Field[] fields = object.getClass().getDeclaredFields();
|
21 |
for (Field field : fields) {
|
22 |
String lable = field.getName();
|
23 |
String firstWord = lable.substring( 0 , 1 ).toUpperCase();
|
24 |
StringBuffer buffer = new StringBuffer( "get" );
|
25 |
buffer.append(firstWord);
|
26 |
buffer.append(lable.substring( 1 ));
|
27 |
String name = buffer.toString();
|
28 |
Method method = object.getClass().getDeclaredMethod(name);
|
29 |
String value = method.invoke(object).toString();
|
30 |
DefaultItem item = new DefaultItem(lable, value);
|
31 |
add(item);
|
32 |
}
|
33 |
} catch (IllegalArgumentException e) {
|
34 |
// TODO Auto-generated catch block
|
35 |
e.printStackTrace();
|
36 |
} catch (NoSuchMethodException e) {
|
37 |
// TODO Auto-generated catch block
|
38 |
e.printStackTrace();
|
39 |
} catch (IllegalAccessException e) {
|
40 |
// TODO Auto-generated catch block
|
41 |
e.printStackTrace();
|
42 |
} catch (InvocationTargetException e) {
|
43 |
// TODO Auto-generated catch block
|
44 |
e.printStackTrace();
|
45 |
}
|
46 |
}
|
47 |
48 |
@Override
|
49 |
public void initView(ViewHolder holder) {
|
50 |
// TODO Auto-generated method stub
|
51 |
holder.addView(R.id.default_lable);
|
52 |
holder.addView(R.id.default_value);
|
53 |
}
|
54 |
55 |
@Override
|
56 |
public void setViewValue(ViewHolder holder, int position) {
|
57 |
// TODO Auto-generated method stub
|
58 |
DefaultItem item = getItem(position);
|
59 |
holder.getTextView(R.id.default_lable).setText(item.getLable());
|
60 |
holder.getTextView(R.id.default_value).setText(item.getValue());
|
61 |
}
|
62 |
63 |
@Override
|
64 |
public boolean isEnabled( int position) {
|
65 |
// TODO Auto-generated method stub
|
66 |
return false ;
|
67 |
}
|
68 |
69 |
} |
javabean代码
01 |
public class DefaultItem {
|
02 |
03 |
String lable;
|
04 |
String value;
|
05 |
06 |
public DefaultItem(String lable, String value) {
|
07 |
super ();
|
08 |
this .lable = lable;
|
09 |
this .value = value;
|
10 |
}
|
11 |
12 |
public String getLable() {
|
13 |
return lable;
|
14 |
}
|
15 |
16 |
public void setLable(String lable) {
|
17 |
this .lable = lable;
|
18 |
}
|
19 |
20 |
public String getValue() {
|
21 |
return value;
|
22 |
}
|
23 |
24 |
public void setValue(String value) {
|
25 |
this .value = value;
|
26 |
}
|
27 |
28 |
} |
xml代码
01 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
|
03 |
android:layout_width = "match_parent"
|
04 |
android:layout_height = "match_parent"
|
05 |
android:orientation = "horizontal"
|
06 |
android:padding = "@dimen/margin_10" >
|
07 |
08 |
< TextView
|
09 |
android:id = "@+id/default_lable"
|
10 |
android:layout_width = "wrap_content"
|
11 |
android:layout_height = "wrap_content" />
|
12 |
13 |
< TextView
|
14 |
android:layout_width = "wrap_content"
|
15 |
android:layout_height = "wrap_content"
|
16 |
android:text = ":" />
|
17 |
18 |
< TextView
|
19 |
android:id = "@+id/default_value"
|
20 |
android:layout_width = "wrap_content"
|
21 |
android:layout_height = "wrap_content" />
|
22 |
23 |
</ LinearLayout >
|
每次实现的代码仅仅这么点
01 |
@Override |
02 |
public void initView(ViewHolder holder) {
|
03 |
// TODO Auto-generated method stub
|
04 |
holder.addView(R.id.default_lable);
|
05 |
holder.addView(R.id.default_value);
|
06 |
} |
07 |
08 |
@Override |
09 |
public void setViewValue(ViewHolder holder, int position) {
|
10 |
// TODO Auto-generated method stub
|
11 |
DefaultItem item = getItem(position);
|
12 |
holder.getTextView(R.id.default_lable).setText(item.getLable());
|
13 |
holder.getTextView(R.id.default_value).setText(item.getValue());
|
14 |
} |
是不是非常不错呢?
- 本文固定链接: http://www.ithtw.com/1300.html
- leehom 2015年03月06日 于 IT十万为什么 发表
相关推荐
安装composer require dandjo/object-adapter目的其目的是在使用诸如Twig之类的模板引擎背后的复杂对象时,使生活变得更轻松。 它提供了一些语法功能,例如编写getSomeProperty getter( getSomeProperty )并通过...
FooterLoaderAdapter是一个抽象模板类,它扩展了RecylcerView.Adapter<RecyclerView> 。 它具有以下方法,用户可以方便地使用它们来创建自己的Recycler Adapter。 public abstract long getYourItemId(int position...
目录编写其他测试 介绍欢迎使用索引交换合作伙伴认证流程! 在下面,您将找到完成认证过程所需的一切,并成为Header Tag Wrapper的一部分! 储存库结构README.md这是主要的文档文件,其中应包含完成认证过程所需的...
目录编写其他测试 介绍欢迎使用索引交换合作伙伴认证流程! 在下面,您将找到完成认证过程所需的一切,并成为Header Tag Wrapper的一部分! 储存库结构README.md这是主要的文档文件,其中应包含完成认证过程所需的...
编写其他测试 介绍 欢迎使用索引交换合作伙伴认证流程! 在下面,您将找到完成认证过程所需的一切,并成为Header Tag Wrapper的一部分! 储存库结构 README.md这是主要的文档文件,其中应包含完成认证过程所需的...
一个可以复用项目模板框架,搭建了一个项目框架,编写了一些最基础的、项目常用的一些代码包括activity基类,adapter基类
template_mate_programmingSome useful code programed with template technology under C++11 standard.Require: C++11 Supportadapter.h:Adapter. It can include the reference of variety different type into ...
:grinning_face:点击查看电子书,体验更好 :backhand_index_pointing_right: 创建型 (Creational)Java Kotlin结构型(Structural)适配器模式(Adapter Class/Object Pattern) Java桥接模式(Bridge Pattern) ...
在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在源码中的使用,每种设计模式都举了一个简单的小例子来进行实现,并加以注释 包名解释 一、DesignPattern 1.1 创建型模式 ...
说明:panel是你编写的打印面板JSON对象,可以在hiprint官网自己进行配置生成。推荐使用方式推荐使用的业务Print组件结构|-----Print|--------index.js|--------adapter.js|--------panel.js文件介绍:Print 文件夹...
着眼于一些特定的API设计模式和惯用法,包括Pimpl惯用法、单例(singleton)、工厂方法(factory method)、代理(proxy)、适配器(adapter)、外观(facade)模式以及观察者(observer)模式。 第4章:设计。讨论...
确定程序的功能 ---> 界面设计 ---> 寻找素材 ---> 开始编写程序 ---> 优化修改程序。 2、程序简要分析 本程序包括十一个类和一个声音文件和图片文件,其中十一个类中包括:主类(PanelFrame),里面的main方法是...
设计模式的主要类型: 创建模式(factory, constructor, singleton, ...) 在必须创建许多不同类型的许多不同对象的情况下,提高代码的灵活性和可重用性结构模式(adapter, facade, proxy, ...) 行为模式(observer, ...
我 们都知道在Git 中想要过滤掉一些不想提交的文件,可以把相应的文件添加到.gitignore 中,而.gitignore 这个Android Studio 插件根据不同的语言来选择模板,就不用自己在费事添加一些文件了,而且还有自动补全功能...
所有的代码示例均采用c#语言编写,但这些概念可以非常轻松地用于vb.net。 第Ⅰ部分 模式与设计原则 第1章 成功应用程序的模式 1.1 设计模式释义 1.1.1 起源 1.1.2 必要性 1.1.3 有效性 1.1.4 局限性 1.2 ...
7.8.5. 编写自己的助手 7.9. 响应对象 7.9.1. 用法 7.9.2. 处理消息头 7.9.3. 命名片段 7.9.4. 在响应对象中测试异常 7.9.5. 子类化响应对象 7.10. 插件 7.10.1. 简介 7.10.2. 编写插件 7.10.3. 使用插件 ...
第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、State、Proxy。最后三章比较...
当然还有一些提示和鼓励:第一次阅读此书时你可能不会完全理解它,但不必着急,我们在起初编写这本书时也没有完全理解它们!请记住,这不是一本读完一遍就可以束之高阁的书。我们希望你在软件设计过程中反复参阅...
为了生成适应于不同屏幕大小、系统版本的图片,我使用 bash 编写了一系列脚本,并实现了一个通用的模板化 SVG 格式,详情见 MaterialColdStart 和 AndroidSVGScripts。 经过自定义窗口背景和其他优化,应用在手机上...
如何在Asp.Net1.1中实现页面模板(所谓的MasterPage技术) Tool Tip 示例(FILTER版) Tool Tip示例 (htc版) 一个.net发送HTTP数据实体的类 按键跳转以及按Enter以不同参数提交,及其他感应事件 动态控制Page页的...