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 | |
| 19 | import com.android.internal.R; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Rect; |
Adam Powell | 4580347 | 2010-01-25 15:10:44 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.util.AttributeSet; |
| 29 | import android.view.ContextMenu; |
| 30 | import android.view.SoundEffectConstants; |
| 31 | import android.view.View; |
| 32 | import android.view.ContextMenu.ContextMenuInfo; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame^] | 33 | import android.view.accessibility.AccessibilityEvent; |
| 34 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.widget.ExpandableListConnector.PositionMetadata; |
| 36 | |
Adam Powell | 4580347 | 2010-01-25 15:10:44 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** |
| 40 | * A view that shows items in a vertically scrolling two-level list. This |
| 41 | * differs from the {@link ListView} by allowing two levels: groups which can |
| 42 | * individually be expanded to show its children. The items come from the |
| 43 | * {@link ExpandableListAdapter} associated with this view. |
| 44 | * <p> |
| 45 | * Expandable lists are able to show an indicator beside each item to display |
| 46 | * the item's current state (the states are usually one of expanded group, |
| 47 | * collapsed group, child, or last child). Use |
| 48 | * {@link #setChildIndicator(Drawable)} or {@link #setGroupIndicator(Drawable)} |
| 49 | * (or the corresponding XML attributes) to set these indicators (see the docs |
| 50 | * for each method to see additional state that each Drawable can have). The |
| 51 | * default style for an {@link ExpandableListView} provides indicators which |
| 52 | * will be shown next to Views given to the {@link ExpandableListView}. The |
| 53 | * layouts android.R.layout.simple_expandable_list_item_1 and |
| 54 | * android.R.layout.simple_expandable_list_item_2 (which should be used with |
| 55 | * {@link SimpleCursorTreeAdapter}) contain the preferred position information |
| 56 | * for indicators. |
| 57 | * <p> |
| 58 | * The context menu information set by an {@link ExpandableListView} will be a |
| 59 | * {@link ExpandableListContextMenuInfo} object with |
| 60 | * {@link ExpandableListContextMenuInfo#packedPosition} being a packed position |
| 61 | * that can be used with {@link #getPackedPositionType(long)} and the other |
| 62 | * similar methods. |
| 63 | * <p> |
| 64 | * <em><b>Note:</b></em> You cannot use the value <code>wrap_content</code> |
| 65 | * for the <code>android:layout_height</code> attribute of a |
| 66 | * ExpandableListView in XML if the parent's size is also not strictly specified |
| 67 | * (for example, if the parent were ScrollView you could not specify |
| 68 | * wrap_content since it also can be any length. However, you can use |
| 69 | * wrap_content if the ExpandableListView parent has a specific size, such as |
| 70 | * 100 pixels. |
| 71 | * |
| 72 | * @attr ref android.R.styleable#ExpandableListView_groupIndicator |
| 73 | * @attr ref android.R.styleable#ExpandableListView_indicatorLeft |
| 74 | * @attr ref android.R.styleable#ExpandableListView_indicatorRight |
| 75 | * @attr ref android.R.styleable#ExpandableListView_childIndicator |
| 76 | * @attr ref android.R.styleable#ExpandableListView_childIndicatorLeft |
| 77 | * @attr ref android.R.styleable#ExpandableListView_childIndicatorRight |
| 78 | * @attr ref android.R.styleable#ExpandableListView_childDivider |
| 79 | */ |
| 80 | public class ExpandableListView extends ListView { |
| 81 | |
| 82 | /** |
| 83 | * The packed position represents a group. |
| 84 | */ |
| 85 | public static final int PACKED_POSITION_TYPE_GROUP = 0; |
| 86 | |
| 87 | /** |
| 88 | * The packed position represents a child. |
| 89 | */ |
| 90 | public static final int PACKED_POSITION_TYPE_CHILD = 1; |
| 91 | |
| 92 | /** |
| 93 | * The packed position represents a neither/null/no preference. |
| 94 | */ |
| 95 | public static final int PACKED_POSITION_TYPE_NULL = 2; |
| 96 | |
| 97 | /** |
| 98 | * The value for a packed position that represents neither/null/no |
| 99 | * preference. This value is not otherwise possible since a group type |
| 100 | * (first bit 0) should not have a child position filled. |
| 101 | */ |
| 102 | public static final long PACKED_POSITION_VALUE_NULL = 0x00000000FFFFFFFFL; |
| 103 | |
| 104 | /** The mask (in packed position representation) for the child */ |
| 105 | private static final long PACKED_POSITION_MASK_CHILD = 0x00000000FFFFFFFFL; |
| 106 | |
| 107 | /** The mask (in packed position representation) for the group */ |
| 108 | private static final long PACKED_POSITION_MASK_GROUP = 0x7FFFFFFF00000000L; |
| 109 | |
| 110 | /** The mask (in packed position representation) for the type */ |
| 111 | private static final long PACKED_POSITION_MASK_TYPE = 0x8000000000000000L; |
| 112 | |
| 113 | /** The shift amount (in packed position representation) for the group */ |
| 114 | private static final long PACKED_POSITION_SHIFT_GROUP = 32; |
| 115 | |
| 116 | /** The shift amount (in packed position representation) for the type */ |
| 117 | private static final long PACKED_POSITION_SHIFT_TYPE = 63; |
| 118 | |
| 119 | /** The mask (in integer child position representation) for the child */ |
| 120 | private static final long PACKED_POSITION_INT_MASK_CHILD = 0xFFFFFFFF; |
| 121 | |
| 122 | /** The mask (in integer group position representation) for the group */ |
| 123 | private static final long PACKED_POSITION_INT_MASK_GROUP = 0x7FFFFFFF; |
| 124 | |
| 125 | /** Serves as the glue/translator between a ListView and an ExpandableListView */ |
| 126 | private ExpandableListConnector mConnector; |
| 127 | |
| 128 | /** Gives us Views through group+child positions */ |
| 129 | private ExpandableListAdapter mAdapter; |
| 130 | |
| 131 | /** Left bound for drawing the indicator. */ |
| 132 | private int mIndicatorLeft; |
| 133 | |
| 134 | /** Right bound for drawing the indicator. */ |
| 135 | private int mIndicatorRight; |
| 136 | |
| 137 | /** |
| 138 | * Left bound for drawing the indicator of a child. Value of |
| 139 | * {@link #CHILD_INDICATOR_INHERIT} means use mIndicatorLeft. |
| 140 | */ |
| 141 | private int mChildIndicatorLeft; |
| 142 | |
| 143 | /** |
| 144 | * Right bound for drawing the indicator of a child. Value of |
| 145 | * {@link #CHILD_INDICATOR_INHERIT} means use mIndicatorRight. |
| 146 | */ |
| 147 | private int mChildIndicatorRight; |
| 148 | |
| 149 | /** |
| 150 | * Denotes when a child indicator should inherit this bound from the generic |
| 151 | * indicator bounds |
| 152 | */ |
| 153 | public static final int CHILD_INDICATOR_INHERIT = -1; |
| 154 | |
| 155 | /** The indicator drawn next to a group. */ |
| 156 | private Drawable mGroupIndicator; |
| 157 | |
| 158 | /** The indicator drawn next to a child. */ |
| 159 | private Drawable mChildIndicator; |
| 160 | |
| 161 | private static final int[] EMPTY_STATE_SET = {}; |
| 162 | |
| 163 | /** State indicating the group is expanded. */ |
| 164 | private static final int[] GROUP_EXPANDED_STATE_SET = |
| 165 | {R.attr.state_expanded}; |
| 166 | |
| 167 | /** State indicating the group is empty (has no children). */ |
| 168 | private static final int[] GROUP_EMPTY_STATE_SET = |
| 169 | {R.attr.state_empty}; |
| 170 | |
| 171 | /** State indicating the group is expanded and empty (has no children). */ |
| 172 | private static final int[] GROUP_EXPANDED_EMPTY_STATE_SET = |
| 173 | {R.attr.state_expanded, R.attr.state_empty}; |
| 174 | |
| 175 | /** States for the group where the 0th bit is expanded and 1st bit is empty. */ |
| 176 | private static final int[][] GROUP_STATE_SETS = { |
| 177 | EMPTY_STATE_SET, // 00 |
| 178 | GROUP_EXPANDED_STATE_SET, // 01 |
| 179 | GROUP_EMPTY_STATE_SET, // 10 |
| 180 | GROUP_EXPANDED_EMPTY_STATE_SET // 11 |
| 181 | }; |
| 182 | |
| 183 | /** State indicating the child is the last within its group. */ |
| 184 | private static final int[] CHILD_LAST_STATE_SET = |
| 185 | {R.attr.state_last}; |
| 186 | |
| 187 | /** Drawable to be used as a divider when it is adjacent to any children */ |
| 188 | private Drawable mChildDivider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
| 190 | // Bounds of the indicator to be drawn |
| 191 | private final Rect mIndicatorRect = new Rect(); |
| 192 | |
| 193 | public ExpandableListView(Context context) { |
| 194 | this(context, null); |
| 195 | } |
| 196 | |
| 197 | public ExpandableListView(Context context, AttributeSet attrs) { |
| 198 | this(context, attrs, com.android.internal.R.attr.expandableListViewStyle); |
| 199 | } |
| 200 | |
| 201 | public ExpandableListView(Context context, AttributeSet attrs, int defStyle) { |
| 202 | super(context, attrs, defStyle); |
| 203 | |
| 204 | TypedArray a = |
| 205 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ExpandableListView, defStyle, |
| 206 | 0); |
| 207 | |
| 208 | mGroupIndicator = a |
| 209 | .getDrawable(com.android.internal.R.styleable.ExpandableListView_groupIndicator); |
| 210 | mChildIndicator = a |
| 211 | .getDrawable(com.android.internal.R.styleable.ExpandableListView_childIndicator); |
| 212 | mIndicatorLeft = a |
| 213 | .getDimensionPixelSize(com.android.internal.R.styleable.ExpandableListView_indicatorLeft, 0); |
| 214 | mIndicatorRight = a |
| 215 | .getDimensionPixelSize(com.android.internal.R.styleable.ExpandableListView_indicatorRight, 0); |
Romain Guy | 875862e | 2011-01-17 11:37:24 -0800 | [diff] [blame] | 216 | if (mIndicatorRight == 0 && mGroupIndicator != null) { |
Romain Guy | fb13abd | 2011-01-16 15:16:38 -0800 | [diff] [blame] | 217 | mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth(); |
| 218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | mChildIndicatorLeft = a.getDimensionPixelSize( |
| 220 | com.android.internal.R.styleable.ExpandableListView_childIndicatorLeft, CHILD_INDICATOR_INHERIT); |
| 221 | mChildIndicatorRight = a.getDimensionPixelSize( |
| 222 | com.android.internal.R.styleable.ExpandableListView_childIndicatorRight, CHILD_INDICATOR_INHERIT); |
| 223 | mChildDivider = a.getDrawable(com.android.internal.R.styleable.ExpandableListView_childDivider); |
| 224 | |
| 225 | a.recycle(); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | @Override |
| 230 | protected void dispatchDraw(Canvas canvas) { |
| 231 | // Draw children, etc. |
| 232 | super.dispatchDraw(canvas); |
| 233 | |
| 234 | // If we have any indicators to draw, we do it here |
| 235 | if ((mChildIndicator == null) && (mGroupIndicator == null)) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | int saveCount = 0; |
| 240 | final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 241 | if (clipToPadding) { |
| 242 | saveCount = canvas.save(); |
| 243 | final int scrollX = mScrollX; |
| 244 | final int scrollY = mScrollY; |
| 245 | canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop, |
| 246 | scrollX + mRight - mLeft - mPaddingRight, |
| 247 | scrollY + mBottom - mTop - mPaddingBottom); |
| 248 | } |
| 249 | |
| 250 | final int headerViewsCount = getHeaderViewsCount(); |
| 251 | |
| 252 | final int lastChildFlPos = mItemCount - getFooterViewsCount() - headerViewsCount - 1; |
| 253 | |
| 254 | final int myB = mBottom; |
| 255 | |
| 256 | PositionMetadata pos; |
| 257 | View item; |
| 258 | Drawable indicator; |
| 259 | int t, b; |
| 260 | |
| 261 | // Start at a value that is neither child nor group |
| 262 | int lastItemType = ~(ExpandableListPosition.CHILD | ExpandableListPosition.GROUP); |
| 263 | |
| 264 | final Rect indicatorRect = mIndicatorRect; |
| 265 | |
| 266 | // The "child" mentioned in the following two lines is this |
| 267 | // View's child, not referring to an expandable list's |
| 268 | // notion of a child (as opposed to a group) |
| 269 | final int childCount = getChildCount(); |
| 270 | for (int i = 0, childFlPos = mFirstPosition - headerViewsCount; i < childCount; |
| 271 | i++, childFlPos++) { |
| 272 | |
| 273 | if (childFlPos < 0) { |
| 274 | // This child is header |
| 275 | continue; |
| 276 | } else if (childFlPos > lastChildFlPos) { |
| 277 | // This child is footer, so are all subsequent children |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | item = getChildAt(i); |
| 282 | t = item.getTop(); |
| 283 | b = item.getBottom(); |
| 284 | |
| 285 | // This item isn't on the screen |
| 286 | if ((b < 0) || (t > myB)) continue; |
| 287 | |
| 288 | // Get more expandable list-related info for this item |
| 289 | pos = mConnector.getUnflattenedPos(childFlPos); |
| 290 | |
| 291 | // If this item type and the previous item type are different, then we need to change |
| 292 | // the left & right bounds |
| 293 | if (pos.position.type != lastItemType) { |
| 294 | if (pos.position.type == ExpandableListPosition.CHILD) { |
| 295 | indicatorRect.left = (mChildIndicatorLeft == CHILD_INDICATOR_INHERIT) ? |
| 296 | mIndicatorLeft : mChildIndicatorLeft; |
| 297 | indicatorRect.right = (mChildIndicatorRight == CHILD_INDICATOR_INHERIT) ? |
| 298 | mIndicatorRight : mChildIndicatorRight; |
| 299 | } else { |
| 300 | indicatorRect.left = mIndicatorLeft; |
| 301 | indicatorRect.right = mIndicatorRight; |
| 302 | } |
| 303 | |
Adam Powell | 3e5e6ac | 2011-02-28 21:55:52 -0800 | [diff] [blame] | 304 | indicatorRect.left += mPaddingLeft; |
| 305 | indicatorRect.right += mPaddingLeft; |
| 306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | lastItemType = pos.position.type; |
| 308 | } |
| 309 | |
| 310 | if (indicatorRect.left != indicatorRect.right) { |
| 311 | // Use item's full height + the divider height |
| 312 | if (mStackFromBottom) { |
| 313 | // See ListView#dispatchDraw |
| 314 | indicatorRect.top = t;// - mDividerHeight; |
| 315 | indicatorRect.bottom = b; |
| 316 | } else { |
| 317 | indicatorRect.top = t; |
| 318 | indicatorRect.bottom = b;// + mDividerHeight; |
| 319 | } |
| 320 | |
| 321 | // Get the indicator (with its state set to the item's state) |
| 322 | indicator = getIndicator(pos); |
| 323 | if (indicator != null) { |
| 324 | // Draw the indicator |
| 325 | indicator.setBounds(indicatorRect); |
| 326 | indicator.draw(canvas); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | pos.recycle(); |
| 331 | } |
| 332 | |
| 333 | if (clipToPadding) { |
| 334 | canvas.restoreToCount(saveCount); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Gets the indicator for the item at the given position. If the indicator |
| 340 | * is stateful, the state will be given to the indicator. |
| 341 | * |
| 342 | * @param pos The flat list position of the item whose indicator |
| 343 | * should be returned. |
| 344 | * @return The indicator in the proper state. |
| 345 | */ |
| 346 | private Drawable getIndicator(PositionMetadata pos) { |
| 347 | Drawable indicator; |
| 348 | |
| 349 | if (pos.position.type == ExpandableListPosition.GROUP) { |
| 350 | indicator = mGroupIndicator; |
| 351 | |
| 352 | if (indicator != null && indicator.isStateful()) { |
| 353 | // Empty check based on availability of data. If the groupMetadata isn't null, |
| 354 | // we do a check on it. Otherwise, the group is collapsed so we consider it |
| 355 | // empty for performance reasons. |
| 356 | boolean isEmpty = (pos.groupMetadata == null) || |
| 357 | (pos.groupMetadata.lastChildFlPos == pos.groupMetadata.flPos); |
| 358 | |
| 359 | final int stateSetIndex = |
| 360 | (pos.isExpanded() ? 1 : 0) | // Expanded? |
| 361 | (isEmpty ? 2 : 0); // Empty? |
| 362 | indicator.setState(GROUP_STATE_SETS[stateSetIndex]); |
| 363 | } |
| 364 | } else { |
| 365 | indicator = mChildIndicator; |
| 366 | |
| 367 | if (indicator != null && indicator.isStateful()) { |
| 368 | // No need for a state sets array for the child since it only has two states |
| 369 | final int stateSet[] = pos.position.flatListPos == pos.groupMetadata.lastChildFlPos |
| 370 | ? CHILD_LAST_STATE_SET |
| 371 | : EMPTY_STATE_SET; |
| 372 | indicator.setState(stateSet); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return indicator; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Sets the drawable that will be drawn adjacent to every child in the list. This will |
| 381 | * be drawn using the same height as the normal divider ({@link #setDivider(Drawable)}) or |
| 382 | * if it does not have an intrinsic height, the height set by {@link #setDividerHeight(int)}. |
| 383 | * |
| 384 | * @param childDivider The drawable to use. |
| 385 | */ |
| 386 | public void setChildDivider(Drawable childDivider) { |
| 387 | mChildDivider = childDivider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
| 391 | void drawDivider(Canvas canvas, Rect bounds, int childIndex) { |
| 392 | int flatListPosition = childIndex + mFirstPosition; |
| 393 | |
| 394 | // Only proceed as possible child if the divider isn't above all items (if it is above |
| 395 | // all items, then the item below it has to be a group) |
| 396 | if (flatListPosition >= 0) { |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 397 | final int adjustedPosition = getFlatPositionForConnector(flatListPosition); |
| 398 | PositionMetadata pos = mConnector.getUnflattenedPos(adjustedPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | // If this item is a child, or it is a non-empty group that is expanded |
| 400 | if ((pos.position.type == ExpandableListPosition.CHILD) || (pos.isExpanded() && |
| 401 | pos.groupMetadata.lastChildFlPos != pos.groupMetadata.flPos)) { |
| 402 | // These are the cases where we draw the child divider |
| 403 | final Drawable divider = mChildDivider; |
Romain Guy | 95930e1 | 2010-10-04 13:46:02 -0700 | [diff] [blame] | 404 | divider.setBounds(bounds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | divider.draw(canvas); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | pos.recycle(); |
| 407 | return; |
| 408 | } |
| 409 | pos.recycle(); |
| 410 | } |
| 411 | |
| 412 | // Otherwise draw the default divider |
| 413 | super.drawDivider(canvas, bounds, flatListPosition); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * This overloaded method should not be used, instead use |
| 418 | * {@link #setAdapter(ExpandableListAdapter)}. |
| 419 | * <p> |
| 420 | * {@inheritDoc} |
| 421 | */ |
| 422 | @Override |
| 423 | public void setAdapter(ListAdapter adapter) { |
| 424 | throw new RuntimeException( |
| 425 | "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " + |
| 426 | "setAdapter(ListAdapter)"); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * This method should not be used, use {@link #getExpandableListAdapter()}. |
| 431 | */ |
| 432 | @Override |
| 433 | public ListAdapter getAdapter() { |
| 434 | /* |
| 435 | * The developer should never really call this method on an |
| 436 | * ExpandableListView, so it would be nice to throw a RuntimeException, |
| 437 | * but AdapterView calls this |
| 438 | */ |
| 439 | return super.getAdapter(); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Register a callback to be invoked when an item has been clicked and the |
| 444 | * caller prefers to receive a ListView-style position instead of a group |
| 445 | * and/or child position. In most cases, the caller should use |
| 446 | * {@link #setOnGroupClickListener} and/or {@link #setOnChildClickListener}. |
| 447 | * <p /> |
| 448 | * {@inheritDoc} |
| 449 | */ |
| 450 | @Override |
| 451 | public void setOnItemClickListener(OnItemClickListener l) { |
| 452 | super.setOnItemClickListener(l); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Sets the adapter that provides data to this view. |
| 457 | * @param adapter The adapter that provides data to this view. |
| 458 | */ |
| 459 | public void setAdapter(ExpandableListAdapter adapter) { |
| 460 | // Set member variable |
| 461 | mAdapter = adapter; |
| 462 | |
| 463 | if (adapter != null) { |
| 464 | // Create the connector |
| 465 | mConnector = new ExpandableListConnector(adapter); |
| 466 | } else { |
| 467 | mConnector = null; |
| 468 | } |
| 469 | |
| 470 | // Link the ListView (superclass) to the expandable list data through the connector |
| 471 | super.setAdapter(mConnector); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Gets the adapter that provides data to this view. |
| 476 | * @return The adapter that provides data to this view. |
| 477 | */ |
| 478 | public ExpandableListAdapter getExpandableListAdapter() { |
| 479 | return mAdapter; |
| 480 | } |
| 481 | |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 482 | /** |
| 483 | * @param position An absolute (including header and footer) flat list position. |
| 484 | * @return true if the position corresponds to a header or a footer item. |
| 485 | */ |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 486 | private boolean isHeaderOrFooterPosition(int position) { |
| 487 | final int footerViewsStart = mItemCount - getFooterViewsCount(); |
| 488 | return (position < getHeaderViewsCount() || position >= footerViewsStart); |
| 489 | } |
| 490 | |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Converts an absolute item flat position into a group/child flat position, shifting according |
| 493 | * to the number of header items. |
| 494 | * |
| 495 | * @param flatListPosition The absolute flat position |
| 496 | * @return A group/child flat position as expected by the connector. |
| 497 | */ |
| 498 | private int getFlatPositionForConnector(int flatListPosition) { |
| 499 | return flatListPosition - getHeaderViewsCount(); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Converts a group/child flat position into an absolute flat position, that takes into account |
| 504 | * the possible headers. |
| 505 | * |
| 506 | * @param flatListPosition The child/group flat position |
| 507 | * @return An absolute flat position. |
| 508 | */ |
| 509 | private int getAbsoluteFlatPosition(int flatListPosition) { |
| 510 | return flatListPosition + getHeaderViewsCount(); |
| 511 | } |
| 512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | @Override |
| 514 | public boolean performItemClick(View v, int position, long id) { |
| 515 | // Ignore clicks in header/footers |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 516 | if (isHeaderOrFooterPosition(position)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | // Clicked on a header/footer, so ignore pass it on to super |
| 518 | return super.performItemClick(v, position, id); |
| 519 | } |
| 520 | |
| 521 | // Internally handle the item click |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 522 | final int adjustedPosition = getFlatPositionForConnector(position); |
| 523 | return handleItemClick(v, adjustedPosition, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /** |
| 527 | * This will either expand/collapse groups (if a group was clicked) or pass |
| 528 | * on the click to the proper child (if a child was clicked) |
| 529 | * |
| 530 | * @param position The flat list position. This has already been factored to |
| 531 | * remove the header/footer. |
| 532 | * @param id The ListAdapter ID, not the group or child ID. |
| 533 | */ |
| 534 | boolean handleItemClick(View v, int position, long id) { |
| 535 | final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position); |
| 536 | |
| 537 | id = getChildOrGroupId(posMetadata.position); |
| 538 | |
| 539 | boolean returnValue; |
| 540 | if (posMetadata.position.type == ExpandableListPosition.GROUP) { |
| 541 | /* It's a group, so handle collapsing/expanding */ |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 542 | |
| 543 | /* It's a group click, so pass on event */ |
| 544 | if (mOnGroupClickListener != null) { |
| 545 | if (mOnGroupClickListener.onGroupClick(this, v, |
| 546 | posMetadata.position.groupPos, id)) { |
| 547 | posMetadata.recycle(); |
| 548 | return true; |
| 549 | } |
| 550 | } |
| 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | if (posMetadata.isExpanded()) { |
| 553 | /* Collapse it */ |
| 554 | mConnector.collapseGroup(posMetadata); |
| 555 | |
| 556 | playSoundEffect(SoundEffectConstants.CLICK); |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | if (mOnGroupCollapseListener != null) { |
| 559 | mOnGroupCollapseListener.onGroupCollapse(posMetadata.position.groupPos); |
| 560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | /* Expand it */ |
| 563 | mConnector.expandGroup(posMetadata); |
| 564 | |
| 565 | playSoundEffect(SoundEffectConstants.CLICK); |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | if (mOnGroupExpandListener != null) { |
| 568 | mOnGroupExpandListener.onGroupExpand(posMetadata.position.groupPos); |
| 569 | } |
Adam Powell | 4580347 | 2010-01-25 15:10:44 -0800 | [diff] [blame] | 570 | |
| 571 | final int groupPos = posMetadata.position.groupPos; |
| 572 | final int groupFlatPos = posMetadata.position.flatListPos; |
Gilles Debunne | 52b65d3 | 2010-03-01 10:54:51 -0800 | [diff] [blame] | 573 | |
| 574 | final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); |
| 575 | smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos), |
| 576 | shiftedGroupPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | returnValue = true; |
| 580 | } else { |
| 581 | /* It's a child, so pass on event */ |
| 582 | if (mOnChildClickListener != null) { |
| 583 | playSoundEffect(SoundEffectConstants.CLICK); |
| 584 | return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos, |
| 585 | posMetadata.position.childPos, id); |
| 586 | } |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | returnValue = false; |
| 589 | } |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 590 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | posMetadata.recycle(); |
Romain Guy | 4039545 | 2009-12-04 16:51:52 -0800 | [diff] [blame] | 592 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | return returnValue; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Expand a group in the grouped list view |
| 598 | * |
| 599 | * @param groupPos the group to be expanded |
| 600 | * @return True if the group was expanded, false otherwise (if the group |
| 601 | * was already expanded, this will return false) |
| 602 | */ |
| 603 | public boolean expandGroup(int groupPos) { |
John Reck | 63f46e7 | 2011-05-19 10:09:36 -0700 | [diff] [blame] | 604 | return expandGroup(groupPos, false); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Expand a group in the grouped list view |
| 609 | * |
| 610 | * @param groupPos the group to be expanded |
| 611 | * @param animate true if the expanding group should be animated in |
| 612 | * @return True if the group was expanded, false otherwise (if the group |
| 613 | * was already expanded, this will return false) |
| 614 | */ |
| 615 | public boolean expandGroup(int groupPos, boolean animate) { |
| 616 | PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition.obtain( |
| 617 | ExpandableListPosition.GROUP, groupPos, -1, -1)); |
| 618 | boolean retValue = mConnector.expandGroup(pm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | |
| 620 | if (mOnGroupExpandListener != null) { |
| 621 | mOnGroupExpandListener.onGroupExpand(groupPos); |
| 622 | } |
John Reck | 63f46e7 | 2011-05-19 10:09:36 -0700 | [diff] [blame] | 623 | |
| 624 | if (animate) { |
| 625 | final int groupFlatPos = pm.position.flatListPos; |
| 626 | |
| 627 | final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); |
| 628 | smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos), |
| 629 | shiftedGroupPosition); |
| 630 | } |
| 631 | pm.recycle(); |
| 632 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | return retValue; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Collapse a group in the grouped list view |
| 638 | * |
| 639 | * @param groupPos position of the group to collapse |
| 640 | * @return True if the group was collapsed, false otherwise (if the group |
| 641 | * was already collapsed, this will return false) |
| 642 | */ |
| 643 | public boolean collapseGroup(int groupPos) { |
| 644 | boolean retValue = mConnector.collapseGroup(groupPos); |
| 645 | |
| 646 | if (mOnGroupCollapseListener != null) { |
| 647 | mOnGroupCollapseListener.onGroupCollapse(groupPos); |
| 648 | } |
| 649 | |
| 650 | return retValue; |
| 651 | } |
| 652 | |
| 653 | /** Used for being notified when a group is collapsed */ |
| 654 | public interface OnGroupCollapseListener { |
| 655 | /** |
| 656 | * Callback method to be invoked when a group in this expandable list has |
| 657 | * been collapsed. |
| 658 | * |
| 659 | * @param groupPosition The group position that was collapsed |
| 660 | */ |
| 661 | void onGroupCollapse(int groupPosition); |
| 662 | } |
| 663 | |
| 664 | private OnGroupCollapseListener mOnGroupCollapseListener; |
| 665 | |
| 666 | public void setOnGroupCollapseListener( |
| 667 | OnGroupCollapseListener onGroupCollapseListener) { |
| 668 | mOnGroupCollapseListener = onGroupCollapseListener; |
| 669 | } |
| 670 | |
| 671 | /** Used for being notified when a group is expanded */ |
| 672 | public interface OnGroupExpandListener { |
| 673 | /** |
| 674 | * Callback method to be invoked when a group in this expandable list has |
| 675 | * been expanded. |
| 676 | * |
| 677 | * @param groupPosition The group position that was expanded |
| 678 | */ |
| 679 | void onGroupExpand(int groupPosition); |
| 680 | } |
| 681 | |
| 682 | private OnGroupExpandListener mOnGroupExpandListener; |
| 683 | |
| 684 | public void setOnGroupExpandListener( |
| 685 | OnGroupExpandListener onGroupExpandListener) { |
| 686 | mOnGroupExpandListener = onGroupExpandListener; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Interface definition for a callback to be invoked when a group in this |
| 691 | * expandable list has been clicked. |
| 692 | */ |
| 693 | public interface OnGroupClickListener { |
| 694 | /** |
| 695 | * Callback method to be invoked when a group in this expandable list has |
| 696 | * been clicked. |
| 697 | * |
| 698 | * @param parent The ExpandableListConnector where the click happened |
| 699 | * @param v The view within the expandable list/ListView that was clicked |
| 700 | * @param groupPosition The group position that was clicked |
| 701 | * @param id The row id of the group that was clicked |
| 702 | * @return True if the click was handled |
| 703 | */ |
| 704 | boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, |
| 705 | long id); |
| 706 | } |
| 707 | |
| 708 | private OnGroupClickListener mOnGroupClickListener; |
| 709 | |
| 710 | public void setOnGroupClickListener(OnGroupClickListener onGroupClickListener) { |
| 711 | mOnGroupClickListener = onGroupClickListener; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Interface definition for a callback to be invoked when a child in this |
| 716 | * expandable list has been clicked. |
| 717 | */ |
| 718 | public interface OnChildClickListener { |
| 719 | /** |
| 720 | * Callback method to be invoked when a child in this expandable list has |
| 721 | * been clicked. |
| 722 | * |
| 723 | * @param parent The ExpandableListView where the click happened |
| 724 | * @param v The view within the expandable list/ListView that was clicked |
| 725 | * @param groupPosition The group position that contains the child that |
| 726 | * was clicked |
| 727 | * @param childPosition The child position within the group |
| 728 | * @param id The row id of the child that was clicked |
| 729 | * @return True if the click was handled |
| 730 | */ |
| 731 | boolean onChildClick(ExpandableListView parent, View v, int groupPosition, |
| 732 | int childPosition, long id); |
| 733 | } |
| 734 | |
| 735 | private OnChildClickListener mOnChildClickListener; |
| 736 | |
| 737 | public void setOnChildClickListener(OnChildClickListener onChildClickListener) { |
| 738 | mOnChildClickListener = onChildClickListener; |
| 739 | } |
| 740 | |
| 741 | /** |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 742 | * Converts a flat list position (the raw position of an item (child or group) |
| 743 | * in the list) to an group and/or child position (represented in a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | * packed position). This is useful in situations where the caller needs to |
| 745 | * use the underlying {@link ListView}'s methods. Use |
| 746 | * {@link ExpandableListView#getPackedPositionType} , |
| 747 | * {@link ExpandableListView#getPackedPositionChild}, |
| 748 | * {@link ExpandableListView#getPackedPositionGroup} to unpack. |
| 749 | * |
| 750 | * @param flatListPosition The flat list position to be converted. |
| 751 | * @return The group and/or child position for the given flat list position |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 752 | * in packed position representation. #PACKED_POSITION_VALUE_NULL if |
| 753 | * the position corresponds to a header or a footer item. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | */ |
| 755 | public long getExpandableListPosition(int flatListPosition) { |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 756 | if (isHeaderOrFooterPosition(flatListPosition)) { |
| 757 | return PACKED_POSITION_VALUE_NULL; |
| 758 | } |
| 759 | |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 760 | final int adjustedPosition = getFlatPositionForConnector(flatListPosition); |
| 761 | PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | long packedPos = pm.position.getPackedPosition(); |
| 763 | pm.recycle(); |
| 764 | return packedPos; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Converts a group and/or child position to a flat list position. This is |
| 769 | * useful in situations where the caller needs to use the underlying |
| 770 | * {@link ListView}'s methods. |
| 771 | * |
| 772 | * @param packedPosition The group and/or child positions to be converted in |
| 773 | * packed position representation. Use |
| 774 | * {@link #getPackedPositionForChild(int, int)} or |
| 775 | * {@link #getPackedPositionForGroup(int)}. |
| 776 | * @return The flat list position for the given child or group. |
| 777 | */ |
| 778 | public int getFlatListPosition(long packedPosition) { |
| 779 | PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition |
| 780 | .obtainPosition(packedPosition)); |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 781 | final int flatListPosition = pm.position.flatListPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | pm.recycle(); |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 783 | return getAbsoluteFlatPosition(flatListPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Gets the position of the currently selected group or child (along with |
| 788 | * its type). Can return {@link #PACKED_POSITION_VALUE_NULL} if no selection. |
| 789 | * |
| 790 | * @return A packed position containing the currently selected group or |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 791 | * child's position and type. #PACKED_POSITION_VALUE_NULL if no selection |
| 792 | * or if selection is on a header or a footer item. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | */ |
| 794 | public long getSelectedPosition() { |
| 795 | final int selectedPos = getSelectedItemPosition(); |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 796 | |
| 797 | // The case where there is no selection (selectedPos == -1) is also handled here. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | return getExpandableListPosition(selectedPos); |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Gets the ID of the currently selected group or child. Can return -1 if no |
| 803 | * selection. |
| 804 | * |
| 805 | * @return The ID of the currently selected group or child. -1 if no |
| 806 | * selection. |
| 807 | */ |
| 808 | public long getSelectedId() { |
| 809 | long packedPos = getSelectedPosition(); |
| 810 | if (packedPos == PACKED_POSITION_VALUE_NULL) return -1; |
| 811 | |
| 812 | int groupPos = getPackedPositionGroup(packedPos); |
| 813 | |
| 814 | if (getPackedPositionType(packedPos) == PACKED_POSITION_TYPE_GROUP) { |
| 815 | // It's a group |
| 816 | return mAdapter.getGroupId(groupPos); |
| 817 | } else { |
| 818 | // It's a child |
| 819 | return mAdapter.getChildId(groupPos, getPackedPositionChild(packedPos)); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Sets the selection to the specified group. |
| 825 | * @param groupPosition The position of the group that should be selected. |
| 826 | */ |
| 827 | public void setSelectedGroup(int groupPosition) { |
| 828 | ExpandableListPosition elGroupPos = ExpandableListPosition |
| 829 | .obtainGroupPosition(groupPosition); |
| 830 | PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos); |
| 831 | elGroupPos.recycle(); |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 832 | final int absoluteFlatPosition = getAbsoluteFlatPosition(pm.position.flatListPos); |
| 833 | super.setSelection(absoluteFlatPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | pm.recycle(); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Sets the selection to the specified child. If the child is in a collapsed |
| 839 | * group, the group will only be expanded and child subsequently selected if |
| 840 | * shouldExpandGroup is set to true, otherwise the method will return false. |
| 841 | * |
| 842 | * @param groupPosition The position of the group that contains the child. |
| 843 | * @param childPosition The position of the child within the group. |
| 844 | * @param shouldExpandGroup Whether the child's group should be expanded if |
| 845 | * it is collapsed. |
| 846 | * @return Whether the selection was successfully set on the child. |
| 847 | */ |
| 848 | public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) { |
| 849 | ExpandableListPosition elChildPos = ExpandableListPosition.obtainChildPosition( |
| 850 | groupPosition, childPosition); |
| 851 | PositionMetadata flatChildPos = mConnector.getFlattenedPos(elChildPos); |
| 852 | |
| 853 | if (flatChildPos == null) { |
| 854 | // The child's group isn't expanded |
| 855 | |
| 856 | // Shouldn't expand the group, so return false for we didn't set the selection |
| 857 | if (!shouldExpandGroup) return false; |
| 858 | |
| 859 | expandGroup(groupPosition); |
| 860 | |
| 861 | flatChildPos = mConnector.getFlattenedPos(elChildPos); |
| 862 | |
| 863 | // Sanity check |
| 864 | if (flatChildPos == null) { |
| 865 | throw new IllegalStateException("Could not find child"); |
| 866 | } |
| 867 | } |
| 868 | |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 869 | int absoluteFlatPosition = getAbsoluteFlatPosition(flatChildPos.position.flatListPos); |
| 870 | super.setSelection(absoluteFlatPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | |
| 872 | elChildPos.recycle(); |
| 873 | flatChildPos.recycle(); |
| 874 | |
| 875 | return true; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Whether the given group is currently expanded. |
| 880 | * |
| 881 | * @param groupPosition The group to check. |
| 882 | * @return Whether the group is currently expanded. |
| 883 | */ |
| 884 | public boolean isGroupExpanded(int groupPosition) { |
| 885 | return mConnector.isGroupExpanded(groupPosition); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Gets the type of a packed position. See |
| 890 | * {@link #getPackedPositionForChild(int, int)}. |
| 891 | * |
| 892 | * @param packedPosition The packed position for which to return the type. |
| 893 | * @return The type of the position contained within the packed position, |
| 894 | * either {@link #PACKED_POSITION_TYPE_CHILD}, {@link #PACKED_POSITION_TYPE_GROUP}, or |
| 895 | * {@link #PACKED_POSITION_TYPE_NULL}. |
| 896 | */ |
| 897 | public static int getPackedPositionType(long packedPosition) { |
| 898 | if (packedPosition == PACKED_POSITION_VALUE_NULL) { |
| 899 | return PACKED_POSITION_TYPE_NULL; |
| 900 | } |
| 901 | |
| 902 | return (packedPosition & PACKED_POSITION_MASK_TYPE) == PACKED_POSITION_MASK_TYPE |
| 903 | ? PACKED_POSITION_TYPE_CHILD |
| 904 | : PACKED_POSITION_TYPE_GROUP; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Gets the group position from a packed position. See |
| 909 | * {@link #getPackedPositionForChild(int, int)}. |
| 910 | * |
| 911 | * @param packedPosition The packed position from which the group position |
| 912 | * will be returned. |
| 913 | * @return The group position portion of the packed position. If this does |
| 914 | * not contain a group, returns -1. |
| 915 | */ |
| 916 | public static int getPackedPositionGroup(long packedPosition) { |
| 917 | // Null |
| 918 | if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1; |
| 919 | |
| 920 | return (int) ((packedPosition & PACKED_POSITION_MASK_GROUP) >> PACKED_POSITION_SHIFT_GROUP); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Gets the child position from a packed position that is of |
| 925 | * {@link #PACKED_POSITION_TYPE_CHILD} type (use {@link #getPackedPositionType(long)}). |
| 926 | * To get the group that this child belongs to, use |
| 927 | * {@link #getPackedPositionGroup(long)}. See |
| 928 | * {@link #getPackedPositionForChild(int, int)}. |
| 929 | * |
| 930 | * @param packedPosition The packed position from which the child position |
| 931 | * will be returned. |
| 932 | * @return The child position portion of the packed position. If this does |
| 933 | * not contain a child, returns -1. |
| 934 | */ |
| 935 | public static int getPackedPositionChild(long packedPosition) { |
| 936 | // Null |
| 937 | if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1; |
| 938 | |
| 939 | // Group since a group type clears this bit |
| 940 | if ((packedPosition & PACKED_POSITION_MASK_TYPE) != PACKED_POSITION_MASK_TYPE) return -1; |
| 941 | |
| 942 | return (int) (packedPosition & PACKED_POSITION_MASK_CHILD); |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * Returns the packed position representation of a child's position. |
| 947 | * <p> |
| 948 | * In general, a packed position should be used in |
| 949 | * situations where the position given to/returned from an |
| 950 | * {@link ExpandableListAdapter} or {@link ExpandableListView} method can |
| 951 | * either be a child or group. The two positions are packed into a single |
| 952 | * long which can be unpacked using |
| 953 | * {@link #getPackedPositionChild(long)}, |
| 954 | * {@link #getPackedPositionGroup(long)}, and |
| 955 | * {@link #getPackedPositionType(long)}. |
| 956 | * |
| 957 | * @param groupPosition The child's parent group's position. |
| 958 | * @param childPosition The child position within the group. |
| 959 | * @return The packed position representation of the child (and parent group). |
| 960 | */ |
| 961 | public static long getPackedPositionForChild(int groupPosition, int childPosition) { |
| 962 | return (((long)PACKED_POSITION_TYPE_CHILD) << PACKED_POSITION_SHIFT_TYPE) |
| 963 | | ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP) |
| 964 | << PACKED_POSITION_SHIFT_GROUP) |
| 965 | | (childPosition & PACKED_POSITION_INT_MASK_CHILD); |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Returns the packed position representation of a group's position. See |
| 970 | * {@link #getPackedPositionForChild(int, int)}. |
| 971 | * |
| 972 | * @param groupPosition The child's parent group's position. |
| 973 | * @return The packed position representation of the group. |
| 974 | */ |
| 975 | public static long getPackedPositionForGroup(int groupPosition) { |
| 976 | // No need to OR a type in because PACKED_POSITION_GROUP == 0 |
| 977 | return ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP) |
| 978 | << PACKED_POSITION_SHIFT_GROUP); |
| 979 | } |
| 980 | |
| 981 | @Override |
| 982 | ContextMenuInfo createContextMenuInfo(View view, int flatListPosition, long id) { |
Gilles Debunne | 47ccdf3 | 2010-02-26 11:30:24 -0800 | [diff] [blame] | 983 | if (isHeaderOrFooterPosition(flatListPosition)) { |
| 984 | // Return normal info for header/footer view context menus |
Jeff Sharkey | fd0d627 | 2009-08-17 00:52:46 -0700 | [diff] [blame] | 985 | return new AdapterContextMenuInfo(view, flatListPosition, id); |
| 986 | } |
| 987 | |
Gilles Debunne | 272f3a9 | 2010-03-03 15:55:13 -0800 | [diff] [blame] | 988 | final int adjustedPosition = getFlatPositionForConnector(flatListPosition); |
Jeff Sharkey | fd0d627 | 2009-08-17 00:52:46 -0700 | [diff] [blame] | 989 | PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | ExpandableListPosition pos = pm.position; |
| 991 | pm.recycle(); |
| 992 | |
| 993 | id = getChildOrGroupId(pos); |
| 994 | long packedPosition = pos.getPackedPosition(); |
| 995 | pos.recycle(); |
| 996 | |
| 997 | return new ExpandableListContextMenuInfo(view, packedPosition, id); |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Gets the ID of the group or child at the given <code>position</code>. |
| 1002 | * This is useful since there is no ListAdapter ID -> ExpandableListAdapter |
| 1003 | * ID conversion mechanism (in some cases, it isn't possible). |
| 1004 | * |
| 1005 | * @param position The position of the child or group whose ID should be |
| 1006 | * returned. |
| 1007 | */ |
| 1008 | private long getChildOrGroupId(ExpandableListPosition position) { |
| 1009 | if (position.type == ExpandableListPosition.CHILD) { |
| 1010 | return mAdapter.getChildId(position.groupPos, position.childPos); |
| 1011 | } else { |
| 1012 | return mAdapter.getGroupId(position.groupPos); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * Sets the indicator to be drawn next to a child. |
| 1018 | * |
| 1019 | * @param childIndicator The drawable to be used as an indicator. If the |
| 1020 | * child is the last child for a group, the state |
| 1021 | * {@link android.R.attr#state_last} will be set. |
| 1022 | */ |
| 1023 | public void setChildIndicator(Drawable childIndicator) { |
| 1024 | mChildIndicator = childIndicator; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * Sets the drawing bounds for the child indicator. For either, you can |
| 1029 | * specify {@link #CHILD_INDICATOR_INHERIT} to use inherit from the general |
| 1030 | * indicator's bounds. |
| 1031 | * |
| 1032 | * @see #setIndicatorBounds(int, int) |
| 1033 | * @param left The left position (relative to the left bounds of this View) |
| 1034 | * to start drawing the indicator. |
| 1035 | * @param right The right position (relative to the left bounds of this |
| 1036 | * View) to end the drawing of the indicator. |
| 1037 | */ |
| 1038 | public void setChildIndicatorBounds(int left, int right) { |
| 1039 | mChildIndicatorLeft = left; |
| 1040 | mChildIndicatorRight = right; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * Sets the indicator to be drawn next to a group. |
| 1045 | * |
| 1046 | * @param groupIndicator The drawable to be used as an indicator. If the |
| 1047 | * group is empty, the state {@link android.R.attr#state_empty} will be |
| 1048 | * set. If the group is expanded, the state |
| 1049 | * {@link android.R.attr#state_expanded} will be set. |
| 1050 | */ |
| 1051 | public void setGroupIndicator(Drawable groupIndicator) { |
| 1052 | mGroupIndicator = groupIndicator; |
Romain Guy | 875862e | 2011-01-17 11:37:24 -0800 | [diff] [blame] | 1053 | if (mIndicatorRight == 0 && mGroupIndicator != null) { |
| 1054 | mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth(); |
| 1055 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | } |
Romain Guy | 875862e | 2011-01-17 11:37:24 -0800 | [diff] [blame] | 1057 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | /** |
| 1059 | * Sets the drawing bounds for the indicators (at minimum, the group indicator |
| 1060 | * is affected by this; the child indicator is affected by this if the |
| 1061 | * child indicator bounds are set to inherit). |
| 1062 | * |
| 1063 | * @see #setChildIndicatorBounds(int, int) |
| 1064 | * @param left The left position (relative to the left bounds of this View) |
| 1065 | * to start drawing the indicator. |
| 1066 | * @param right The right position (relative to the left bounds of this |
| 1067 | * View) to end the drawing of the indicator. |
| 1068 | */ |
| 1069 | public void setIndicatorBounds(int left, int right) { |
| 1070 | mIndicatorLeft = left; |
| 1071 | mIndicatorRight = right; |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * Extra menu information specific to an {@link ExpandableListView} provided |
| 1076 | * to the |
| 1077 | * {@link android.view.View.OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo) } |
| 1078 | * callback when a context menu is brought up for this AdapterView. |
| 1079 | */ |
| 1080 | public static class ExpandableListContextMenuInfo implements ContextMenu.ContextMenuInfo { |
| 1081 | |
| 1082 | public ExpandableListContextMenuInfo(View targetView, long packedPosition, long id) { |
| 1083 | this.targetView = targetView; |
| 1084 | this.packedPosition = packedPosition; |
| 1085 | this.id = id; |
| 1086 | } |
| 1087 | |
| 1088 | /** |
| 1089 | * The view for which the context menu is being displayed. This |
| 1090 | * will be one of the children Views of this {@link ExpandableListView}. |
| 1091 | */ |
| 1092 | public View targetView; |
| 1093 | |
| 1094 | /** |
| 1095 | * The packed position in the list represented by the adapter for which |
| 1096 | * the context menu is being displayed. Use the methods |
| 1097 | * {@link ExpandableListView#getPackedPositionType}, |
| 1098 | * {@link ExpandableListView#getPackedPositionChild}, and |
| 1099 | * {@link ExpandableListView#getPackedPositionGroup} to unpack this. |
| 1100 | */ |
| 1101 | public long packedPosition; |
| 1102 | |
| 1103 | /** |
| 1104 | * The ID of the item (group or child) for which the context menu is |
| 1105 | * being displayed. |
| 1106 | */ |
| 1107 | public long id; |
| 1108 | } |
| 1109 | |
| 1110 | static class SavedState extends BaseSavedState { |
| 1111 | ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList; |
| 1112 | |
| 1113 | /** |
| 1114 | * Constructor called from {@link ExpandableListView#onSaveInstanceState()} |
| 1115 | */ |
| 1116 | SavedState( |
| 1117 | Parcelable superState, |
| 1118 | ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList) { |
| 1119 | super(superState); |
| 1120 | this.expandedGroupMetadataList = expandedGroupMetadataList; |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Constructor called from {@link #CREATOR} |
| 1125 | */ |
| 1126 | private SavedState(Parcel in) { |
| 1127 | super(in); |
| 1128 | expandedGroupMetadataList = new ArrayList<ExpandableListConnector.GroupMetadata>(); |
| 1129 | in.readList(expandedGroupMetadataList, ExpandableListConnector.class.getClassLoader()); |
| 1130 | } |
| 1131 | |
| 1132 | @Override |
| 1133 | public void writeToParcel(Parcel out, int flags) { |
| 1134 | super.writeToParcel(out, flags); |
| 1135 | out.writeList(expandedGroupMetadataList); |
| 1136 | } |
| 1137 | |
| 1138 | public static final Parcelable.Creator<SavedState> CREATOR |
| 1139 | = new Parcelable.Creator<SavedState>() { |
| 1140 | public SavedState createFromParcel(Parcel in) { |
| 1141 | return new SavedState(in); |
| 1142 | } |
| 1143 | |
| 1144 | public SavedState[] newArray(int size) { |
| 1145 | return new SavedState[size]; |
| 1146 | } |
| 1147 | }; |
| 1148 | } |
| 1149 | |
| 1150 | @Override |
| 1151 | public Parcelable onSaveInstanceState() { |
| 1152 | Parcelable superState = super.onSaveInstanceState(); |
| 1153 | return new SavedState(superState, |
| 1154 | mConnector != null ? mConnector.getExpandedGroupMetadataList() : null); |
| 1155 | } |
| 1156 | |
| 1157 | @Override |
| 1158 | public void onRestoreInstanceState(Parcelable state) { |
Romain Guy | 7444e14 | 2009-05-21 12:54:16 -0700 | [diff] [blame] | 1159 | if (!(state instanceof SavedState)) { |
| 1160 | super.onRestoreInstanceState(state); |
| 1161 | return; |
| 1162 | } |
| 1163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | SavedState ss = (SavedState) state; |
| 1165 | super.onRestoreInstanceState(ss.getSuperState()); |
| 1166 | |
| 1167 | if (mConnector != null && ss.expandedGroupMetadataList != null) { |
| 1168 | mConnector.setExpandedGroupMetadataList(ss.expandedGroupMetadataList); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame^] | 1172 | @Override |
| 1173 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1174 | super.onInitializeAccessibilityEvent(event); |
| 1175 | event.setClassName(ExpandableListView.class.getName()); |
| 1176 | } |
| 1177 | |
| 1178 | @Override |
| 1179 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1180 | super.onInitializeAccessibilityNodeInfo(info); |
| 1181 | info.setClassName(ExpandableListView.class.getName()); |
| 1182 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | } |