ExpandableListView.java revision 605c5af9c8d36aab16fb9d2313fecc57301104bd
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 17package android.widget; 18 19import com.android.internal.R; 20 21import android.content.Context; 22import android.content.res.TypedArray; 23import android.graphics.Canvas; 24import android.graphics.Rect; 25import android.graphics.drawable.Drawable; 26import android.os.Parcel; 27import android.os.Parcelable; 28import android.util.AttributeSet; 29import android.view.ContextMenu; 30import android.view.SoundEffectConstants; 31import android.view.View; 32import android.view.ContextMenu.ContextMenuInfo; 33import android.view.accessibility.AccessibilityEvent; 34import android.view.accessibility.AccessibilityNodeInfo; 35import android.widget.ExpandableListConnector.PositionMetadata; 36 37import java.util.ArrayList; 38 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 */ 80public 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; 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); 216 if (mIndicatorRight == 0 && mGroupIndicator != null) { 217 mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth(); 218 } 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 final boolean isLayoutRtl = isLayoutRtl(); 292 final int width = getWidth(); 293 294 // If this item type and the previous item type are different, then we need to change 295 // the left & right bounds 296 if (pos.position.type != lastItemType) { 297 if (pos.position.type == ExpandableListPosition.CHILD) { 298 indicatorRect.left = (mChildIndicatorLeft == CHILD_INDICATOR_INHERIT) ? 299 mIndicatorLeft : mChildIndicatorLeft; 300 indicatorRect.right = (mChildIndicatorRight == CHILD_INDICATOR_INHERIT) ? 301 mIndicatorRight : mChildIndicatorRight; 302 } else { 303 indicatorRect.left = mIndicatorLeft; 304 indicatorRect.right = mIndicatorRight; 305 } 306 307 if (isLayoutRtl) { 308 final int temp = indicatorRect.left; 309 indicatorRect.left = width - indicatorRect.right; 310 indicatorRect.right = width - temp; 311 312 indicatorRect.left -= mPaddingRight; 313 indicatorRect.right -= mPaddingRight; 314 } else { 315 indicatorRect.left += mPaddingLeft; 316 indicatorRect.right += mPaddingLeft; 317 } 318 319 lastItemType = pos.position.type; 320 } 321 322 if (indicatorRect.left != indicatorRect.right) { 323 // Use item's full height + the divider height 324 if (mStackFromBottom) { 325 // See ListView#dispatchDraw 326 indicatorRect.top = t;// - mDividerHeight; 327 indicatorRect.bottom = b; 328 } else { 329 indicatorRect.top = t; 330 indicatorRect.bottom = b;// + mDividerHeight; 331 } 332 333 // Get the indicator (with its state set to the item's state) 334 indicator = getIndicator(pos); 335 if (indicator != null) { 336 // Draw the indicator 337 indicator.setBounds(indicatorRect); 338 indicator.draw(canvas); 339 } 340 } 341 pos.recycle(); 342 } 343 344 if (clipToPadding) { 345 canvas.restoreToCount(saveCount); 346 } 347 } 348 349 /** 350 * Gets the indicator for the item at the given position. If the indicator 351 * is stateful, the state will be given to the indicator. 352 * 353 * @param pos The flat list position of the item whose indicator 354 * should be returned. 355 * @return The indicator in the proper state. 356 */ 357 private Drawable getIndicator(PositionMetadata pos) { 358 Drawable indicator; 359 360 if (pos.position.type == ExpandableListPosition.GROUP) { 361 indicator = mGroupIndicator; 362 363 if (indicator != null && indicator.isStateful()) { 364 // Empty check based on availability of data. If the groupMetadata isn't null, 365 // we do a check on it. Otherwise, the group is collapsed so we consider it 366 // empty for performance reasons. 367 boolean isEmpty = (pos.groupMetadata == null) || 368 (pos.groupMetadata.lastChildFlPos == pos.groupMetadata.flPos); 369 370 final int stateSetIndex = 371 (pos.isExpanded() ? 1 : 0) | // Expanded? 372 (isEmpty ? 2 : 0); // Empty? 373 indicator.setState(GROUP_STATE_SETS[stateSetIndex]); 374 } 375 } else { 376 indicator = mChildIndicator; 377 378 if (indicator != null && indicator.isStateful()) { 379 // No need for a state sets array for the child since it only has two states 380 final int stateSet[] = pos.position.flatListPos == pos.groupMetadata.lastChildFlPos 381 ? CHILD_LAST_STATE_SET 382 : EMPTY_STATE_SET; 383 indicator.setState(stateSet); 384 } 385 } 386 387 return indicator; 388 } 389 390 /** 391 * Sets the drawable that will be drawn adjacent to every child in the list. This will 392 * be drawn using the same height as the normal divider ({@link #setDivider(Drawable)}) or 393 * if it does not have an intrinsic height, the height set by {@link #setDividerHeight(int)}. 394 * 395 * @param childDivider The drawable to use. 396 */ 397 public void setChildDivider(Drawable childDivider) { 398 mChildDivider = childDivider; 399 } 400 401 @Override 402 void drawDivider(Canvas canvas, Rect bounds, int childIndex) { 403 int flatListPosition = childIndex + mFirstPosition; 404 405 // Only proceed as possible child if the divider isn't above all items (if it is above 406 // all items, then the item below it has to be a group) 407 if (flatListPosition >= 0) { 408 final int adjustedPosition = getFlatPositionForConnector(flatListPosition); 409 PositionMetadata pos = mConnector.getUnflattenedPos(adjustedPosition); 410 // If this item is a child, or it is a non-empty group that is expanded 411 if ((pos.position.type == ExpandableListPosition.CHILD) || (pos.isExpanded() && 412 pos.groupMetadata.lastChildFlPos != pos.groupMetadata.flPos)) { 413 // These are the cases where we draw the child divider 414 final Drawable divider = mChildDivider; 415 divider.setBounds(bounds); 416 divider.draw(canvas); 417 pos.recycle(); 418 return; 419 } 420 pos.recycle(); 421 } 422 423 // Otherwise draw the default divider 424 super.drawDivider(canvas, bounds, flatListPosition); 425 } 426 427 /** 428 * This overloaded method should not be used, instead use 429 * {@link #setAdapter(ExpandableListAdapter)}. 430 * <p> 431 * {@inheritDoc} 432 */ 433 @Override 434 public void setAdapter(ListAdapter adapter) { 435 throw new RuntimeException( 436 "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " + 437 "setAdapter(ListAdapter)"); 438 } 439 440 /** 441 * This method should not be used, use {@link #getExpandableListAdapter()}. 442 */ 443 @Override 444 public ListAdapter getAdapter() { 445 /* 446 * The developer should never really call this method on an 447 * ExpandableListView, so it would be nice to throw a RuntimeException, 448 * but AdapterView calls this 449 */ 450 return super.getAdapter(); 451 } 452 453 /** 454 * Register a callback to be invoked when an item has been clicked and the 455 * caller prefers to receive a ListView-style position instead of a group 456 * and/or child position. In most cases, the caller should use 457 * {@link #setOnGroupClickListener} and/or {@link #setOnChildClickListener}. 458 * <p /> 459 * {@inheritDoc} 460 */ 461 @Override 462 public void setOnItemClickListener(OnItemClickListener l) { 463 super.setOnItemClickListener(l); 464 } 465 466 /** 467 * Sets the adapter that provides data to this view. 468 * @param adapter The adapter that provides data to this view. 469 */ 470 public void setAdapter(ExpandableListAdapter adapter) { 471 // Set member variable 472 mAdapter = adapter; 473 474 if (adapter != null) { 475 // Create the connector 476 mConnector = new ExpandableListConnector(adapter); 477 } else { 478 mConnector = null; 479 } 480 481 // Link the ListView (superclass) to the expandable list data through the connector 482 super.setAdapter(mConnector); 483 } 484 485 /** 486 * Gets the adapter that provides data to this view. 487 * @return The adapter that provides data to this view. 488 */ 489 public ExpandableListAdapter getExpandableListAdapter() { 490 return mAdapter; 491 } 492 493 /** 494 * @param position An absolute (including header and footer) flat list position. 495 * @return true if the position corresponds to a header or a footer item. 496 */ 497 private boolean isHeaderOrFooterPosition(int position) { 498 final int footerViewsStart = mItemCount - getFooterViewsCount(); 499 return (position < getHeaderViewsCount() || position >= footerViewsStart); 500 } 501 502 /** 503 * Converts an absolute item flat position into a group/child flat position, shifting according 504 * to the number of header items. 505 * 506 * @param flatListPosition The absolute flat position 507 * @return A group/child flat position as expected by the connector. 508 */ 509 private int getFlatPositionForConnector(int flatListPosition) { 510 return flatListPosition - getHeaderViewsCount(); 511 } 512 513 /** 514 * Converts a group/child flat position into an absolute flat position, that takes into account 515 * the possible headers. 516 * 517 * @param flatListPosition The child/group flat position 518 * @return An absolute flat position. 519 */ 520 private int getAbsoluteFlatPosition(int flatListPosition) { 521 return flatListPosition + getHeaderViewsCount(); 522 } 523 524 @Override 525 public boolean performItemClick(View v, int position, long id) { 526 // Ignore clicks in header/footers 527 if (isHeaderOrFooterPosition(position)) { 528 // Clicked on a header/footer, so ignore pass it on to super 529 return super.performItemClick(v, position, id); 530 } 531 532 // Internally handle the item click 533 final int adjustedPosition = getFlatPositionForConnector(position); 534 return handleItemClick(v, adjustedPosition, id); 535 } 536 537 /** 538 * This will either expand/collapse groups (if a group was clicked) or pass 539 * on the click to the proper child (if a child was clicked) 540 * 541 * @param position The flat list position. This has already been factored to 542 * remove the header/footer. 543 * @param id The ListAdapter ID, not the group or child ID. 544 */ 545 boolean handleItemClick(View v, int position, long id) { 546 final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position); 547 548 id = getChildOrGroupId(posMetadata.position); 549 550 boolean returnValue; 551 if (posMetadata.position.type == ExpandableListPosition.GROUP) { 552 /* It's a group, so handle collapsing/expanding */ 553 554 /* It's a group click, so pass on event */ 555 if (mOnGroupClickListener != null) { 556 if (mOnGroupClickListener.onGroupClick(this, v, 557 posMetadata.position.groupPos, id)) { 558 posMetadata.recycle(); 559 return true; 560 } 561 } 562 563 if (posMetadata.isExpanded()) { 564 /* Collapse it */ 565 mConnector.collapseGroup(posMetadata); 566 567 playSoundEffect(SoundEffectConstants.CLICK); 568 569 if (mOnGroupCollapseListener != null) { 570 mOnGroupCollapseListener.onGroupCollapse(posMetadata.position.groupPos); 571 } 572 } else { 573 /* Expand it */ 574 mConnector.expandGroup(posMetadata); 575 576 playSoundEffect(SoundEffectConstants.CLICK); 577 578 if (mOnGroupExpandListener != null) { 579 mOnGroupExpandListener.onGroupExpand(posMetadata.position.groupPos); 580 } 581 582 final int groupPos = posMetadata.position.groupPos; 583 final int groupFlatPos = posMetadata.position.flatListPos; 584 585 final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); 586 smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos), 587 shiftedGroupPosition); 588 } 589 590 returnValue = true; 591 } else { 592 /* It's a child, so pass on event */ 593 if (mOnChildClickListener != null) { 594 playSoundEffect(SoundEffectConstants.CLICK); 595 return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos, 596 posMetadata.position.childPos, id); 597 } 598 599 returnValue = false; 600 } 601 602 posMetadata.recycle(); 603 604 return returnValue; 605 } 606 607 /** 608 * Expand a group in the grouped list view 609 * 610 * @param groupPos the group to be expanded 611 * @return True if the group was expanded, false otherwise (if the group 612 * was already expanded, this will return false) 613 */ 614 public boolean expandGroup(int groupPos) { 615 return expandGroup(groupPos, false); 616 } 617 618 /** 619 * Expand a group in the grouped list view 620 * 621 * @param groupPos the group to be expanded 622 * @param animate true if the expanding group should be animated in 623 * @return True if the group was expanded, false otherwise (if the group 624 * was already expanded, this will return false) 625 */ 626 public boolean expandGroup(int groupPos, boolean animate) { 627 ExpandableListPosition elGroupPos = ExpandableListPosition.obtain( 628 ExpandableListPosition.GROUP, groupPos, -1, -1); 629 PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos); 630 elGroupPos.recycle(); 631 boolean retValue = mConnector.expandGroup(pm); 632 633 if (mOnGroupExpandListener != null) { 634 mOnGroupExpandListener.onGroupExpand(groupPos); 635 } 636 637 if (animate) { 638 final int groupFlatPos = pm.position.flatListPos; 639 640 final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); 641 smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos), 642 shiftedGroupPosition); 643 } 644 pm.recycle(); 645 646 return retValue; 647 } 648 649 /** 650 * Collapse a group in the grouped list view 651 * 652 * @param groupPos position of the group to collapse 653 * @return True if the group was collapsed, false otherwise (if the group 654 * was already collapsed, this will return false) 655 */ 656 public boolean collapseGroup(int groupPos) { 657 boolean retValue = mConnector.collapseGroup(groupPos); 658 659 if (mOnGroupCollapseListener != null) { 660 mOnGroupCollapseListener.onGroupCollapse(groupPos); 661 } 662 663 return retValue; 664 } 665 666 /** Used for being notified when a group is collapsed */ 667 public interface OnGroupCollapseListener { 668 /** 669 * Callback method to be invoked when a group in this expandable list has 670 * been collapsed. 671 * 672 * @param groupPosition The group position that was collapsed 673 */ 674 void onGroupCollapse(int groupPosition); 675 } 676 677 private OnGroupCollapseListener mOnGroupCollapseListener; 678 679 public void setOnGroupCollapseListener( 680 OnGroupCollapseListener onGroupCollapseListener) { 681 mOnGroupCollapseListener = onGroupCollapseListener; 682 } 683 684 /** Used for being notified when a group is expanded */ 685 public interface OnGroupExpandListener { 686 /** 687 * Callback method to be invoked when a group in this expandable list has 688 * been expanded. 689 * 690 * @param groupPosition The group position that was expanded 691 */ 692 void onGroupExpand(int groupPosition); 693 } 694 695 private OnGroupExpandListener mOnGroupExpandListener; 696 697 public void setOnGroupExpandListener( 698 OnGroupExpandListener onGroupExpandListener) { 699 mOnGroupExpandListener = onGroupExpandListener; 700 } 701 702 /** 703 * Interface definition for a callback to be invoked when a group in this 704 * expandable list has been clicked. 705 */ 706 public interface OnGroupClickListener { 707 /** 708 * Callback method to be invoked when a group in this expandable list has 709 * been clicked. 710 * 711 * @param parent The ExpandableListConnector where the click happened 712 * @param v The view within the expandable list/ListView that was clicked 713 * @param groupPosition The group position that was clicked 714 * @param id The row id of the group that was clicked 715 * @return True if the click was handled 716 */ 717 boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, 718 long id); 719 } 720 721 private OnGroupClickListener mOnGroupClickListener; 722 723 public void setOnGroupClickListener(OnGroupClickListener onGroupClickListener) { 724 mOnGroupClickListener = onGroupClickListener; 725 } 726 727 /** 728 * Interface definition for a callback to be invoked when a child in this 729 * expandable list has been clicked. 730 */ 731 public interface OnChildClickListener { 732 /** 733 * Callback method to be invoked when a child in this expandable list has 734 * been clicked. 735 * 736 * @param parent The ExpandableListView where the click happened 737 * @param v The view within the expandable list/ListView that was clicked 738 * @param groupPosition The group position that contains the child that 739 * was clicked 740 * @param childPosition The child position within the group 741 * @param id The row id of the child that was clicked 742 * @return True if the click was handled 743 */ 744 boolean onChildClick(ExpandableListView parent, View v, int groupPosition, 745 int childPosition, long id); 746 } 747 748 private OnChildClickListener mOnChildClickListener; 749 750 public void setOnChildClickListener(OnChildClickListener onChildClickListener) { 751 mOnChildClickListener = onChildClickListener; 752 } 753 754 /** 755 * Converts a flat list position (the raw position of an item (child or group) 756 * in the list) to a group and/or child position (represented in a 757 * packed position). This is useful in situations where the caller needs to 758 * use the underlying {@link ListView}'s methods. Use 759 * {@link ExpandableListView#getPackedPositionType} , 760 * {@link ExpandableListView#getPackedPositionChild}, 761 * {@link ExpandableListView#getPackedPositionGroup} to unpack. 762 * 763 * @param flatListPosition The flat list position to be converted. 764 * @return The group and/or child position for the given flat list position 765 * in packed position representation. #PACKED_POSITION_VALUE_NULL if 766 * the position corresponds to a header or a footer item. 767 */ 768 public long getExpandableListPosition(int flatListPosition) { 769 if (isHeaderOrFooterPosition(flatListPosition)) { 770 return PACKED_POSITION_VALUE_NULL; 771 } 772 773 final int adjustedPosition = getFlatPositionForConnector(flatListPosition); 774 PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition); 775 long packedPos = pm.position.getPackedPosition(); 776 pm.recycle(); 777 return packedPos; 778 } 779 780 /** 781 * Converts a group and/or child position to a flat list position. This is 782 * useful in situations where the caller needs to use the underlying 783 * {@link ListView}'s methods. 784 * 785 * @param packedPosition The group and/or child positions to be converted in 786 * packed position representation. Use 787 * {@link #getPackedPositionForChild(int, int)} or 788 * {@link #getPackedPositionForGroup(int)}. 789 * @return The flat list position for the given child or group. 790 */ 791 public int getFlatListPosition(long packedPosition) { 792 ExpandableListPosition elPackedPos = ExpandableListPosition 793 .obtainPosition(packedPosition); 794 PositionMetadata pm = mConnector.getFlattenedPos(elPackedPos); 795 elPackedPos.recycle(); 796 final int flatListPosition = pm.position.flatListPos; 797 pm.recycle(); 798 return getAbsoluteFlatPosition(flatListPosition); 799 } 800 801 /** 802 * Gets the position of the currently selected group or child (along with 803 * its type). Can return {@link #PACKED_POSITION_VALUE_NULL} if no selection. 804 * 805 * @return A packed position containing the currently selected group or 806 * child's position and type. #PACKED_POSITION_VALUE_NULL if no selection 807 * or if selection is on a header or a footer item. 808 */ 809 public long getSelectedPosition() { 810 final int selectedPos = getSelectedItemPosition(); 811 812 // The case where there is no selection (selectedPos == -1) is also handled here. 813 return getExpandableListPosition(selectedPos); 814 } 815 816 /** 817 * Gets the ID of the currently selected group or child. Can return -1 if no 818 * selection. 819 * 820 * @return The ID of the currently selected group or child. -1 if no 821 * selection. 822 */ 823 public long getSelectedId() { 824 long packedPos = getSelectedPosition(); 825 if (packedPos == PACKED_POSITION_VALUE_NULL) return -1; 826 827 int groupPos = getPackedPositionGroup(packedPos); 828 829 if (getPackedPositionType(packedPos) == PACKED_POSITION_TYPE_GROUP) { 830 // It's a group 831 return mAdapter.getGroupId(groupPos); 832 } else { 833 // It's a child 834 return mAdapter.getChildId(groupPos, getPackedPositionChild(packedPos)); 835 } 836 } 837 838 /** 839 * Sets the selection to the specified group. 840 * @param groupPosition The position of the group that should be selected. 841 */ 842 public void setSelectedGroup(int groupPosition) { 843 ExpandableListPosition elGroupPos = ExpandableListPosition 844 .obtainGroupPosition(groupPosition); 845 PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos); 846 elGroupPos.recycle(); 847 final int absoluteFlatPosition = getAbsoluteFlatPosition(pm.position.flatListPos); 848 super.setSelection(absoluteFlatPosition); 849 pm.recycle(); 850 } 851 852 /** 853 * Sets the selection to the specified child. If the child is in a collapsed 854 * group, the group will only be expanded and child subsequently selected if 855 * shouldExpandGroup is set to true, otherwise the method will return false. 856 * 857 * @param groupPosition The position of the group that contains the child. 858 * @param childPosition The position of the child within the group. 859 * @param shouldExpandGroup Whether the child's group should be expanded if 860 * it is collapsed. 861 * @return Whether the selection was successfully set on the child. 862 */ 863 public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) { 864 ExpandableListPosition elChildPos = ExpandableListPosition.obtainChildPosition( 865 groupPosition, childPosition); 866 PositionMetadata flatChildPos = mConnector.getFlattenedPos(elChildPos); 867 868 if (flatChildPos == null) { 869 // The child's group isn't expanded 870 871 // Shouldn't expand the group, so return false for we didn't set the selection 872 if (!shouldExpandGroup) return false; 873 874 expandGroup(groupPosition); 875 876 flatChildPos = mConnector.getFlattenedPos(elChildPos); 877 878 // Sanity check 879 if (flatChildPos == null) { 880 throw new IllegalStateException("Could not find child"); 881 } 882 } 883 884 int absoluteFlatPosition = getAbsoluteFlatPosition(flatChildPos.position.flatListPos); 885 super.setSelection(absoluteFlatPosition); 886 887 elChildPos.recycle(); 888 flatChildPos.recycle(); 889 890 return true; 891 } 892 893 /** 894 * Whether the given group is currently expanded. 895 * 896 * @param groupPosition The group to check. 897 * @return Whether the group is currently expanded. 898 */ 899 public boolean isGroupExpanded(int groupPosition) { 900 return mConnector.isGroupExpanded(groupPosition); 901 } 902 903 /** 904 * Gets the type of a packed position. See 905 * {@link #getPackedPositionForChild(int, int)}. 906 * 907 * @param packedPosition The packed position for which to return the type. 908 * @return The type of the position contained within the packed position, 909 * either {@link #PACKED_POSITION_TYPE_CHILD}, {@link #PACKED_POSITION_TYPE_GROUP}, or 910 * {@link #PACKED_POSITION_TYPE_NULL}. 911 */ 912 public static int getPackedPositionType(long packedPosition) { 913 if (packedPosition == PACKED_POSITION_VALUE_NULL) { 914 return PACKED_POSITION_TYPE_NULL; 915 } 916 917 return (packedPosition & PACKED_POSITION_MASK_TYPE) == PACKED_POSITION_MASK_TYPE 918 ? PACKED_POSITION_TYPE_CHILD 919 : PACKED_POSITION_TYPE_GROUP; 920 } 921 922 /** 923 * Gets the group position from a packed position. See 924 * {@link #getPackedPositionForChild(int, int)}. 925 * 926 * @param packedPosition The packed position from which the group position 927 * will be returned. 928 * @return The group position portion of the packed position. If this does 929 * not contain a group, returns -1. 930 */ 931 public static int getPackedPositionGroup(long packedPosition) { 932 // Null 933 if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1; 934 935 return (int) ((packedPosition & PACKED_POSITION_MASK_GROUP) >> PACKED_POSITION_SHIFT_GROUP); 936 } 937 938 /** 939 * Gets the child position from a packed position that is of 940 * {@link #PACKED_POSITION_TYPE_CHILD} type (use {@link #getPackedPositionType(long)}). 941 * To get the group that this child belongs to, use 942 * {@link #getPackedPositionGroup(long)}. See 943 * {@link #getPackedPositionForChild(int, int)}. 944 * 945 * @param packedPosition The packed position from which the child position 946 * will be returned. 947 * @return The child position portion of the packed position. If this does 948 * not contain a child, returns -1. 949 */ 950 public static int getPackedPositionChild(long packedPosition) { 951 // Null 952 if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1; 953 954 // Group since a group type clears this bit 955 if ((packedPosition & PACKED_POSITION_MASK_TYPE) != PACKED_POSITION_MASK_TYPE) return -1; 956 957 return (int) (packedPosition & PACKED_POSITION_MASK_CHILD); 958 } 959 960 /** 961 * Returns the packed position representation of a child's position. 962 * <p> 963 * In general, a packed position should be used in 964 * situations where the position given to/returned from an 965 * {@link ExpandableListAdapter} or {@link ExpandableListView} method can 966 * either be a child or group. The two positions are packed into a single 967 * long which can be unpacked using 968 * {@link #getPackedPositionChild(long)}, 969 * {@link #getPackedPositionGroup(long)}, and 970 * {@link #getPackedPositionType(long)}. 971 * 972 * @param groupPosition The child's parent group's position. 973 * @param childPosition The child position within the group. 974 * @return The packed position representation of the child (and parent group). 975 */ 976 public static long getPackedPositionForChild(int groupPosition, int childPosition) { 977 return (((long)PACKED_POSITION_TYPE_CHILD) << PACKED_POSITION_SHIFT_TYPE) 978 | ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP) 979 << PACKED_POSITION_SHIFT_GROUP) 980 | (childPosition & PACKED_POSITION_INT_MASK_CHILD); 981 } 982 983 /** 984 * Returns the packed position representation of a group's position. See 985 * {@link #getPackedPositionForChild(int, int)}. 986 * 987 * @param groupPosition The child's parent group's position. 988 * @return The packed position representation of the group. 989 */ 990 public static long getPackedPositionForGroup(int groupPosition) { 991 // No need to OR a type in because PACKED_POSITION_GROUP == 0 992 return ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP) 993 << PACKED_POSITION_SHIFT_GROUP); 994 } 995 996 @Override 997 ContextMenuInfo createContextMenuInfo(View view, int flatListPosition, long id) { 998 if (isHeaderOrFooterPosition(flatListPosition)) { 999 // Return normal info for header/footer view context menus 1000 return new AdapterContextMenuInfo(view, flatListPosition, id); 1001 } 1002 1003 final int adjustedPosition = getFlatPositionForConnector(flatListPosition); 1004 PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition); 1005 ExpandableListPosition pos = pm.position; 1006 1007 id = getChildOrGroupId(pos); 1008 long packedPosition = pos.getPackedPosition(); 1009 1010 pm.recycle(); 1011 1012 return new ExpandableListContextMenuInfo(view, packedPosition, id); 1013 } 1014 1015 /** 1016 * Gets the ID of the group or child at the given <code>position</code>. 1017 * This is useful since there is no ListAdapter ID -> ExpandableListAdapter 1018 * ID conversion mechanism (in some cases, it isn't possible). 1019 * 1020 * @param position The position of the child or group whose ID should be 1021 * returned. 1022 */ 1023 private long getChildOrGroupId(ExpandableListPosition position) { 1024 if (position.type == ExpandableListPosition.CHILD) { 1025 return mAdapter.getChildId(position.groupPos, position.childPos); 1026 } else { 1027 return mAdapter.getGroupId(position.groupPos); 1028 } 1029 } 1030 1031 /** 1032 * Sets the indicator to be drawn next to a child. 1033 * 1034 * @param childIndicator The drawable to be used as an indicator. If the 1035 * child is the last child for a group, the state 1036 * {@link android.R.attr#state_last} will be set. 1037 */ 1038 public void setChildIndicator(Drawable childIndicator) { 1039 mChildIndicator = childIndicator; 1040 } 1041 1042 /** 1043 * Sets the drawing bounds for the child indicator. For either, you can 1044 * specify {@link #CHILD_INDICATOR_INHERIT} to use inherit from the general 1045 * indicator's bounds. 1046 * 1047 * @see #setIndicatorBounds(int, int) 1048 * @param left The left position (relative to the left bounds of this View) 1049 * to start drawing the indicator. 1050 * @param right The right position (relative to the left bounds of this 1051 * View) to end the drawing of the indicator. 1052 */ 1053 public void setChildIndicatorBounds(int left, int right) { 1054 mChildIndicatorLeft = left; 1055 mChildIndicatorRight = right; 1056 } 1057 1058 /** 1059 * Sets the indicator to be drawn next to a group. 1060 * 1061 * @param groupIndicator The drawable to be used as an indicator. If the 1062 * group is empty, the state {@link android.R.attr#state_empty} will be 1063 * set. If the group is expanded, the state 1064 * {@link android.R.attr#state_expanded} will be set. 1065 */ 1066 public void setGroupIndicator(Drawable groupIndicator) { 1067 mGroupIndicator = groupIndicator; 1068 if (mIndicatorRight == 0 && mGroupIndicator != null) { 1069 mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth(); 1070 } 1071 } 1072 1073 /** 1074 * Sets the drawing bounds for the indicators (at minimum, the group indicator 1075 * is affected by this; the child indicator is affected by this if the 1076 * child indicator bounds are set to inherit). 1077 * 1078 * @see #setChildIndicatorBounds(int, int) 1079 * @param left The left position (relative to the left bounds of this View) 1080 * to start drawing the indicator. 1081 * @param right The right position (relative to the left bounds of this 1082 * View) to end the drawing of the indicator. 1083 */ 1084 public void setIndicatorBounds(int left, int right) { 1085 mIndicatorLeft = left; 1086 mIndicatorRight = right; 1087 } 1088 1089 /** 1090 * Extra menu information specific to an {@link ExpandableListView} provided 1091 * to the 1092 * {@link android.view.View.OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo) } 1093 * callback when a context menu is brought up for this AdapterView. 1094 */ 1095 public static class ExpandableListContextMenuInfo implements ContextMenu.ContextMenuInfo { 1096 1097 public ExpandableListContextMenuInfo(View targetView, long packedPosition, long id) { 1098 this.targetView = targetView; 1099 this.packedPosition = packedPosition; 1100 this.id = id; 1101 } 1102 1103 /** 1104 * The view for which the context menu is being displayed. This 1105 * will be one of the children Views of this {@link ExpandableListView}. 1106 */ 1107 public View targetView; 1108 1109 /** 1110 * The packed position in the list represented by the adapter for which 1111 * the context menu is being displayed. Use the methods 1112 * {@link ExpandableListView#getPackedPositionType}, 1113 * {@link ExpandableListView#getPackedPositionChild}, and 1114 * {@link ExpandableListView#getPackedPositionGroup} to unpack this. 1115 */ 1116 public long packedPosition; 1117 1118 /** 1119 * The ID of the item (group or child) for which the context menu is 1120 * being displayed. 1121 */ 1122 public long id; 1123 } 1124 1125 static class SavedState extends BaseSavedState { 1126 ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList; 1127 1128 /** 1129 * Constructor called from {@link ExpandableListView#onSaveInstanceState()} 1130 */ 1131 SavedState( 1132 Parcelable superState, 1133 ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList) { 1134 super(superState); 1135 this.expandedGroupMetadataList = expandedGroupMetadataList; 1136 } 1137 1138 /** 1139 * Constructor called from {@link #CREATOR} 1140 */ 1141 private SavedState(Parcel in) { 1142 super(in); 1143 expandedGroupMetadataList = new ArrayList<ExpandableListConnector.GroupMetadata>(); 1144 in.readList(expandedGroupMetadataList, ExpandableListConnector.class.getClassLoader()); 1145 } 1146 1147 @Override 1148 public void writeToParcel(Parcel out, int flags) { 1149 super.writeToParcel(out, flags); 1150 out.writeList(expandedGroupMetadataList); 1151 } 1152 1153 public static final Parcelable.Creator<SavedState> CREATOR 1154 = new Parcelable.Creator<SavedState>() { 1155 public SavedState createFromParcel(Parcel in) { 1156 return new SavedState(in); 1157 } 1158 1159 public SavedState[] newArray(int size) { 1160 return new SavedState[size]; 1161 } 1162 }; 1163 } 1164 1165 @Override 1166 public Parcelable onSaveInstanceState() { 1167 Parcelable superState = super.onSaveInstanceState(); 1168 return new SavedState(superState, 1169 mConnector != null ? mConnector.getExpandedGroupMetadataList() : null); 1170 } 1171 1172 @Override 1173 public void onRestoreInstanceState(Parcelable state) { 1174 if (!(state instanceof SavedState)) { 1175 super.onRestoreInstanceState(state); 1176 return; 1177 } 1178 1179 SavedState ss = (SavedState) state; 1180 super.onRestoreInstanceState(ss.getSuperState()); 1181 1182 if (mConnector != null && ss.expandedGroupMetadataList != null) { 1183 mConnector.setExpandedGroupMetadataList(ss.expandedGroupMetadataList); 1184 } 1185 } 1186 1187 @Override 1188 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 1189 super.onInitializeAccessibilityEvent(event); 1190 event.setClassName(ExpandableListView.class.getName()); 1191 } 1192 1193 @Override 1194 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 1195 super.onInitializeAccessibilityNodeInfo(info); 1196 info.setClassName(ExpandableListView.class.getName()); 1197 } 1198} 1199