一般使用pageAdapter需要实现getcount、isViewFromObject、instantiateItem、destroyItem等默认函数,其实完全可以抽取共有代码到父类当中。
/** * Created by Xiaoxuan948 on 2015/10/27. * Desc: * 提示:先给adapter设置数据,再绑定适配器 setAdapter会调用instantiateItem方法 */public class AbsUnityBasePageAdapter<T> extends PagerAdapter implements INotifyAdapterDataSetChange { private static final UtilsLog lg = UtilsLog.getLogger(AbsUnityBasePageAdapter.class.getSimpleName()); private LayoutInflater layoutInflater; private IAdapterDataModifyHelper<T> IAdapterataModifyHelper; public AbsUnityBasePageAdapter() { this.IAdapterataModifyHelper = new DataModifyHelper<>(); } public IAdapterDataModifyHelper<T> getDataModifyHelper() { this.IAdapterataModifyHelper.setNotifyAdapterDataSetChange(this); return IAdapterataModifyHelper; } @Override public void notifyAdapter() { lg.e("notifyAdapter with dataSize:" + this.IAdapterataModifyHelper.getDataResources().size()); notifyDataSetChanged(); } /*PagerAdapter默认必须实现方法*/ @Override public boolean isViewFromObject(View view, Object object) { lg.e("isViewFromObject:" + (view == object ? "true" : "false")); return view == object; } @Override public int getCount() { return IAdapterataModifyHelper.getDataResources().size(); } @Override public Object instantiateItem(ViewGroup container, int position) { if (layoutInflater == null) { layoutInflater = LayoutInflater.from(container.getContext()); } if (position >= IAdapterataModifyHelper.getDataResources().size()) { lg.e("修正position:instantiateItem failed for position:" + position + " out of size:" + IAdapterataModifyHelper.getDataResources().size()); position = position % getDataModifyHelper().getDataResources().size(); } lg.e("generateItemView for position:" + position); View itemView = generateItemView(IAdapterataModifyHelper.getDataResources().get(position), layoutInflater, position); container.addView(itemView); return itemView; } @Override //不调用会报错 public void destroyItem(ViewGroup container, int position, Object object) { lg.e("destroyItem:" + position); container.removeView((View) object); } /*自定义的抽象方法:基于Position构建View对象*/ protected View generateItemView(T itemEntity, LayoutInflater layoutInflater, int position) { if (itemEntity instanceof View) { return (View) IAdapterataModifyHelper.getDataResources().get(position); } else { throw new NullPointerException("DataResource Type is not View,so child class must Override"); } }}
其中,IAdapterataModifyHelper当中封装对数据的增删改查等操作,并且已内部处理空指针异常。
instantiateItem中国,首先通过数组是否越界矫正position,调用generateItemView方法生成View对象并添加至container当中。在generateItemView实现当中,默认实现会判断当前数据集是否为View的集合,否则抛出异常,这里主要是为了强制开发人员重写generateItemView根据itemEntity生成View对象。
业务层使用方式如下:
adapter = new AbsUnityBasePageAdapter<String>() { @Override protected View generateItemView(String itemEntity, LayoutInflater layoutInflater, int position) { lg.e("ViewPagerLabBaseAdapter generateItemView with position :" + position); View itemRootView = layoutInflater.inflate(R.layout.simple_item_vertical, null); ViewFindHelper viewFindHelper = new ViewFindHelper(itemRootView); ImageView imgItemCommon = viewFindHelper.generateView(R.id.imgItemCommon); imgItemCommon.setScaleType(ImageView.ScaleType.CENTER_CROP); Glide.with(layoutInflater.getContext()).load(itemEntity).into(imgItemCommon); return itemRootView; }}; adapter.getDataModifyHelper().setDataResource( "http://tupian.enterdesk.com/2013/mxy/12/10/15/3.jpg", "http://pic1.nipic.com/2008-11-13/2008111384358912_2.jpg", "http://img4.imgtn.bdimg.com/it/u=1249974208,2325623016&fm=21&gp=0.jpg", "http://img0.imgtn.bdimg.com/it/u=1918423052,1540132432&fm=21&gp=0.jpg");vpActViewPager.setAdapter(adapter);
时间: 2024-12-30 10:49:58