如果你项目一直用系统给你封装的BaseAdapter的话,那么我只想说,你不累么? 代码繁多还要写数据缓存,还不如自己动手写一个模板吧,这样后面项目就可以直接套用了,编写和执行效率大大提升啊。 BaseAdapter.java
package com.android.dev.base; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.content.Context; import android.content.res.Resources; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 实现这个适配器模板会少很多代码 * @author leehom * @param <T> */ public abstract class BaseAdapter<T> extends android.widget.BaseAdapter { protected List<T> list; protected Context context; protected LayoutInflater inflater; private int layoutID; public BaseAdapter(Context context, T[] ts, int layoutID) { super(); this.context = context; this.list = new ArrayList<T>(); this.layoutID = layoutID; Collections.addAll(list, ts); this.inflater = LayoutInflater.from(context); } public BaseAdapter(Context context, List<T> list, int layoutID) { super(); this.layoutID = layoutID; this.context = context; this.list = new ArrayList<T>(); this.list.addAll(list); this.inflater = LayoutInflater.from(context); } public Resources getResources() { return context.getResources(); } public synchronized void add(T object) { list.add(object); } public synchronized void addAll(Collection<? extends T> collection) { list.addAll(collection); } public synchronized void remove(T object) { list.remove(object); } public synchronized void insert(T object, int index) { list.add(index, object); } public synchronized void clear() { list.clear(); } public synchronized void sort(Comparator<? super T> comparator) { Collections.sort(list, comparator); } @Override public T getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { convertView = inflater.inflate(layoutID, null); holder = new ViewHolder(convertView); initView(holder); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } setViewValue(holder, position); return convertView; } public List<T> getList() { return list; } /** * 向ViewHolder类里面添加View * @param holder */ public abstract void initView(ViewHolder holder); /** * 从ViewHolder获取对应ID的View设置其值 * @param holder * @param t 数据对象 */ public abstract void setViewValue(ViewHolder holder, int position); }
用的时候也是继承它,然后只需要初始化ID和设置Value就行了。每次实现的代码仅仅这么点
import java.util.List; import android.content.Context; import com.android.dev.base.BaseAdapter; import com.android.dev.base.ViewHolder public class ReceiveAdapter extends BaseAdapter<DataItem> { public ReceiveAdapter(Context context, List<DataItem> list, int layoutID) { super(context, list, layoutID); // TODO Auto-generated constructor stub } @Override public void initView(ViewHolder holder) { // TODO Auto-generated method stub holder.addView(R.id.name); holder.addView(R.id.value); } @Override public void setViewValue(ViewHolder holder, int position) { // TODO Auto-generated method stub DataItem item = getItem(position); holder.setText(R.id.name, item.getName()); holder.setText(R.id.value, item.getValue()); } @Override public boolean isEnabled(int position) { // TODO Auto-generated method stub return false; } }
是不是非常不错呢?
最后是ViewHolder代码
package com.android.dev.base; import android.util.SparseArray; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class ViewHolder { SparseArray<View> array; View parent; Object obj; public ViewHolder(View parent) { this.parent = parent; array = new SparseArray<View>(); } public void addView(int id) { array.append(id, parent.findViewById(id)); } public View getView(int id) { return array.get(id); } public void setVisible(int id, boolean visible) { if (visible) getView(id).setVisibility(View.VISIBLE); else getView(id).setVisibility(View.INVISIBLE); } public void setVisibleGone(int id, boolean visible) { if (visible) getView(id).setVisibility(View.VISIBLE); else getView(id).setVisibility(View.GONE); } public void setSelected(int id, boolean selected) { getView(id).setSelected(selected); } public Button getButton(int id) { return (Button) getView(id); } public TextView getTextView(int id) { return (TextView) getView(id); } public EditText getEditView(int id) { return (EditText) getView(id); } public ImageView getImageView(int id) { return (ImageView) getView(id); } public ImageButton getImageButton(int id) { return (ImageButton) getView(id); } public void setText(int id, String text) { getTextView(id).setText(text); } public void setText(int id, CharSequence text) { getTextView(id).setText(text); } public void setText(int id, int text) { getTextView(id).setText(String.valueOf(text)); } public void setText(int id, float text) { getTextView(id).setText(String.valueOf(text)); } public void setImage(int id, int imgId) { getImageView(id).setImageResource(imgId); } public void showView(int id) { getView(id).setVisibility(View.VISIBLE); } public void hideView(int id, boolean gone) { getView(id).setVisibility(gone ? View.GONE : View.INVISIBLE); } public void setTag(int id, Object obj) { getView(id).setTag(obj); } public Object getTag(int id) { return getView(id).getTag(); } public View getParent() { return parent; } public void setParent(View parent) { this.parent = parent; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } }
收藏的用户(0) X
正在加载信息~
推荐阅读
还在用Android自带的WebView组件?太Out了!
最新回复 (2)
- BarbarasWexy 2015-3-26引用 2楼ViewHolder如何写的,可否也贴出来呢?
- LucieA8696582846 2015-3-26引用 3楼
package com.android.dev.base; import android.util.SparseArray; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class ViewHolder { SparseArray<View> array; View parent; Object obj; public ViewHolder(View parent) { this.parent = parent; array = new SparseArray<View>(); } public void addView(int id) { array.append(id, parent.findViewById(id)); } public View getView(int id) { return array.get(id); } public void setVisible(int id, boolean visible) { if (visible) getView(id).setVisibility(View.VISIBLE); else getView(id).setVisibility(View.INVISIBLE); } public void setVisibleGone(int id, boolean visible) { if (visible) getView(id).setVisibility(View.VISIBLE); else getView(id).setVisibility(View.GONE); } public void setSelected(int id, boolean selected) { getView(id).setSelected(selected); } public Button getButton(int id) { return (Button) getView(id); } public TextView getTextView(int id) { return (TextView) getView(id); } public EditText getEditView(int id) { return (EditText) getView(id); } public ImageView getImageView(int id) { return (ImageView) getView(id); } public ImageButton getImageButton(int id) { return (ImageButton) getView(id); } public void setText(int id, String text) { getTextView(id).setText(text); } public void setText(int id, CharSequence text) { getTextView(id).setText(text); } public void setText(int id, int text) { getTextView(id).setText(String.valueOf(text)); } public void setText(int id, float text) { getTextView(id).setText(String.valueOf(text)); } public void setImage(int id, int imgId) { getImageView(id).setImageResource(imgId); } public void showView(int id) { getView(id).setVisibility(View.VISIBLE); } public void hideView(int id, boolean gone) { getView(id).setVisibility(gone ? View.GONE : View.INVISIBLE); } public void setTag(int id, Object obj) { getView(id).setTag(obj); } public Object getTag(int id) { return getView(id).getTag(); } public View getParent() { return parent; } public void setParent(View parent) { this.parent = parent; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } }
站点信息
- 文章2295
- 用户1336
- 访客10465927
每日一句
Wisdom begins in wonder.
智慧始于好奇。
智慧始于好奇。
新会员