ArrayObjectAdapter.java revision 9a934fa3710ecb3fb2eb67e33a611563a144186d
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import java.util.ArrayList; 17import java.util.Collection; 18 19/** 20 * An ObjectAdapter implemented with an {@link ArrayList}. 21 */ 22public class ArrayObjectAdapter extends ObjectAdapter { 23 24 private ArrayList<Object> mItems = new ArrayList<Object>(); 25 26 /** 27 * Construct an adapter with the given {@link PresenterSelector}. 28 */ 29 public ArrayObjectAdapter(PresenterSelector presenterSelector) { 30 super(presenterSelector); 31 } 32 33 /** 34 * Construct an adapter that uses the given {@link Presenter} for all items. 35 */ 36 public ArrayObjectAdapter(Presenter presenter) { 37 super(presenter); 38 } 39 40 /** 41 * Construct an adapter. 42 */ 43 public ArrayObjectAdapter() { 44 super(); 45 } 46 47 @Override 48 public int size() { 49 return mItems.size(); 50 } 51 52 @Override 53 public Object get(int index) { 54 return mItems.get(index); 55 } 56 57 /** 58 * Returns the index for the first occurrence of item in the adapter, or -1 if 59 * not found. 60 * 61 * @param item The item to find in the list. 62 * @return Index of the first occurrence of the item in the adapter, or -1 63 * if not found. 64 */ 65 public int indexOf(Object item) { 66 return mItems.indexOf(item); 67 } 68 69 /** 70 * Notify that the content of a range of items changed. Note that this is 71 * not same as items being added or removed. 72 * 73 * @param positionStart The position of first item that has changed. 74 * @param itemCount The count of how many items have changed. 75 */ 76 public void notifyArrayItemRangeChanged(int positionStart, int itemCount) { 77 notifyItemRangeChanged(positionStart, itemCount); 78 } 79 80 /** 81 * Adds an item to the end of the adapter. 82 * 83 * @param item The item to add to the end of the adapter. 84 */ 85 public void add(Object item) { 86 add(mItems.size(), item); 87 } 88 89 /** 90 * Inserts an item into this adapter at the specified index. 91 * 92 * @param index The index at which the item should be inserted. 93 * @param item The item to insert into the adapter. 94 */ 95 public void add(int index, Object item) { 96 mItems.add(index, item); 97 notifyItemRangeInserted(index, 1); 98 } 99 100 /** 101 * Adds the objects in the given collection to the adapter, starting at the 102 * given index. 103 * 104 * @param index The index at which the items should be inserted. 105 * @param items A {@link Collection} of items to insert. 106 */ 107 public void addAll(int index, Collection items) { 108 int itemsCount = items.size(); 109 if (itemsCount == 0) { 110 return; 111 } 112 mItems.addAll(index, items); 113 notifyItemRangeInserted(index, itemsCount); 114 } 115 116 /** 117 * Removes the first occurrence of the given item from the adapter. 118 * 119 * @param item The item to remove from the adapter. 120 * @return True if the item was found and thus removed from the adapter. 121 */ 122 public boolean remove(Object item) { 123 int index = mItems.indexOf(item); 124 if (index >= 0) { 125 mItems.remove(index); 126 notifyItemRangeRemoved(index, 1); 127 } 128 return index >= 0; 129 } 130 131 /** 132 * Replaces item at position with a new item and calls notifyItemRangeChanged() 133 * at the given position. Note that this method does not compare new item to 134 * existing item. 135 * @param position The index of item to replace. 136 * @param item The new item to be placed at given position. 137 */ 138 public void replace(int position, Object item) { 139 mItems.set(position, item); 140 notifyItemRangeChanged(position, 1); 141 } 142 143 /** 144 * Removes a range of items from the adapter. The range is specified by giving 145 * the starting position and the number of elements to remove. 146 * 147 * @param position The index of the first item to remove. 148 * @param count The number of items to remove. 149 * @return The number of items removed. 150 */ 151 public int removeItems(int position, int count) { 152 int itemsToRemove = Math.min(count, mItems.size() - position); 153 if (itemsToRemove <= 0) { 154 return 0; 155 } 156 157 for (int i = 0; i < itemsToRemove; i++) { 158 mItems.remove(position); 159 } 160 notifyItemRangeRemoved(position, itemsToRemove); 161 return itemsToRemove; 162 } 163 164 /** 165 * Removes all items from this adapter, leaving it empty. 166 */ 167 public void clear() { 168 int itemCount = mItems.size(); 169 if (itemCount == 0) { 170 return; 171 } 172 mItems.clear(); 173 notifyItemRangeRemoved(0, itemCount); 174 } 175} 176