1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v7.widget.util; 18 19import android.support.v7.util.SortedList; 20import android.support.v7.widget.RecyclerView; 21 22/** 23 * A {@link SortedList.Callback} implementation that can bind a {@link SortedList} to a 24 * {@link RecyclerView.Adapter}. 25 */ 26public abstract class SortedListAdapterCallback<T2> extends SortedList.Callback<T2> { 27 28 final RecyclerView.Adapter mAdapter; 29 30 /** 31 * Creates a {@link SortedList.Callback} that will forward data change events to the provided 32 * Adapter. 33 * 34 * @param adapter The Adapter instance which should receive events from the SortedList. 35 */ 36 public SortedListAdapterCallback(RecyclerView.Adapter adapter) { 37 mAdapter = adapter; 38 } 39 40 @Override 41 public void onInserted(int position, int count) { 42 mAdapter.notifyItemRangeInserted(position, count); 43 } 44 45 @Override 46 public void onRemoved(int position, int count) { 47 mAdapter.notifyItemRangeRemoved(position, count); 48 } 49 50 @Override 51 public void onMoved(int fromPosition, int toPosition) { 52 mAdapter.notifyItemMoved(fromPosition, toPosition); 53 } 54 55 @Override 56 public void onChanged(int position, int count) { 57 mAdapter.notifyItemRangeChanged(position, count); 58 } 59} 60