ArrayObjectAdapter.java revision 8b068ddbbf22a246eab49ec25a2f7c3abfbdca51
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 * Adapter implemented with an {@link ArrayList}. 21 */ 22public class ArrayObjectAdapter extends ObjectAdapter { 23 24 private ArrayList<Object> mItems = new ArrayList<Object>(); 25 26 public ArrayObjectAdapter(PresenterSelector presenterSelector) { 27 super(presenterSelector); 28 } 29 30 public ArrayObjectAdapter(Presenter presenter) { 31 super(presenter); 32 } 33 34 public ArrayObjectAdapter() { 35 super(); 36 } 37 38 @Override 39 public int size() { 40 return mItems.size(); 41 } 42 43 @Override 44 public Object get(int index) { 45 return mItems.get(index); 46 } 47 48 /** 49 * Adds an item to the end of the list. 50 * 51 * @param item The item to add to the end of the list. 52 */ 53 public void add(Object item) { 54 add(mItems.size(), item); 55 } 56 57 /** 58 * Inserts an item into this list at the specified index. 59 * 60 * @param index The index at which the item should be inserted. 61 * @param item The item to insert into the list. 62 */ 63 public void add(int index, Object item) { 64 mItems.add(index, item); 65 notifyItemRangeInserted(index, 1); 66 } 67 68 /** 69 * Adds the objects in the given collection to the list, starting at the 70 * given index. 71 * 72 * @param index The index at which the items should be inserted. 73 * @param items A {@link Collection} of items to insert. 74 */ 75 public void addAll(int index, Collection<Object> items) { 76 int itemsCount = items.size(); 77 mItems.addAll(index, items); 78 notifyItemRangeInserted(index, itemsCount); 79 } 80 81 /** 82 * Removes the first occurrence of the given item from the list. 83 * 84 * @param item The item to remove from the list. 85 * @return True if the item was found and thus removed from the list. 86 */ 87 public boolean remove(Object item) { 88 int index = mItems.indexOf(item); 89 if (index >= 0) { 90 mItems.remove(index); 91 notifyItemRangeRemoved(index, 1); 92 } 93 return index >= 0; 94 } 95 96 /** 97 * Removes a range of items from the list. The range is specified by giving 98 * the starting position and the number of elements to remove. 99 * 100 * @param position The index of the first item to remove. 101 * @param count The number of items to remove. 102 * @return The number of items removed. 103 */ 104 public int removeItems(int position, int count) { 105 int itemsToRemove = Math.min(count, mItems.size() - position); 106 107 for (int i = 0; i < itemsToRemove; i++) { 108 mItems.remove(position); 109 } 110 notifyItemRangeRemoved(position, itemsToRemove); 111 return itemsToRemove; 112 } 113 114 /** 115 * Removes all items from this list, leaving it empty. 116 */ 117 public void clear() { 118 int itemCount = mItems.size(); 119 mItems.clear(); 120 notifyItemRangeRemoved(0, itemCount); 121 } 122} 123