如何在Recycler视图中自动更新商品数据?

时间:2019-07-17 09:16:42

标签: java android android-recyclerview

我们假设其中有一个RecyclerView,其中包含CardView,在每张卡中,即物品中有两个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);
    }
}

4 个答案:

答案 0 :(得分:0)

可以说您的适配器具有一个私有字段mItems和一个看起来像这样的公共方法

public void setItems(List<YourClass> items){
    mItems= items;
    notifyDataSetChanged();
}

调用此方法将刷新您的回收站视图。 另外,您可以像这样简单地通知适配器:

yourAdapterInstance.notifyDataSetChanged();

答案 1 :(得分:0)

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。

答案 2 :(得分:-1)

您应该只设置一次adapter,即recylerview.setAdapter(adapter)。在要更改Arraylist中数据的任何其他地方,请使用adapter.notifyDatasetChanged()刷新列表。它将仅刷新更改,而不刷新整个列表。

来源:https://medium.com/@suragch/updating-data-in-an-android-recyclerview-842e56adbfd8

答案 3 :(得分:-1)

您应该将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中的部分更新机制,它仅更新更改的内容。