ArrayObjectAdapter.java revision a7ba44025ec38541f429a830763ff799b5e5267e
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 * Search first occurrence of item in the list; return -1 if not found. 50 * @param item The item to search in the list. 51 * @return First occurrence of item in the list or -1 if not found. 52 */ 53 public int indexOf(Object item) { 54 return mItems.indexOf(item); 55 } 56 57 /** 58 * Notify content of range of items changed. Note that this is not same 59 * as add or remove items. 60 * @param positionStart The position of first item that has changed. 61 * @param itemCount The count of how many items has changed. 62 */ 63 public void notifyArrayItemRangeChanged(int positionStart, int itemCount) { 64 notifyItemRangeChanged(positionStart, itemCount); 65 } 66 67 /** 68 * Adds an item to the end of the list. 69 * 70 * @param item The item to add to the end of the list. 71 */ 72 public void add(Object item) { 73 add(mItems.size(), item); 74 } 75 76 /** 77 * Inserts an item into this list at the specified index. 78 * 79 * @param index The index at which the item should be inserted. 80 * @param item The item to insert into the list. 81 */ 82 public void add(int index, Object item) { 83 mItems.add(index, item); 84 notifyItemRangeInserted(index, 1); 85 } 86 87 /** 88 * Adds the objects in the given collection to the list, starting at the 89 * given index. 90 * 91 * @param index The index at which the items should be inserted. 92 * @param items A {@link Collection} of items to insert. 93 */ 94 public void addAll(int index, Collection items) { 95 int itemsCount = items.size(); 96 mItems.addAll(index, items); 97 notifyItemRangeInserted(index, itemsCount); 98 } 99 100 /** 101 * Removes the first occurrence of the given item from the list. 102 * 103 * @param item The item to remove from the list. 104 * @return True if the item was found and thus removed from the list. 105 */ 106 public boolean remove(Object item) { 107 int index = mItems.indexOf(item); 108 if (index >= 0) { 109 mItems.remove(index); 110 notifyItemRangeRemoved(index, 1); 111 } 112 return index >= 0; 113 } 114 115 /** 116 * Removes a range of items from the list. The range is specified by giving 117 * the starting position and the number of elements to remove. 118 * 119 * @param position The index of the first item to remove. 120 * @param count The number of items to remove. 121 * @return The number of items removed. 122 */ 123 public int removeItems(int position, int count) { 124 int itemsToRemove = Math.min(count, mItems.size() - position); 125 126 for (int i = 0; i < itemsToRemove; i++) { 127 mItems.remove(position); 128 } 129 notifyItemRangeRemoved(position, itemsToRemove); 130 return itemsToRemove; 131 } 132 133 /** 134 * Removes all items from this list, leaving it empty. 135 */ 136 public void clear() { 137 int itemCount = mItems.size(); 138 mItems.clear(); 139 notifyItemRangeRemoved(0, itemCount); 140 } 141} 142