我有一个应用程序来显示项目列表。每当价格发生变化时,我只需要更新商品的价格文本视图。但整个列表正在回收站视图中更新。
我正在使用这个库中的 DiffUtil在下面的示例中,当updateItems()
被调用时,我只需要将GRAPES Item
Price Textview 更新为最新价格。
请指导我......
代码
分段
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);
}
模型
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);
}
}