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