如果你项目一直用系统给你封装的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;
|
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;
|
019 |
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
|
021 |
????protected List<T> list;
|
022 |
????protected Context context;
|
023 |
????protected LayoutInflater inflater;
|
024 |
????private int layoutID;
|
026 |
????public BaseAdapter(Context context, T[] ts, int layoutID) {
|
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);
|
035 |
????public BaseAdapter(Context context, List<T> list, int layoutID) {
|
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);
|
044 |
????public Resources getResources() {
|
045 |
????????return context.getResources();
|
048 |
????public synchronized void add(T object) {
|
049 |
????????list.add(object);
|
052 |
????public synchronized void addAll(Collection<? extends T> collection) {
|
053 |
????????list.addAll(collection);
|
056 |
????public synchronized void remove(T object) {
|
057 |
????????list.remove(object);
|
060 |
????public synchronized void insert(T object, int index) {
|
061 |
????????list.add(index, object);
|
064 |
????public synchronized void clear() {
|
065 |
????????list.clear();
|
068 |
????public synchronized void sort(Comparator<? super T> comparator) {
|
069 |
????????Collections.sort(list, comparator);
|
073 |
????public T getItem(int position) {
|
075 |
????????return list.get(position);
|
079 |
????public int getCount() {
|
081 |
????????return list.size();
|
085 |
????public long getItemId(int position) {
|
087 |
????????return position;
|
091 |
????public View getView(int position, View convertView, ViewGroup parent) {
|
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);
|
100 |
????????????holder = (ViewHolder) convertView.getTag();
|
102 |
????????setViewValue(holder, position);
|
103 |
????????return convertView;
|
106 |
????public List<T> getList() {
|
111 |
?????* 向ViewHolder类里面添加View
|
114 |
????public abstract void initView(ViewHolder holder);
|
117 |
?????* 从ViewHolder获取对应ID的View设置其值
|
121 |
????public abstract void setViewValue(ViewHolder holder, int position);
|
用的时候也是继承它,然后只需要初始化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;
|
06 |
import android.annotation.SuppressLint;
|
07 |
import android.content.Context;
|
09 |
import com.fenjin.app.humiture.R;
|
10 |
import com.fenjin.app.item.DefaultItem;
|
12 |
@SuppressLint("DefaultLocale")
|
13 |
public class DefaultAdapter extends BaseAdapter<DefaultItem> {
|
15 |
????public DefaultAdapter(Context context, Object object) {
|
16 |
????????super(context, new ArrayList<DefaultItem>(),
|
17 |
????????????????R.layout.default_list_item_layout);
|
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);
|
33 |
????????} catch (IllegalArgumentException e) {
|
35 |
????????????e.printStackTrace();
|
36 |
????????} catch (NoSuchMethodException e) {
|
38 |
????????????e.printStackTrace();
|
39 |
????????} catch (IllegalAccessException e) {
|
41 |
????????????e.printStackTrace();
|
42 |
????????} catch (InvocationTargetException e) {
|
44 |
????????????e.printStackTrace();
|
49 |
????public void initView(ViewHolder holder) {
|
51 |
????????holder.addView(R.id.default_lable);
|
52 |
????????holder.addView(R.id.default_value);
|
56 |
????public void setViewValue(ViewHolder holder, int position) {
|
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());
|
64 |
????public boolean isEnabled(int position) {
|
javabean代码
01 |
public class DefaultItem {
|
06 |
????public DefaultItem(String lable, String value) {
|
08 |
????????this.lable = lable;
|
09 |
????????this.value = value;
|
12 |
????public String getLable() {
|
16 |
????public void setLable(String lable) {
|
17 |
????????this.lable = lable;
|
20 |
????public String getValue() {
|
24 |
????public void setValue(String value) {
|
25 |
????????this.value = value;
|
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" >
|
09 |
????????android:id="@+id/default_lable"
|
10 |
????????android:layout_width="wrap_content"
|
11 |
????????android:layout_height="wrap_content" />
|
14 |
????????android:layout_width="wrap_content"
|
15 |
????????android:layout_height="wrap_content"
|
16 |
????????android:text=":" />
|
19 |
????????android:id="@+id/default_value"
|
20 |
????????android:layout_width="wrap_content"
|
21 |
????????android:layout_height="wrap_content" />
|
每次实现的代码仅仅这么点
02 |
public void initView(ViewHolder holder) {
|
04 |
????holder.addView(R.id.default_lable);
|
05 |
????holder.addView(R.id.default_value);
|
09 |
public void setViewValue(ViewHolder holder, int position) {
|
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());
|
是不是非常不错呢?
编写自己的Adapter模板
原文:http://lexsain.iteye.com/blog/2189739