Android適配器之DataModifyHelper資料操作類的封裝

來源:互聯網
上載者:User

標籤:

編寫適配器代碼時常常被以下幾個問題所困擾:1.業務層和適配器中對同一組資料進行維護,難以管理2.在業務層針對資料進行修改後必須通知適配器更新,否則提示The content of the adapter has changed but ListView did not receive anotification
3.業務層修改資料時充斥大量的非空&資料標準化等冗餘代碼
針對前兩個問題,可以將資料交由適配器去管理,業務層對資料的增刪改查均通過適配器進行處理,這樣僅需要維護好adapter中的資料即可。
關於資料的非空判斷以及增刪改查等操作,可以抽取資料操作類來簡化適配器當中的邏輯。
下面給出具體的代碼實現適配器更新方式介面定義,其具體實現可根據適配器類型(BaseAdapter/PageAdapter/RecyclerView.Adapter)自行調整。
/**
* @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);
}
介面IAdapterDataModifyHelper定義針對資料的各種操作,其子類AbsDataModifyHelper進行簡要實現。
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;
}
    //省略部分代碼
}
觀察可知,在AbsDataModifyHelper構造方法中執行個體化mDataResources對象,其對應於清單控制項中顯示的資料。setNotifyAdapterDataSetChange為外界提供資料更新的介面注入方法,在資料修改後調用INotifyDataSetChange的notify...()方法即可通知適配器更新。

開發人員可自行擴充AbsDataModifyHelper,下面給出一個樣本實作類別DataModifyHelper。
/**
* @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();
}
}
setDataResource方法中,針對List資料先進行非空過濾,再判斷List非空後進行資料修改,以確保mDataResources集合始終非空。這裡會注意到其參數使用無限制萬用字元,主要是為了提高程式的擴充性,當適配器基於介面編程時能方便set資料。(使用transform的原因:List<Child>無法轉為List<Parent>)addDataResource的實現過程同setDataResource基本一致,都是針對資料集進行非空過濾以及判斷等一系列處理。
源碼連結:https://github.com/xiaoxuan948/AndroidUnityLab/tree/master/unity_base_dev_helper/src/main/java/com/coca/unity_base_dev_helper/adapter
使用樣本(部落格園可通過左邊"搜尋"進入):Android適配器之基於BaseAdapter定義AbslistView萬能適配器
Android適配器之 V7擴充包 RecyclerView 的使用以及萬能適配器的封裝實現
Android適配器之定義PagerAdapter萬能適配器




來自為知筆記(Wiz)

Android適配器之DataModifyHelper資料操作類的封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.