title: How to use BindingAdapter inorder to update RecyclerView?
body: <p>I am using MVVM and DataBindg in my application.
  I have a problem that When data will be received from the server, my recyclerView adapter will add new items with no stop.</p>
  
  <p>For example, if I received 15 items, <code>recyclerview</code> will add 15, then 30, then 60, ...</p>
  
  <p>This is my fragment ViewModel:</p>
  
  <pre><code>public class ArticlesViewModel extends BaseObservable {
  
    // These observable fields will update Views automatically
    public final ObservableList&lt;Article&gt; items = new ObservableArrayList&lt;&gt;();
  
    public final ObservableBoolean dataLoading = new ObservableBoolean(false);
  
  
    private final AppRepository mTasksRepository;
  
    private final ObservableBoolean mIsDataLoadingError = new ObservableBoolean(false);
  
    private Context mContext; // To avoid leaks, this must be an Application Context.
  
    private static final String TAG = "ArticlesViewModel";
    public ArticlesViewModel(
      AppRepository repository,
      Context context) {
      mContext = context.getApplicationContext(); // Force use of Application Context.
      mTasksRepository = repository;
    }
  
  
    public void start() {
      loadTasks(false);
    }
  
    @Bindable
    public boolean isEmpty() {
      return items.isEmpty();
    }
  
    public void loadTasks(boolean forceUpdate) {
      loadTasks(forceUpdate, true);
    }
  
  
    public String getSnackbarText() {
      return snackbarText.get();
    }
  
    private void loadTasks(boolean forceUpdate, final boolean showLoadingUI) {
  
      if (showLoadingUI) {
        dataLoading.set(true);
      }
  
      mTasksRepository.getArticleList(0, new AppDataSource.LoadDefaultListCallback() {
        @Override
        public void onTasksLoaded(List list) {
  
          Log.d(TAG, "onTasksLoaded: ");
  
          if (showLoadingUI) {
            dataLoading.set(false);
          }
          mIsDataLoadingError.set(false);
  
          items.clear();
          items.addAll(list);
          notifyPropertyChanged(BR.empty); // It's a @Bindable so update manually
        }
  
        @Override
        public void onDataNotAvailable() {
          mIsDataLoadingError.set(true);
        }
      });
    }
  
  }
  </code></pre>
  
  <p>I checked this part of my code and this will execute once.
  And this is XML of this fragment:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
  
    &lt;data&gt;
  
      &lt;import type="android.view.View"/&gt;
  
      &lt;variable
        name="view"
        type="project.article.ArticleFragment"/&gt;
  
      &lt;variable
        name="viewmodel"
        type="project.article.ArticlesViewModel"/&gt;
  
    &lt;/data&gt;
  
  
    &lt;android.support.constraint.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
  
      &lt;android.support.v7.widget.RecyclerView
        android:id="@+id/article_recycler_article"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:items="@{viewmodel.items}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/article_recycler_cat"/&gt;
  
    &lt;/android.support.constraint.ConstraintLayout&gt;
  
  &lt;/layout&gt;
  </code></pre>
  
  <p>I also use <code>BindingAdapter</code> to set received data into mine <code>recyclerView</code>.
  This is my BindingAdapter class:</p>
  
  <pre><code>public class TasksListBindings {
  
    private static final String TAG = "TasksListBindings";
  
    @SuppressWarnings("unchecked")
    @BindingAdapter("app:items")
    public static void setItems(RecyclerView recyclerView, List&lt;Article&gt; items) {
  
      RecyclerArticleAdapter adapter = (RecyclerArticleAdapter) recyclerView.getAdapter();
      Log.d(TAG, "setItems: " + items.size());
        adapter.swapData(items); //this part of code will call more than 20 times that makes application to crash.
    }
  }
  </code></pre>
  
  <p>And finally this is my recyclerView adapter to set received data into it:</p>
  
  <pre><code>public class RecyclerArticleAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
  
    private List&lt;Article&gt; mainList;
  
    private static final String TAG = "RecyclerArticleAdapter";
  
    public RecyclerArticleAdapter(List&lt;Article&gt; mainList) {
      this.mainList = mainList;
      Log.d(TAG, "RecyclerArticleAdapter: " + mainList.size());
    }
  
  
    public void swapData(List&lt;Article&gt; dataList) {
  
      if (mainList.size() == 0) {
  
        mainList = dataList;
  
      } else {
  
        mainList.addAll(dataList);
      }
  
      notifyDataSetChanged();
    }
  
    public List&lt;Article&gt; getList() {
      return mainList;
    }
  
    public class ItemViewHolder extends RecyclerView.ViewHolder {
  
      private Context context;
      private ArticleItemBinding articleItemBinding;
  
      public ItemViewHolder(
        ArticleItemBinding binding) {
        super(binding.getRoot());
        context = itemView.getContext();
        articleItemBinding = binding;
      }
    }
  
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
      ArticleItemBinding binding = DataBindingUtil.inflate(
        LayoutInflater.from(parent.getContext()),
        R.layout.article_item, parent, false);
  
      ItemViewHolder holder = new ItemViewHolder(binding);
      return holder;
    }
  
  
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rawHolder, final int position) {
      final ItemViewHolder holder = (ItemViewHolder) rawHolder;
  
      Article article = mainList.get(position);
      holder.articleItemBinding.setArticle(article);
    }
  
    @Override
    public int getItemCount() {
      return mainList.size();
    }
  
  }
  </code></pre>
  
  <p>I can't understand that where is the problem.
  I really appreciate your answers.</p>
  
  <p>Thank you.</p>