The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 17 | package android.widget; |
| 18 | |
Mathew Inwood | a85f4eb | 2018-08-21 16:08:34 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.database.DataSetObserver; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | |
| 26 | /** |
| 27 | * ListAdapter used when a ListView has header views. This ListAdapter |
| 28 | * wraps another one and also keeps track of the header views and their |
| 29 | * associated data objects. |
| 30 | *<p>This is intended as a base class; you will probably not need to |
| 31 | * use this class directly in your own code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | */ |
| 33 | public class HeaderViewListAdapter implements WrapperListAdapter, Filterable { |
| 34 | |
Mathew Inwood | a85f4eb | 2018-08-21 16:08:34 +0100 | [diff] [blame] | 35 | @UnsupportedAppUsage |
Gilles Debunne | 478a745 | 2010-03-23 16:44:48 -0700 | [diff] [blame] | 36 | private final ListAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Gilles Debunne | 176f9fc | 2010-03-02 11:00:50 -0800 | [diff] [blame] | 38 | // These two ArrayList are assumed to NOT be null. |
Gilles Debunne | 478a745 | 2010-03-23 16:44:48 -0700 | [diff] [blame] | 39 | // They are indeed created when declared in ListView and then shared. |
Mathew Inwood | a85f4eb | 2018-08-21 16:08:34 +0100 | [diff] [blame] | 40 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | ArrayList<ListView.FixedViewInfo> mHeaderViewInfos; |
Mathew Inwood | a85f4eb | 2018-08-21 16:08:34 +0100 | [diff] [blame] | 42 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | ArrayList<ListView.FixedViewInfo> mFooterViewInfos; |
Gilles Debunne | 478a745 | 2010-03-23 16:44:48 -0700 | [diff] [blame] | 44 | |
| 45 | // Used as a placeholder in case the provided info views are indeed null. |
| 46 | // Currently only used by some CTS tests, which may be removed. |
| 47 | static final ArrayList<ListView.FixedViewInfo> EMPTY_INFO_LIST = |
| 48 | new ArrayList<ListView.FixedViewInfo>(); |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | boolean mAreAllFixedViewsSelectable; |
| 51 | |
Gilles Debunne | 478a745 | 2010-03-23 16:44:48 -0700 | [diff] [blame] | 52 | private final boolean mIsFilterable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | public HeaderViewListAdapter(ArrayList<ListView.FixedViewInfo> headerViewInfos, |
| 55 | ArrayList<ListView.FixedViewInfo> footerViewInfos, |
| 56 | ListAdapter adapter) { |
| 57 | mAdapter = adapter; |
| 58 | mIsFilterable = adapter instanceof Filterable; |
| 59 | |
Gilles Debunne | 478a745 | 2010-03-23 16:44:48 -0700 | [diff] [blame] | 60 | if (headerViewInfos == null) { |
| 61 | mHeaderViewInfos = EMPTY_INFO_LIST; |
| 62 | } else { |
| 63 | mHeaderViewInfos = headerViewInfos; |
| 64 | } |
| 65 | |
| 66 | if (footerViewInfos == null) { |
| 67 | mFooterViewInfos = EMPTY_INFO_LIST; |
| 68 | } else { |
| 69 | mFooterViewInfos = footerViewInfos; |
| 70 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | |
| 72 | mAreAllFixedViewsSelectable = |
| 73 | areAllListInfosSelectable(mHeaderViewInfos) |
| 74 | && areAllListInfosSelectable(mFooterViewInfos); |
| 75 | } |
| 76 | |
| 77 | public int getHeadersCount() { |
Gilles Debunne | 176f9fc | 2010-03-02 11:00:50 -0800 | [diff] [blame] | 78 | return mHeaderViewInfos.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public int getFootersCount() { |
Gilles Debunne | 176f9fc | 2010-03-02 11:00:50 -0800 | [diff] [blame] | 82 | return mFooterViewInfos.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public boolean isEmpty() { |
Adam Powell | fc93d0c | 2013-06-12 17:38:25 -0700 | [diff] [blame] | 86 | return mAdapter == null || mAdapter.isEmpty(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private boolean areAllListInfosSelectable(ArrayList<ListView.FixedViewInfo> infos) { |
| 90 | if (infos != null) { |
| 91 | for (ListView.FixedViewInfo info : infos) { |
| 92 | if (!info.isSelectable) { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | public boolean removeHeader(View v) { |
| 101 | for (int i = 0; i < mHeaderViewInfos.size(); i++) { |
| 102 | ListView.FixedViewInfo info = mHeaderViewInfos.get(i); |
| 103 | if (info.view == v) { |
| 104 | mHeaderViewInfos.remove(i); |
| 105 | |
| 106 | mAreAllFixedViewsSelectable = |
| 107 | areAllListInfosSelectable(mHeaderViewInfos) |
| 108 | && areAllListInfosSelectable(mFooterViewInfos); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | public boolean removeFooter(View v) { |
| 118 | for (int i = 0; i < mFooterViewInfos.size(); i++) { |
| 119 | ListView.FixedViewInfo info = mFooterViewInfos.get(i); |
| 120 | if (info.view == v) { |
| 121 | mFooterViewInfos.remove(i); |
| 122 | |
| 123 | mAreAllFixedViewsSelectable = |
| 124 | areAllListInfosSelectable(mHeaderViewInfos) |
| 125 | && areAllListInfosSelectable(mFooterViewInfos); |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | public int getCount() { |
| 135 | if (mAdapter != null) { |
| 136 | return getFootersCount() + getHeadersCount() + mAdapter.getCount(); |
| 137 | } else { |
| 138 | return getFootersCount() + getHeadersCount(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public boolean areAllItemsEnabled() { |
| 143 | if (mAdapter != null) { |
| 144 | return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled(); |
| 145 | } else { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public boolean isEnabled(int position) { |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 151 | // Header (negative positions will throw an IndexOutOfBoundsException) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | int numHeaders = getHeadersCount(); |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 153 | if (position < numHeaders) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | return mHeaderViewInfos.get(position).isSelectable; |
| 155 | } |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 156 | |
| 157 | // Adapter |
| 158 | final int adjPosition = position - numHeaders; |
| 159 | int adapterCount = 0; |
| 160 | if (mAdapter != null) { |
| 161 | adapterCount = mAdapter.getCount(); |
| 162 | if (adjPosition < adapterCount) { |
| 163 | return mAdapter.isEnabled(adjPosition); |
| 164 | } |
| 165 | } |
| 166 | |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 167 | // Footer (off-limits positions will throw an IndexOutOfBoundsException) |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 168 | return mFooterViewInfos.get(adjPosition - adapterCount).isSelectable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public Object getItem(int position) { |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 172 | // Header (negative positions will throw an IndexOutOfBoundsException) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | int numHeaders = getHeadersCount(); |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 174 | if (position < numHeaders) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | return mHeaderViewInfos.get(position).data; |
| 176 | } |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 177 | |
| 178 | // Adapter |
| 179 | final int adjPosition = position - numHeaders; |
| 180 | int adapterCount = 0; |
| 181 | if (mAdapter != null) { |
| 182 | adapterCount = mAdapter.getCount(); |
| 183 | if (adjPosition < adapterCount) { |
| 184 | return mAdapter.getItem(adjPosition); |
| 185 | } |
| 186 | } |
| 187 | |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 188 | // Footer (off-limits positions will throw an IndexOutOfBoundsException) |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 189 | return mFooterViewInfos.get(adjPosition - adapterCount).data; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | public long getItemId(int position) { |
| 193 | int numHeaders = getHeadersCount(); |
| 194 | if (mAdapter != null && position >= numHeaders) { |
| 195 | int adjPosition = position - numHeaders; |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 196 | int adapterCount = mAdapter.getCount(); |
| 197 | if (adjPosition < adapterCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | return mAdapter.getItemId(adjPosition); |
| 199 | } |
| 200 | } |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | public boolean hasStableIds() { |
| 205 | if (mAdapter != null) { |
| 206 | return mAdapter.hasStableIds(); |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | public View getView(int position, View convertView, ViewGroup parent) { |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 212 | // Header (negative positions will throw an IndexOutOfBoundsException) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | int numHeaders = getHeadersCount(); |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 214 | if (position < numHeaders) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | return mHeaderViewInfos.get(position).view; |
| 216 | } |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 217 | |
| 218 | // Adapter |
| 219 | final int adjPosition = position - numHeaders; |
| 220 | int adapterCount = 0; |
| 221 | if (mAdapter != null) { |
| 222 | adapterCount = mAdapter.getCount(); |
| 223 | if (adjPosition < adapterCount) { |
| 224 | return mAdapter.getView(adjPosition, convertView, parent); |
| 225 | } |
| 226 | } |
| 227 | |
Taylor H. Perkins | 62b2bb8 | 2013-04-30 12:04:52 -0700 | [diff] [blame] | 228 | // Footer (off-limits positions will throw an IndexOutOfBoundsException) |
Gilles Debunne | 34783aa | 2010-03-24 14:47:28 -0700 | [diff] [blame] | 229 | return mFooterViewInfos.get(adjPosition - adapterCount).view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public int getItemViewType(int position) { |
| 233 | int numHeaders = getHeadersCount(); |
| 234 | if (mAdapter != null && position >= numHeaders) { |
| 235 | int adjPosition = position - numHeaders; |
| 236 | int adapterCount = mAdapter.getCount(); |
| 237 | if (adjPosition < adapterCount) { |
| 238 | return mAdapter.getItemViewType(adjPosition); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER; |
| 243 | } |
| 244 | |
| 245 | public int getViewTypeCount() { |
| 246 | if (mAdapter != null) { |
| 247 | return mAdapter.getViewTypeCount(); |
| 248 | } |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | public void registerDataSetObserver(DataSetObserver observer) { |
| 253 | if (mAdapter != null) { |
| 254 | mAdapter.registerDataSetObserver(observer); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | public void unregisterDataSetObserver(DataSetObserver observer) { |
| 259 | if (mAdapter != null) { |
| 260 | mAdapter.unregisterDataSetObserver(observer); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | public Filter getFilter() { |
| 265 | if (mIsFilterable) { |
| 266 | return ((Filterable) mAdapter).getFilter(); |
| 267 | } |
| 268 | return null; |
| 269 | } |
| 270 | |
| 271 | public ListAdapter getWrappedAdapter() { |
| 272 | return mAdapter; |
| 273 | } |
| 274 | } |