标签:
编写适配器代码时常常被以下几个问题所困扰:/**
* @Description:
* @author: Xiaoxuan948
* @date: 2016/7/20 20:39
*/
public interface INotifyAdapterDataSetChange {
/**
* 通知适配器更新
*/
void notifyUnityAdapterDataChange();
void notifyUnityAdapterDataInsert(int position);
void notifyUnityAdapterDataUpdate(int position);
void notifyUnityAdapterDataRemove(int position);
}
public abstract class AbsDataModifyHelper<T> implements IAdapterDataModifyHelper<T> {
private final UtilsLog lg = UtilsLog.getLogger(AbsDataModifyHelper.class);
protected List<T> mDataResources;
protected INotifyAdapterDataSetChange INotifyDataSetChange;
public AbsDataModifyHelper() {
this.mDataResources = new ArrayList<>();
}
@Override
public final void setNotifyAdapterDataSetChange(INotifyAdapterDataSetChange INotifyDataSetChange) {
this.INotifyDataSetChange = INotifyDataSetChange;
}
@Override
public final List<T> getDataResources() {
return mDataResources;
}
//省略部分代码
}
/**
* @Description:
* @author: Xiaoxuan948
* @date: 2016/8/26 10:04
*/
public class DataModifyHelper<T> extends AbsDataModifyHelper<T> {
private final UtilsLog lg = UtilsLog.getLogger(DataModifyHelper.class);
@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.notifyUnityAdapterDataChange();
}
@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.notifyUnityAdapterDataInsert(location);
}
@Override
public void reviseDataResource(int position, T enity) {
if (isOperateLocationRight(position)) {
this.mDataResources.set(position, enity);
INotifyDataSetChange.notifyUnityAdapterDataUpdate(position);
} else {
lg.e("reviseDataResource failed because position out of size " + this.mDataResources.size());
}
}
@Override
public void removeDataResourceOnPosition(int position) {
this.mDataResources.remove(position);
INotifyDataSetChange.notifyUnityAdapterDataRemove(position);
}
@Override
public void clear() {
this.mDataResources.clear();
INotifyDataSetChange.notifyUnityAdapterDataChange();
}
}
Android适配器之DataModifyHelper数据操作类的封装
标签:
原文地址:http://www.cnblogs.com/linux007/p/5809433.html