Answer the question
In order to leave comments, you need to log in
Databinding and RecyclerView how to merge?
There is a History object, it has two fields mTime and mItem.
The list of objects changes after a request to the server and is displayed through the RecyclerView.
How to transfer changed data using Databinding?
Thank you.
Answer the question
In order to leave comments, you need to log in
Working with data in RecyclerView occurs through the adapter class. I once threw in a simple abstract adapter when I was testing data binding:
public abstract class AbstractRecyclerAdapter<E> extends
RecyclerView.Adapter<AbstractRecyclerAdapter.BindingHolder> {
private List<E> mElements;
public AbstractRecyclerAdapter() {
mElements = new ArrayList<>();
}
public abstract int getItemLayoutId();
public abstract int getVariableId();
public void setElements(List<E> elements) {
mElements = elements;
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutId(),
parent, false);
return new BindingHolder(v);
}
@Override
public void onBindViewHolder(BindingHolder holder, int position) {
final E element = mElements.get(position);
holder.getBinding().setVariable(getVariableId(), element);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return mElements.size();
}
public static class BindingHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;
public BindingHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}
public ViewDataBinding getBinding() {
return binding;
}
}
}
public class HistoryRecyclerAdapter<History> extends AbstractRecyclerAdapter<History> {
public HistoryRecyclerAdapter() {
super();
}
@Override
public int getItemLayoutId() {
return R.layout.item_history;
}
@Override
public int getVariableId() {
return BR.history;
}
}
mRecyclerAdapter.setElements(getData());
mRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerAdapter.notifyDataSetChanged();
...
<data>
...
<variable
name="history "
type="History " />
...
</data>
...
Update data in RecyclerView. pull the notify method on it (there are a lot of them, choose the one that suits you). The RecyclerView will update the items, and the binds will pull up the new values.
You can create a method with the annotation BindingAdapter inside which you update the data in the adapter and pull the notification, attach the method to the RecyclerView as an argument to its collection with data, which is distributed through the ObservableField. Every time the collection will be networked with data, the method will twitch and update the RecyclerView
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question