标签:
/**
* 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");
}
}
}
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);
Android适配器之定义PagerAdapter万能适配器
标签:
原文地址:http://www.cnblogs.com/linux007/p/5801162.html