Android 打造万能适配器

    我们在项目开发中,经常会和适配器(Adapter)打交道,仔细观察,适配器里面有好多代码都可以进行重构,所以,是时候打造属于你的万能适配器。

一.基本做法(旧的做法)

    1.新建项目;

    2.新建布局文件activity_main.xml,创建ListView控件,



    



     新建适配器item布局文件,adapter_item.xml(显示2个TextView),



    

    

    3.新建实体类-Bean(测试实体类);

public class Bean {
	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public Bean(String name, String age) {
		super();
		this.name = name;
		this.age = age;
	}
}

    4.新建适配器-MyAdapter1;

public class MyAdapter1 extends BaseAdapter {

	private List mData;
	private LayoutInflater inflater;
	public MyAdapter1(Context context,List data) {
		mData=data;
		inflater=LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@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;
		if(convertView==null){
			holder=new ViewHolder();
			convertView=inflater.inflate(R.layout.adapter_item, parent, false);
			holder.tvName=(TextView) convertView.findViewById(R.id.id_tv_name);
			holder.tvAge=(TextView) convertView.findViewById(R.id.id_tv_age);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		Bean bean=mData.get(position);
		holder.tvName.setText(bean.getName());
		holder.tvAge.setText(bean.getAge());
		return convertView;
	}

	private class ViewHolder {
		TextView tvName;
		TextView tvAge;
	}
}

以上这些代码都很简单,就不解释了!  

  5.新建Activity-MainActivity;

public class MainActivity extends Activity {

	private MyAdapter1 adapter1;
	private ListView lv;
	private List mData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lv=(ListView) findViewById(R.id.id_lv);
		mData=new ArrayList();
		initData();
		adapter1=new MyAdapter1(this, mData);
		lv.setAdapter(adapter1);
	}
//加载模拟数据
	private void initData() {
		Bean bean=new Bean("zhangsan","12");
		mData.add(bean);
		 bean=new Bean("lisi","23");
		mData.add(bean);
		 bean=new Bean("wangwu","32");
		mData.add(bean);
		 bean=new Bean("liuer","42");
		mData.add(bean);
		 bean=new Bean("yuyi","51");
		mData.add(bean);
		 bean=new Bean("xuba","64");
		mData.add(bean);
		 bean=new Bean("wujiu","73");
		mData.add(bean);
		 bean=new Bean("tushi","87");
		mData.add(bean);
		bean=new Bean("guoqi","90");
		mData.add(bean);
		bean=new Bean("renliu","13");
		mData.add(bean);
	}

}


以上就是最普通、最通常的做法。

二.代码重构(新的做法)

    1.新建项目;

    2.新建布局文件activity_main.xml,创建ListView控件,新建适配器item布局文件,adapter_item.xml(显示2个TextView);

    3.新建实体类-Bean(测试实体类);(前面这几步都一样)

    4.新建ViewHolder类,该类封装之前在适配器中的一些代码;

//构造方法 -加载布局
public ViewHolder(Context context, ViewGroup parent, int layoutId,
			int position) {
		array = new SparseArray();
		mPosition = position;
		mConverView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		mConverView.setTag(this);
	}
//获取ViewHolder-如果convertView是空,则去创建布局,并返回ViewHolder;否则,返回已经存在的ViewHolder
public static ViewHolder getViewHodler(Context context, View convertView,
			ViewGroup parent, int layoutId, int position) {
		if (convertView == null) {
			return new ViewHolder(context, parent, layoutId, position);
		} else {
			ViewHolder holder = (ViewHolder) convertView.getTag();
			holder.mPosition = position;
			return holder;
		}
	}
//获取控件,通过ViewId
public  T getView(int viewId) {
		View view = array.get(viewId);
		if (view == null) {
			view = mConverView.findViewById(viewId);
			array.put(viewId, view);
		}
		return (T) view;
	}
以上就是一些必要的方法;

     5.新建CommonAdapter类,该类实现在适配器中一些重复的代码;

public abstract class CommonAdapter extends BaseAdapter {

	public List mData;
	private Context mContext;
	private int mLayoutId;

	public CommonAdapter(Context context, List data,int layoutId) {
		mContext = context;
		mData = data;
		mLayoutId=layoutId;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public T getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public  View getView(int position, View convertView,
			ViewGroup parent){
		ViewHolder holder=ViewHolder.getViewHodler(mContext, convertView, parent, mLayoutId, position);
		convert(holder, mData.get(position));
		return holder.getmConverView();
	}
//子适配器通过这个方法去实现具体的操作
	public abstract void convert(ViewHolder holder,T t);
	
}
    6.新建MyAdapter2类,只需要很简单的几行就可以了;

public class MyAdapter2 extends CommonAdapter  {

	
	public MyAdapter2(Context context, List data) {
		super(context, data, R.layout.adapter_item);
	}

	@Override
	public void convert(com.example.projectfast.adapter.fast.ViewHolder holder,
			Bean bean) {
		holder.setText(R.id.id_tv_name,bean.getName()).setText(R.id.id_tv_age, bean.getAge());
	}
}
万能适配器的代码基本上就完了,在activity中万能适配器的使用方法和一般的适配器都一样!代码量很少吧!

三.总结 

    在实际项目中使用这样的万能适配器,确实比之前的代码量少了很多!并且,这个万能适配器的实现也不是很复杂!要是你觉得不理解或者有些枯燥,那么请去慕课网看看鸿洋大牛的视频吧!值得推荐!

    推荐和项目有关的文章,Android 项目开发框架。

代码下载链接

PS:这篇文章是看了鸿洋大牛的视频后,写的!在此,非常感谢慕课网以及鸿洋大牛!希望有更多更好的视频以及文章可以分享!

你可能感兴趣的:(android,项目快速开发)