[英]How to automatically update items data in recycler view?
假設其中有一個包含CardView
的RecyclerView
,在每個卡中,即項目中有兩個TextView
一個用於表示設備名稱,另一個用於rssi級別,因此,當用戶刷新數據時,只有rssi會刷新整個數據列表刷新。
我已經在RecyclerView
獲得了數據,但是重復了它而不是對其進行了更新。
型號類別:-
import android.support.annotation.NonNull;
public class RepeaterModel implements Comparable,Cloneable{
public String macdev;
public int rssi ;
public int imageid;
public RepeaterModel(String macdev, int rssi, int imageid) {
this.macdev = macdev;
this.rssi = rssi;
this.imageid = imageid;
}
public String getMacdev() {
return macdev;
}
public void setMacdev(String macdev) {
this.macdev = macdev;
}
public int getRssi() {
return rssi;
}
public void setRssi(int rssi) {
this.rssi = rssi;
}
public int getImageid() {
return imageid;
}
public void setImageid(int imageid) {
this.imageid = imageid;
}
@Override
public int compareTo(@NonNull Object o) {
RepeaterModel compare =(RepeaterModel)o;
if(compare.getMacdev().equals(this.macdev) && compare.getImageid()==this.imageid && compare.getRssi()==this.rssi)
{
return 0;
}
return 1;
}
@Override
public RepeaterModel clone()
{
RepeaterModel clone;
try {
clone = (RepeaterModel) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e); //should not happen
}
return clone;
}
}
設備適配器類:-
public class ReapeaterDeviceAdapter extends RecyclerView.Adapter<ReapeaterDeviceAdapter.CryptoViewHolder> {
private ArrayList<RepeaterModel> data;
public class CryptoViewHolder extends RecyclerView.ViewHolder {
private TextView mName, mPrice;
public CryptoViewHolder(View itemView) {
super(itemView);
mName = itemView.findViewById(R.id.txtName);
mPrice = itemView.findViewById(R.id.txtPrice);
}
}
public ReapeaterDeviceAdapter(ArrayList<RepeaterModel> data) {
this.data = data;
}
@Override
public CryptoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.repeater_dev_data,parent, false);
return new CryptoViewHolder(itemView);
}
@Override
public void onBindViewHolder(CryptoViewHolder holder, int position) {
holder.mName.setText(data.get(position).macdev);
holder.mPrice.setText(String.valueOf(data.get(position).rssi));
}
@Override
public void onBindViewHolder(CryptoViewHolder holder, int position, List<Object> payloads) {
if (payloads.isEmpty()) {
super.onBindViewHolder(holder, position, payloads);
} else {
Bundle o = (Bundle) payloads.get(0);
for (String key : o.keySet()) {
if (key.equals("price")) {
holder.mName.setText(data.get(position).macdev);
holder.mPrice.setText(String.valueOf(data.get(position).rssi));
holder.mPrice.setTextColor(Color.GREEN);
this.notifyItemChanged(position);
}
}
}
}
@Override
public int getItemCount() {
return data.size();
}
public ArrayList<RepeaterModel> getData() {
return data;
}
public void setData(ArrayList<RepeaterModel> newData) {
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallBack(newData, data));
diffResult.dispatchUpdatesTo(this);
data.clear();
this.data.addAll(newData);
//this.notifyDataSetChanged();
}
}
DiffUtilCallback類:-
public class MyDiffUtilCallBack extends DiffUtil.Callback {
ArrayList<RepeaterModel> newList;
ArrayList<RepeaterModel> oldList;
public MyDiffUtilCallBack(ArrayList<RepeaterModel> newList, ArrayList<RepeaterModel> oldList) {
this.newList = newList;
this.oldList = oldList;
}
@Override
public int getOldListSize() {
return oldList != null ? oldList.size() : 0;
}
@Override
public int getNewListSize() {
return newList != null ? newList.size() : 0;
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return newList.get(newItemPosition).getMacdev()==oldList.get(oldItemPosition).getMacdev() ;
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
int result = newList.get(newItemPosition).compareTo(oldList.get(oldItemPosition));
return result == 0;
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
RepeaterModel newModel = newList.get(newItemPosition);
RepeaterModel oldModel = oldList.get(oldItemPosition);
Bundle diff = new Bundle();
if(newModel.getMacdev().equals(oldModel.getMacdev()) ) {
if (newModel.rssi != (oldModel.rssi)) {
diff.putInt("price", newModel.rssi);
}
if (diff.size() == 0) {
return null;
}
}
return diff;
//return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
假設您的適配器有一個私有字段mItems
和一個公共方法,如下所示
public void setItems(List<YourClass> items){
mItems= items;
notifyDataSetChanged();
}
調用此方法將刷新您的回收站視圖。 另外,您可以像這樣簡單地通知適配器:
yourAdapterInstance.notifyDataSetChanged();
DiffCallback的實現無法正常工作:
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return newList.get(newItemPosition).getMacdev()==oldList.get(oldItemPosition).getMacdev() ;
}
使用equals方法而不是'=='
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return newList.get(newItemPosition).getMacdev().equals(oldList.get(oldItemPosition).getMacdev()) ;
}
另外,刪除this.notifyItemChanged(position);
來自方法public void onBindViewHolder(CryptoViewHolder holder, int position, List<Object> payloads) {
另外,在分派更新之前,您也需要更新列表。
public void setData(ArrayList<RepeaterModel> newData) {
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new
MyDiffUtilCallBack(newData, data));
data.clear();
this.data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
}
PS:代碼可能無法正常工作,將顏色更改為“綠色”可能會影響“未更新”的項目以便回收。 最好通過將已編輯/更新的信息添加到模型來更改RepeaterModel。
您應該只設置一次adapter
即recylerview.setAdapter(adapter)
在要更改Arraylist
數據的任何其他地方,請使用adapter.notifyDatasetChanged()
刷新列表。 它將僅刷新更改,而不刷新整個列表。
資料來源: https : //medium.com/@suragch/updating-data-in-an-android-recyclerview-842e56adbfd8
您應該將notifyItemChanged()與自定義對象一起使用,而不是notifyDatasetChanged。
創建具有2個成員的數據類,例如UpdateRecord
data class UpdateRecord(val _name : String? , val _rssi :String?)
當rssi更改時,請調用適配器的
notifyItemChange(position, UpdateRecord(null, newRssi))
您將收到onBindViewHolder(position,payload)調用,有效負載中的第一個對象是UpdateRecord對象。 檢查並做
val updateRecord = payload[0] as UpdateRecord
if (updateRecord._name != null) {
// update name text view
}
if (updateRecord._rssi != null) {
// update rssi text view
}
這是RecyclerView中的部分更新機制,該機制僅更新更改的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.