在开发的过程中,常常会用到ViewPager、ListView、GridView等、这些带有Item的视图控件,而这些控件有个共同点就是都要用到它们的适配器,我们在实现视图展示时,一般都会去写个自定义的适配器去继承PagerAdapter或Adapter或Adapter的子类,因为Android源码自带的这些适配器都比较抽象,往往在我们开发时,写的一些自定义适配器都需要重写Adapter父类的一些方法,在重写时,有很多较通用性的代码,比较耦合,下面将PagerAdapter和Adapter的子类BaseAdapter进行了封装与抽象,免去了每次都去重写这些通用的代码,代码如下:
1.ViewPagerAdapter
/** * 通用ViewPagerAdapter * @author Jenly * */ public class ViewPagerAdapter extends PagerAdapter { private List<View> listViews = null; public ViewPagerAdapter(List<View> listViews) { this.listViews = listViews; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager)container).removeView(listViews.get(position)); } @Override public int getCount() { return listViews.size(); } @Override public Object instantiateItem(View container, int position) { ((ViewPager)container).addView(listViews.get(position),0); return listViews.get(position); } @Override public boolean isViewFromObject(View paramView, Object paramObject) { return paramView == paramObject; } public List<View> getListViews() { return listViews; } public void setListViews(List<View> listViews) { this.listViews = listViews; } }
因为ViewPager的Item基本上都是继承View,所以这个ViewPagerAdapter 基本上可作为ViewPager控件的通用适配器。
2.AbstractAdapter
/** * 抽象适配器(免去一些有共性的代码) * @author Jenly * * @param <T> */ public abstract class AbstractAdapter<T> extends BaseAdapter { protected Context context; protected List<T> listData; protected LayoutInflater layoutInflater; public AbstractAdapter(Context context,List<T> listData){ this.context = context; this.listData = listData; layoutInflater = LayoutInflater.from(context); } @Override public int getCount() { if(listData!=null){ return listData.size(); } return 0; } @Override public Object getItem(int position) { if(listData!=null){ return listData.get(position); } return null; } @Override public long getItemId(int position) { return position; } public List<T> getListData() { return listData; } public void setListData(List<T> listData) { this.listData = listData; } }
AbstractAdapter<T> 将父类比较有共性的方法重写,自定义适配器时,只要继承AbstractAdapter<T>,重写getView方法就可以了。
3.HolderAdapter
/** * 通用适配器(适合一些常规的适配器) * @author Jenly * * @param <T> */ public abstract class HolderAdapter<T> extends AbstractAdapter<T>{ public HolderAdapter(Context context, List<T> listData) { super(context, listData); } @Override public View getView(int position, View convertView, ViewGroup parent) { Object holder = null; if(convertView==null){ convertView = buildConvertView(layoutInflater); holder = buildHolder(convertView); convertView.setTag(holder); }else{ holder = convertView.getTag(); } bindViewData(holder,listData.get(position),position); return convertView; } /** * 建立convertView * @param layoutInflater * @return */ public abstract View buildConvertView(LayoutInflater layoutInflater); /** * 建立视图Holder * @param convertView * @return */ public abstract Object buildHolder(View convertView); /** * 绑定数据 * @param holder * @param t * @param position */ public abstract void bindViewDatas(Object holder,T t,int position); }
HolderAdapter 继承于上面的AbstractAdapter类,将getView方法进行重写与抽象,使代码更加简洁,用起来更加简单,只要是继承于BaseAdapter的自定义适配器类,改为继承于HolderAdapter 基本上通用,然后只需实现BuildConvertView、buildHolder、bindViewDatas这三个方法。
下面是个自定义的测试适配器,继承HolderAdapter实现它的三个抽象方法:
public class TestHolderAdapter extends HolderAdapter<String>{ public TestHolderAdapter(Context context, List<String> listData) { super(context, listData); } @Override public View buildConvertView(LayoutInflater layoutInflater) { return layoutInflater.inflate(R.layout.activity_list_item, null); } @Override public Object buildHolder(View convertView) { ViewHolder holder = new ViewHolder(); holder.tv = (TextView)convertView.findViewById(R.id.tv); return holder; } @Override public void bindViewDatas(Object holder, String t, int position) { ((ViewHolder)holder).tv.setText(t); } private static class ViewHolder{ TextView tv; } }
时间: 2024-12-29 11:30:55