DiffUtil(存疑)
阿新 • • 發佈:2019-02-05
初級
ComplexTypeDiffCallBack
package joggle.diffutils;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
import java.util.List;
import bean.sync.ComplexType;
/**
* Created on 2018/6/27.
*
* @desc DiffUtil是RecycleView support library v7 24.2.0新增類
* 比兩資料表並算出一系列將舊資料錶轉新資料表操作,即局刷
*/
public class ComplexTypeDiffCallBack extends DiffUtil.Callback {
private List<ComplexType> oldData;
private List<ComplexType> newData;
public ComplexTypeDiffCallBack(List<ComplexType> oldData, List<ComplexType> newData) {
this.oldData = oldData;
this .newData = newData;
}
@Override
public int getOldListSize() {
return oldData.size();
}
@Override
public int getNewListSize() {
return newData.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldData.get(oldItemPosition).equals(newData.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldData.get(oldItemPosition).equals(newData.get(newItemPosition));
}
}
主程式碼
// 局刷
DiffUtil.DiffResult diffResult = DiffUtil.
calculateDiff(new ComplexTypeDiffCallBack(complexTypeList, LitePal.findAll(ComplexType.class)), false);
// DiffUtil.DiffResult之dispatchUpdatesTo()傳Adapter
diffResult.dispatchUpdatesTo(paraConfigChooseComplexTypeAdapter);
// 設Adapter新資料
complexTypeList = LitePal.findAll(ComplexType.class); paraConfigChooseComplexTypeAdapter.setParaConfigChooseComplexTypeData(complexTypeList);
高階
ComplexTypeDiffCallBack
package joggle.diffutils;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
import java.util.List;
import bean.sync.ComplexType;
/**
* Created on 2018/6/27.
*
* @desc DiffUtil是RecycleView support library v7 24.2.0新增類
* 比兩資料表並算出一系列將舊資料錶轉新資料表操作,即局刷
*/
public class ComplexTypeDiffCallBack extends DiffUtil.Callback {
private List<ComplexType> oldData;
private List<ComplexType> newData;
public ComplexTypeDiffCallBack(List<ComplexType> oldData, List<ComplexType> newData) {
this.oldData = oldData;
this.newData = newData;
}
@Override
public int getOldListSize() {
return oldData.size();
}
@Override
public int getNewListSize() {
return newData.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldData.get(oldItemPosition).equals(newData.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldData.get(oldItemPosition).equals(newData.get(newItemPosition));
}
/**
* DiffUtil高階用法
*
* @param oldItemPosition 老位
* @param newItemPosition 新位
* @return 實體
*/
@Override
@Nullable
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
// 定向重新整理中部分更新(效率最高)
// 無Item閃動
ComplexType complexTypeOld = oldData.get(oldItemPosition);
ComplexType complexTypeNew = newData.get(newItemPosition);
// 不比核心欄位(定等)
Bundle payload = new Bundle();
if (complexTypeOld.getCjrq() != complexTypeNew.getCjrq()) {
payload.putLong("KEY_CJRQ", complexTypeNew.getCjrq());
}
if (!complexTypeOld.getFzlx().equals(complexTypeNew.getFzlx())) {
payload.putString("KEY_FZLX", complexTypeNew.getFzlx());
}
if (!complexTypeOld.getServiceId().equals(complexTypeNew.getServiceId())) {
payload.putString("KEY_SERVICE_ID", complexTypeNew.getServiceId());
}
if (!complexTypeOld.getSfzy().equals(complexTypeNew.getSfzy())) {
payload.putString("KEY_SFZY", complexTypeNew.getSfzy());
}
if (complexTypeOld.getLocalAdd() != complexTypeNew.getLocalAdd()) {
payload.putInt("KEY_LOCAL_ADD", complexTypeNew.getLocalAdd());
}
if (payload.size() == 0) {
// 無變返空
return null;
}
// public Object getChangePayload()非空時onChange通Partial bind,無Item閃動(否有)
return payload;
}
}
介面卡
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ComplexType complexType = complexTypeList.get(position);
// 資料存itemView之Tag以便點獲
older.itemView.setTag(complexType);
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position, @NonNull List<Object> payloads) {
if (payloads.isEmpty()) {
onBindViewHolder(holder, position);
} else {
ComplexType complexType = complexTypeList.get(position);
Bundle payload = (Bundle) payloads.get(0);
for (String key : payload.keySet()) {
switch (key) {
// 此可用payload資料(data亦新亦可用)
case "KEY_CJRQ":
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
break;
case "KEY_FZLX":
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
break;
case "KEY_SERVICE_ID":
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
break;
case "KEY_SFZY":
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
break;
ase "KEY_LOCAL_ADD":
holder.tvBasicConfigChoose.setText(complexType.getFzlx());
break;
default:
break;
}
}
}
}
主程式碼
// 局刷
DiffUtil.DiffResult diffResult = DiffUtil.
calculateDiff(new ComplexTypeDiffCallBack(complexTypeList, LitePal.findAll(ComplexType.class)), false);
// DiffUtil.DiffResult之dispatchUpdatesTo()傳Adapter
diffResult.dispatchUpdatesTo(paraConfigChooseComplexTypeAdapter);
// 設Adapter新資料
complexTypeList = LitePal.findAll(ComplexType.class); paraConfigChooseComplexTypeAdapter.setParaConfigChooseComplexTypeData(complexTypeList);
疑問
- 初級偶現重新整理時Item閃動
- 高階介面卡之三參
onBindViewHolder
中payloads
總為空