mherod
2/8/2016 - 9:35 AM

AdapterOnListChangedCallback.java

import android.databinding.ObservableList;
import android.support.v7.widget.RecyclerView;

import java.lang.ref.WeakReference;

public class AdapterOnListChangedCallback extends ObservableList.OnListChangedCallback {

    private final WeakReference<RecyclerView.Adapter<?>> adapterWeakReference;

    public AdapterOnListChangedCallback(RecyclerView.Adapter<?> adapter) {
        this.adapterWeakReference = new WeakReference<>(adapter);
    }

    @Override
    public void onChanged(ObservableList sender) {
        RecyclerView.Adapter adapter = adapterWeakReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) {
        RecyclerView.Adapter adapter = adapterWeakReference.get();
        if (adapter != null) {
            adapter.notifyItemRangeChanged(positionStart, itemCount);
        }
    }

    @Override
    public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) {
        RecyclerView.Adapter adapter = adapterWeakReference.get();
        if (adapter != null) {
            adapter.notifyItemRangeInserted(positionStart, itemCount);
        }
    }

    @Override
    public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) {
        RecyclerView.Adapter adapter = adapterWeakReference.get();
        if (adapter != null) {
            adapter.notifyItemMoved(fromPosition, toPosition);
        }
    }

    @Override
    public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) {
        RecyclerView.Adapter adapter = adapterWeakReference.get();
        if (adapter != null) {
            adapter.notifyItemRangeRemoved(positionStart, itemCount);
        }
    }
}