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