标签:
/**
* Created by Administrator on 2016/4/19.
* 适配器的数据修改方法的定义
*/
public interface IAdapterDataModifyHelper<T> {
/**
* 通知适配器更新界面
*
* @param INotifyDataSetChange
*/
void setNotifyAdapterDataSetChange(INotifyAdapterDataSetChange INotifyDataSetChange);
/**
* 获取数据集
*
* @return
*/
List<T> getDataResources();
/**
* 设置数据集
*
* @param dataResources
*/
void setDataResource(T... dataResources);
void setDataResource(List<? extends T> dataResource);
/**
* 添加数据
*
* @param location
* @param dataResources
*/
void addDataResource(int location, T... dataResources);
void addDataResource(int location, List<? extends T> dataResources);
void addFirstDataResource(T... dataResources);
void addFirstDataResource(List<T> dataResources);
void addEndDataResource(T... dataResources);
void addEndDataResource(List<T> dataResources);
/**
* 修改元素
*
* @param position
* @param enity
*/
void reviseDataResource(int position, T enity);
/**
* 移除指定元素
*/
void removeDataResource(T removedEntiy);
/**
* 移除指定位置的元素
*
* @param position
*/
void removeDataResourceOnPosition(int position);
/**
* 清空所有数据集
*/
void clear();
/**
* 修正带操作的数据索引下标
*
* @param location
* @return
*/
int proofOperateDataLocation(int location);
/**
* 判断操作的location位置是否正确
*
* @return
*/
boolean isOperateLocationRight(int position);
}
/**
* @Description:针对数据的相关处理Helper类
* @author: Xiaoxuan948
* @date: 2016/7/20 20:37
*/
public class DataModifyHelper<T> implements IAdapterDataModifyHelper<T> {
private final UtilsLog lg = UtilsLog.getLogger(DataModifyHelper.class);
protected List<T> mDataResources;
private INotifyAdapterDataSetChange INotifyDataSetChange;
public DataModifyHelper() {
this.mDataResources = new ArrayList<>();
}
@Override
public void setNotifyAdapterDataSetChange(INotifyAdapterDataSetChange INotifyDataSetChange) {
this.INotifyDataSetChange = INotifyDataSetChange;
}
@Override
public List<T> getDataResources() {
return mDataResources;
}
@Override
public void setDataResource(List<? extends T> setDataResources) {
/*针对集合空数据进行处理*/
if (UtilsCollections.isCollectionNotEmpty(setDataResources) && setDataResources.contains(null)) {
lg.e("setDataResource集合包含null数据");
setDataResources = Lists.newArrayList(Collections2.filter(setDataResources, new Predicate<T>() {
@Override
public boolean apply(T input) {
return input != null;
}
}));
}
if (!UtilsCollections.isCollectionNotEmpty(setDataResources)) {
return;
}
/*针对类型不匹配进行处理*/
this.mDataResources = Lists.transform(setDataResources, new Function<Object, T>() {
@Override
public T apply(Object input) {
return (T) input;
}
});
INotifyDataSetChange.notifyAdapter();
}
@Override
public void addDataResource(int location, List<? extends T> addDataResources) {
if (UtilsCollections.isCollectionNotEmpty(addDataResources) && addDataResources.contains(null)) {
lg.e("addDataResource集合包含null数据");
addDataResources = Lists.newArrayList(Collections2.filter(addDataResources, new Predicate<T>() {
@Override
public boolean apply(T input) {
return input != null;
}
}));
}
if (!UtilsCollections.isCollectionNotEmpty(addDataResources)) {
lg.e("addDataResource:待加入的集合为空");
return;
}
location = proofOperateDataLocation(location);
this.mDataResources.addAll(location, addDataResources);
INotifyDataSetChange.notifyAdapter();
}
@Override
public void reviseDataResource(int position, T enity) {
if (isOperateLocationRight(position)) {
this.mDataResources.set(position, enity);
INotifyDataSetChange.notifyAdapter();
} else {
lg.e("reviseDataResource failed because position out of size " + this.mDataResources.size());
}
}
@Override
public void removeDataResourceOnPosition(int position) {
this.mDataResources.remove(position);
INotifyDataSetChange.notifyAdapter();
}
@Override
public void clear() {
this.mDataResources.clear();
INotifyDataSetChange.notifyAdapter();
}
@Override
public boolean isOperateLocationRight(int position) {
return !(position < 0 || position > this.mDataResources.size());
}
@Override
public int proofOperateDataLocation(int location) {
int newLocation = location < 0 ? 0 : location > this.mDataResources.size() ? this.mDataResources.size() : location;
lg.e("proofOperateDataLocation from " + location + " to " + newLocation);
return newLocation;
}
}
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();
}
/**
* 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)
);
Android适配器之基于BaseAdapter定义AbslistView万能适配器
标签:
原文地址:http://www.cnblogs.com/linux007/p/5800905.html