0

I have a app to shows a list of items. I need to update only the price textview of the item whenever the price get changed. But the whole list is getting updated in the recyclerview.

I'm using DiffUtil from this Library In the below example, When updateItems() is called, I need to update only the GRAPES Item Price Textview to the latest price.

Please guide me....

CODE

Fragment

public class DashboardFragment extends Fragment {

private DashboardViewModel dashboardViewModel;
final ArrayList<Indices> items = new ArrayList<>();
private RendererRecyclerViewAdapter mRecyclerViewAdapter;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    dashboardViewModel = new ViewModelProvider(this).get(DashboardViewModel.class);

    View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
    setHasOptionsMenu(true);

    mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
    mRecyclerViewAdapter.registerRenderer(getYourViewBinder());
    mRecyclerViewAdapter.setDiffCallback(new DiffCallback());

    final RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
    recyclerView.setAdapter(mRecyclerViewAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    mRecyclerViewAdapter.setItems(getItems());

    return root;
}

    private class DiffCallback extends DefaultDiffCallback<Indices> {
    @Override
    public boolean areItemsTheSame(@NonNull final Indices oldItem, @NonNull final Indices newItem) {
        return oldItem.getId() == newItem.getId();
    }

    @Override
    public boolean areContentsTheSame(@NonNull Indices oldItem, @NonNull Indices newItem) {
        return oldItem.equals(newItem);
    }
}

private ViewRenderer getYourViewBinder() {
    return new ViewRenderer<>(
            R.layout.item_futures, /* your item layout */
            Indices.class, /* your model class */
            (model, finder, payloads) -> finder
                    .setText(R.id.title, model.getName())
                    .find(R.id.tickerView, (ViewProvider<TickerView>) customView -> {
                        customView.setText(model.getPrice());
                    })
                    .setText(R.id.price, model.getPrice()));
}

public List<Indices> getItems() {
    items.add(new Indices(001, "APPLE", "15"));
    items.add(new Indices(002, "GRAPES", "50"));
    return items;
}

private void updateItems() {
    items.add(new Indices(001, "APPLE", "15"));
    items.add(new Indices(002, "GRAPES", "56"));
    mRecyclerViewAdapter.setItems(items);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.refresh) {
        updateItems();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

MODEL

public class Indices implements ViewModel {

@SerializedName("name")
@Expose
private String name;

@SerializedName("ltp")
@Expose
private String ltp;


@SerializedName("id")
@Expose
private int id;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPrice() {
    return ltp;
}

public Indices(int id, String name, String price) {
    this.id = id;
    this.name = name;
    this.ltp = price;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Indices indices = (Indices) o;

    return id == indices.id &&
            Objects.equals(name, indices.name) &&
            Objects.equals(ltp, indices.ltp);
}

@Override
public int hashCode() {
    return Objects.hash(name, ltp, id);
}

}
Arulnadhan
  • 923
  • 4
  • 17
  • 46

0 Answers0