D
D
Dmitry Serkov2016-07-22 23:08:14
Java
Dmitry Serkov, 2016-07-22 23:08:14

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

2 answer(s)
A
AlexeyVD, 2016-07-25
@Hutaab

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;
        }
    }
}

Example implementation for the History class:
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;
    }
}

Add and change elements in the list using setElements() and notifyDataSetChanged():
mRecyclerAdapter.setElements(getData());
mRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerAdapter.notifyDataSetChanged();

Well, in the markup of your item_history there should be a variable history declaration with other bindings:
...
    <data>
        ...
        <variable
            name="history "
            type="History " />
       ...
    </data>
...

T
Tiberal, 2016-07-22
@Tiberal

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 question

Ask a Question

731 491 924 answers to any question