Android项目-高考作文-抽象BaseAdapter

时间:2022-02-14 11:47:24

1, 在使用baseAdapter的时候,总是有需要重复的代码如:

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

2, 把这些重复的代码统统扔给父类就行了:

public abstract class AbstractAdapter<T> extends BaseAdapter {

	protected List<T> list;
	protected LayoutInflater lif;

	public AbstractAdapter(Context context, List<T> list) {
		this.list = list;
		lif = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return populateView(position, convertView, parent);
	}

	protected abstract View populateView(int position, View convertView,
			ViewGroup parent);

}
AbstractAdapter

3, 子类使用的时候, 就不要直接继承BaseActivity 而是AbstractAdapter  , 只需要专心写我们的populateView就行了.