1/* 2 * Copyright 2018 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 androidx.recyclerview.widget; 18 19import androidx.annotation.NonNull; 20 21/** 22 * ListUpdateCallback that dispatches update events to the given adapter. 23 * 24 * @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter) 25 */ 26public final class AdapterListUpdateCallback implements ListUpdateCallback { 27 @NonNull 28 private final RecyclerView.Adapter mAdapter; 29 30 /** 31 * Creates an AdapterListUpdateCallback that will dispatch update events to the given adapter. 32 * 33 * @param adapter The Adapter to send updates to. 34 */ 35 public AdapterListUpdateCallback(@NonNull RecyclerView.Adapter adapter) { 36 mAdapter = adapter; 37 } 38 39 /** {@inheritDoc} */ 40 @Override 41 public void onInserted(int position, int count) { 42 mAdapter.notifyItemRangeInserted(position, count); 43 } 44 45 /** {@inheritDoc} */ 46 @Override 47 public void onRemoved(int position, int count) { 48 mAdapter.notifyItemRangeRemoved(position, count); 49 } 50 51 /** {@inheritDoc} */ 52 @Override 53 public void onMoved(int fromPosition, int toPosition) { 54 mAdapter.notifyItemMoved(fromPosition, toPosition); 55 } 56 57 /** {@inheritDoc} */ 58 @Override 59 public void onChanged(int position, int count, Object payload) { 60 mAdapter.notifyItemRangeChanged(position, count, payload); 61 } 62} 63