推荐优先本地搜索浏览 Android适配器之DataModifyHelper数据操作类的封装
定义BaseAdapter抽象基类,抽取共性部分,即实现getCount、getItem、getItemId
观察源码可知,在构造函数中实例化DataModifyHelper对象,该对象负责数据的增删改查等操作。适配器中提供getDataModifyHelper方法,供外界获得操作数据的DataModifyHelper帮助类。
public abstract class AbsUnityBaseAdapter<T> extends BaseAdapter implements INotifyAdapterDataSetChange {
private static final UtilsLog lg = UtilsLog.getLogger(AbsUnityBaseAdapter.class);
private LayoutInflater layoutInflater;
private IAdapterDataModifyHelper<T> IAdapterataModifyHelper;
/**
* 构造方法
*/
protected AbsUnityBaseAdapter() {
this.IAdapterataModifyHelper = new DataModifyHelper<>();
}
public IAdapterDataModifyHelper<T> getDataModifyHelper() {
this.IAdapterataModifyHelper.setNotifyAdapterDataSetChange(this);
return IAdapterataModifyHelper;
}
@Override
public void notifyAdapter() {
notifyDataSetChanged();
}
/**
* default method
*/
@Override
public int getCount() {
return IAdapterataModifyHelper.getDataResources().size();
}
@Override
public T getItem(int position) {
//可能数组越界
if (position >= IAdapterataModifyHelper.getDataResources().size()) {
lg.e("getItem:index " + position + " is out of size " + IAdapterataModifyHelper.getDataResources().size());
return null;
}
return IAdapterataModifyHelper.getDataResources().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UnityAdapterViewHolder helper = null;
if (convertView != null) {
helper = (UnityAdapterViewHolder) convertView.getTag();
} else {
if (layoutInflater == null) {
layoutInflater = LayoutInflater.from(parent.getContext());
}
helper = new UnityAdapterViewHolder(layoutInflater, getLayoutId(), parent);
}
convert(IAdapterataModifyHelper.getDataResources().get(position), helper, position);
return helper.getConvertView();
}
/*自定义抽象方法*/
public abstract void convert(T itemEntity, UnityAdapterViewHolder helper, int position);
public abstract int getLayoutId();
}
分析getView部分源码可知,其基于UnityAdapterViewHolder类封装ViewHolder模式,构造方法如下:
/**
* Created by Administrator on 2015/8/25.
*/
public class UnityAdapterViewHolder {
private SparseArray<View> views;
private View convertView;
public UnityAdapterViewHolder(LayoutInflater layoutInflater, int layoutId, ViewGroup parent) {
this.views = new SparseArray<>();
this.convertView = layoutInflater.inflate(layoutId, parent, false);
this.convertView.setTag(this);
}
public View getConvertView() {
return convertView;
}
/**
* generateView
*
* @param viewId
* @param <T>
* @return
*/
protected <T extends View> T generateView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = convertView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
/**
* @param viewId
* @param clz
* @param <T>
* @return
*/
public <T extends View> T generateView(int viewId, Class<T> clz) {
return generateView(viewId);
}
}
业务层使用方式如下:
adapter = new AbsUnityBaseRecyclerAdapter<CorePageEntity>() {
@Override
public void convert(CorePageEntity itemEntity, UnityRecyclerAdapterViewHolder viewHolder, int position) {
viewHolder.generateView(R.id.tvItemCommon, TextView.class).setText(itemEntity.getCorePageTitle());
}
@Override
public int getItemLayoutId(int viewType) {
return R.layout.simple_item_horizontal;
}
};
对于数据修改等操作,基于以下方式调用即可轻松实现。
adapter.getDataModifyHelper().setDataResource(
CorePageEntity.create(TestActivity.class),
CorePageEntity.create(PullToRefreshActivity.class)
);