定義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)
);