blob: 8279ee5e9ccd12ad66a7be2ce3130961f73e23a5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.Rect;
Adam Powell45803472010-01-25 15:10:44 -080025import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import 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.widget.ExpandableListConnector.PositionMetadata;
34
Adam Powell45803472010-01-25 15:10:44 -080035import java.util.ArrayList;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
38 * A view that shows items in a vertically scrolling two-level list. This
39 * differs from the {@link ListView} by allowing two levels: groups which can
40 * individually be expanded to show its children. The items come from the
41 * {@link ExpandableListAdapter} associated with this view.
42 * <p>
43 * Expandable lists are able to show an indicator beside each item to display
44 * the item's current state (the states are usually one of expanded group,
45 * collapsed group, child, or last child). Use
46 * {@link #setChildIndicator(Drawable)} or {@link #setGroupIndicator(Drawable)}
47 * (or the corresponding XML attributes) to set these indicators (see the docs
48 * for each method to see additional state that each Drawable can have). The
49 * default style for an {@link ExpandableListView} provides indicators which
50 * will be shown next to Views given to the {@link ExpandableListView}. The
51 * layouts android.R.layout.simple_expandable_list_item_1 and
52 * android.R.layout.simple_expandable_list_item_2 (which should be used with
53 * {@link SimpleCursorTreeAdapter}) contain the preferred position information
54 * for indicators.
55 * <p>
56 * The context menu information set by an {@link ExpandableListView} will be a
57 * {@link ExpandableListContextMenuInfo} object with
58 * {@link ExpandableListContextMenuInfo#packedPosition} being a packed position
59 * that can be used with {@link #getPackedPositionType(long)} and the other
60 * similar methods.
61 * <p>
62 * <em><b>Note:</b></em> You cannot use the value <code>wrap_content</code>
63 * for the <code>android:layout_height</code> attribute of a
64 * ExpandableListView in XML if the parent's size is also not strictly specified
65 * (for example, if the parent were ScrollView you could not specify
66 * wrap_content since it also can be any length. However, you can use
67 * wrap_content if the ExpandableListView parent has a specific size, such as
68 * 100 pixels.
69 *
70 * @attr ref android.R.styleable#ExpandableListView_groupIndicator
71 * @attr ref android.R.styleable#ExpandableListView_indicatorLeft
72 * @attr ref android.R.styleable#ExpandableListView_indicatorRight
73 * @attr ref android.R.styleable#ExpandableListView_childIndicator
74 * @attr ref android.R.styleable#ExpandableListView_childIndicatorLeft
75 * @attr ref android.R.styleable#ExpandableListView_childIndicatorRight
76 * @attr ref android.R.styleable#ExpandableListView_childDivider
77 */
78public class ExpandableListView extends ListView {
79
80 /**
81 * The packed position represents a group.
82 */
83 public static final int PACKED_POSITION_TYPE_GROUP = 0;
84
85 /**
86 * The packed position represents a child.
87 */
88 public static final int PACKED_POSITION_TYPE_CHILD = 1;
89
90 /**
91 * The packed position represents a neither/null/no preference.
92 */
93 public static final int PACKED_POSITION_TYPE_NULL = 2;
94
95 /**
96 * The value for a packed position that represents neither/null/no
97 * preference. This value is not otherwise possible since a group type
98 * (first bit 0) should not have a child position filled.
99 */
100 public static final long PACKED_POSITION_VALUE_NULL = 0x00000000FFFFFFFFL;
101
102 /** The mask (in packed position representation) for the child */
103 private static final long PACKED_POSITION_MASK_CHILD = 0x00000000FFFFFFFFL;
104
105 /** The mask (in packed position representation) for the group */
106 private static final long PACKED_POSITION_MASK_GROUP = 0x7FFFFFFF00000000L;
107
108 /** The mask (in packed position representation) for the type */
109 private static final long PACKED_POSITION_MASK_TYPE = 0x8000000000000000L;
110
111 /** The shift amount (in packed position representation) for the group */
112 private static final long PACKED_POSITION_SHIFT_GROUP = 32;
113
114 /** The shift amount (in packed position representation) for the type */
115 private static final long PACKED_POSITION_SHIFT_TYPE = 63;
116
117 /** The mask (in integer child position representation) for the child */
118 private static final long PACKED_POSITION_INT_MASK_CHILD = 0xFFFFFFFF;
119
120 /** The mask (in integer group position representation) for the group */
121 private static final long PACKED_POSITION_INT_MASK_GROUP = 0x7FFFFFFF;
122
123 /** Serves as the glue/translator between a ListView and an ExpandableListView */
124 private ExpandableListConnector mConnector;
125
126 /** Gives us Views through group+child positions */
127 private ExpandableListAdapter mAdapter;
128
129 /** Left bound for drawing the indicator. */
130 private int mIndicatorLeft;
131
132 /** Right bound for drawing the indicator. */
133 private int mIndicatorRight;
134
135 /**
136 * Left bound for drawing the indicator of a child. Value of
137 * {@link #CHILD_INDICATOR_INHERIT} means use mIndicatorLeft.
138 */
139 private int mChildIndicatorLeft;
140
141 /**
142 * Right bound for drawing the indicator of a child. Value of
143 * {@link #CHILD_INDICATOR_INHERIT} means use mIndicatorRight.
144 */
145 private int mChildIndicatorRight;
146
147 /**
148 * Denotes when a child indicator should inherit this bound from the generic
149 * indicator bounds
150 */
151 public static final int CHILD_INDICATOR_INHERIT = -1;
152
153 /** The indicator drawn next to a group. */
154 private Drawable mGroupIndicator;
155
156 /** The indicator drawn next to a child. */
157 private Drawable mChildIndicator;
158
159 private static final int[] EMPTY_STATE_SET = {};
160
161 /** State indicating the group is expanded. */
162 private static final int[] GROUP_EXPANDED_STATE_SET =
163 {R.attr.state_expanded};
164
165 /** State indicating the group is empty (has no children). */
166 private static final int[] GROUP_EMPTY_STATE_SET =
167 {R.attr.state_empty};
168
169 /** State indicating the group is expanded and empty (has no children). */
170 private static final int[] GROUP_EXPANDED_EMPTY_STATE_SET =
171 {R.attr.state_expanded, R.attr.state_empty};
172
173 /** States for the group where the 0th bit is expanded and 1st bit is empty. */
174 private static final int[][] GROUP_STATE_SETS = {
175 EMPTY_STATE_SET, // 00
176 GROUP_EXPANDED_STATE_SET, // 01
177 GROUP_EMPTY_STATE_SET, // 10
178 GROUP_EXPANDED_EMPTY_STATE_SET // 11
179 };
180
181 /** State indicating the child is the last within its group. */
182 private static final int[] CHILD_LAST_STATE_SET =
183 {R.attr.state_last};
184
185 /** Drawable to be used as a divider when it is adjacent to any children */
186 private Drawable mChildDivider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
188 // Bounds of the indicator to be drawn
189 private final Rect mIndicatorRect = new Rect();
190
191 public ExpandableListView(Context context) {
192 this(context, null);
193 }
194
195 public ExpandableListView(Context context, AttributeSet attrs) {
196 this(context, attrs, com.android.internal.R.attr.expandableListViewStyle);
197 }
198
199 public ExpandableListView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
201
202 TypedArray a =
203 context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ExpandableListView, defStyle,
204 0);
205
206 mGroupIndicator = a
207 .getDrawable(com.android.internal.R.styleable.ExpandableListView_groupIndicator);
208 mChildIndicator = a
209 .getDrawable(com.android.internal.R.styleable.ExpandableListView_childIndicator);
210 mIndicatorLeft = a
211 .getDimensionPixelSize(com.android.internal.R.styleable.ExpandableListView_indicatorLeft, 0);
212 mIndicatorRight = a
213 .getDimensionPixelSize(com.android.internal.R.styleable.ExpandableListView_indicatorRight, 0);
Romain Guy875862e2011-01-17 11:37:24 -0800214 if (mIndicatorRight == 0 && mGroupIndicator != null) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800215 mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth();
216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 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
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 */
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 */
381 public void setChildDivider(Drawable childDivider) {
382 mChildDivider = childDivider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
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) {
Gilles Debunne272f3a92010-03-03 15:55:13 -0800392 final int adjustedPosition = getFlatPositionForConnector(flatListPosition);
393 PositionMetadata pos = mConnector.getUnflattenedPos(adjustedPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 // 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;
Romain Guy95930e12010-10-04 13:46:02 -0700399 divider.setBounds(bounds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 divider.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 pos.recycle();
402 return;
403 }
404 pos.recycle();
405 }
406
407 // Otherwise draw the default divider
408 super.drawDivider(canvas, bounds, flatListPosition);
409 }
410
411 /**
412 * This overloaded method should not be used, instead use
413 * {@link #setAdapter(ExpandableListAdapter)}.
414 * <p>
415 * {@inheritDoc}
416 */
417 @Override
418 public void setAdapter(ListAdapter adapter) {
419 throw new RuntimeException(
420 "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " +
421 "setAdapter(ListAdapter)");
422 }
423
424 /**
425 * This method should not be used, use {@link #getExpandableListAdapter()}.
426 */
427 @Override
428 public ListAdapter getAdapter() {
429 /*
430 * The developer should never really call this method on an
431 * ExpandableListView, so it would be nice to throw a RuntimeException,
432 * but AdapterView calls this
433 */
434 return super.getAdapter();
435 }
436
437 /**
438 * Register a callback to be invoked when an item has been clicked and the
439 * caller prefers to receive a ListView-style position instead of a group
440 * and/or child position. In most cases, the caller should use
441 * {@link #setOnGroupClickListener} and/or {@link #setOnChildClickListener}.
442 * <p />
443 * {@inheritDoc}
444 */
445 @Override
446 public void setOnItemClickListener(OnItemClickListener l) {
447 super.setOnItemClickListener(l);
448 }
449
450 /**
451 * Sets the adapter that provides data to this view.
452 * @param adapter The adapter that provides data to this view.
453 */
454 public void setAdapter(ExpandableListAdapter adapter) {
455 // Set member variable
456 mAdapter = adapter;
457
458 if (adapter != null) {
459 // Create the connector
460 mConnector = new ExpandableListConnector(adapter);
461 } else {
462 mConnector = null;
463 }
464
465 // Link the ListView (superclass) to the expandable list data through the connector
466 super.setAdapter(mConnector);
467 }
468
469 /**
470 * Gets the adapter that provides data to this view.
471 * @return The adapter that provides data to this view.
472 */
473 public ExpandableListAdapter getExpandableListAdapter() {
474 return mAdapter;
475 }
476
Gilles Debunne272f3a92010-03-03 15:55:13 -0800477 /**
478 * @param position An absolute (including header and footer) flat list position.
479 * @return true if the position corresponds to a header or a footer item.
480 */
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800481 private boolean isHeaderOrFooterPosition(int position) {
482 final int footerViewsStart = mItemCount - getFooterViewsCount();
483 return (position < getHeaderViewsCount() || position >= footerViewsStart);
484 }
485
Gilles Debunne272f3a92010-03-03 15:55:13 -0800486 /**
487 * Converts an absolute item flat position into a group/child flat position, shifting according
488 * to the number of header items.
489 *
490 * @param flatListPosition The absolute flat position
491 * @return A group/child flat position as expected by the connector.
492 */
493 private int getFlatPositionForConnector(int flatListPosition) {
494 return flatListPosition - getHeaderViewsCount();
495 }
496
497 /**
498 * Converts a group/child flat position into an absolute flat position, that takes into account
499 * the possible headers.
500 *
501 * @param flatListPosition The child/group flat position
502 * @return An absolute flat position.
503 */
504 private int getAbsoluteFlatPosition(int flatListPosition) {
505 return flatListPosition + getHeaderViewsCount();
506 }
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 @Override
509 public boolean performItemClick(View v, int position, long id) {
510 // Ignore clicks in header/footers
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800511 if (isHeaderOrFooterPosition(position)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 // Clicked on a header/footer, so ignore pass it on to super
513 return super.performItemClick(v, position, id);
514 }
515
516 // Internally handle the item click
Gilles Debunne272f3a92010-03-03 15:55:13 -0800517 final int adjustedPosition = getFlatPositionForConnector(position);
518 return handleItemClick(v, adjustedPosition, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520
521 /**
522 * This will either expand/collapse groups (if a group was clicked) or pass
523 * on the click to the proper child (if a child was clicked)
524 *
525 * @param position The flat list position. This has already been factored to
526 * remove the header/footer.
527 * @param id The ListAdapter ID, not the group or child ID.
528 */
529 boolean handleItemClick(View v, int position, long id) {
530 final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position);
531
532 id = getChildOrGroupId(posMetadata.position);
533
534 boolean returnValue;
535 if (posMetadata.position.type == ExpandableListPosition.GROUP) {
536 /* It's a group, so handle collapsing/expanding */
Romain Guy40395452009-12-04 16:51:52 -0800537
538 /* It's a group click, so pass on event */
539 if (mOnGroupClickListener != null) {
540 if (mOnGroupClickListener.onGroupClick(this, v,
541 posMetadata.position.groupPos, id)) {
542 posMetadata.recycle();
543 return true;
544 }
545 }
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 if (posMetadata.isExpanded()) {
548 /* Collapse it */
549 mConnector.collapseGroup(posMetadata);
550
551 playSoundEffect(SoundEffectConstants.CLICK);
Romain Guy40395452009-12-04 16:51:52 -0800552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 if (mOnGroupCollapseListener != null) {
554 mOnGroupCollapseListener.onGroupCollapse(posMetadata.position.groupPos);
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 /* Expand it */
558 mConnector.expandGroup(posMetadata);
559
560 playSoundEffect(SoundEffectConstants.CLICK);
Romain Guy40395452009-12-04 16:51:52 -0800561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 if (mOnGroupExpandListener != null) {
563 mOnGroupExpandListener.onGroupExpand(posMetadata.position.groupPos);
564 }
Adam Powell45803472010-01-25 15:10:44 -0800565
566 final int groupPos = posMetadata.position.groupPos;
567 final int groupFlatPos = posMetadata.position.flatListPos;
Gilles Debunne52b65d32010-03-01 10:54:51 -0800568
569 final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount();
570 smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos),
571 shiftedGroupPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Romain Guy40395452009-12-04 16:51:52 -0800573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 returnValue = true;
575 } else {
576 /* It's a child, so pass on event */
577 if (mOnChildClickListener != null) {
578 playSoundEffect(SoundEffectConstants.CLICK);
579 return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos,
580 posMetadata.position.childPos, id);
581 }
Romain Guy40395452009-12-04 16:51:52 -0800582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 returnValue = false;
584 }
Romain Guy40395452009-12-04 16:51:52 -0800585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 posMetadata.recycle();
Romain Guy40395452009-12-04 16:51:52 -0800587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 return returnValue;
589 }
590
591 /**
592 * Expand a group in the grouped list view
593 *
594 * @param groupPos the group to be expanded
595 * @return True if the group was expanded, false otherwise (if the group
596 * was already expanded, this will return false)
597 */
598 public boolean expandGroup(int groupPos) {
599 boolean retValue = mConnector.expandGroup(groupPos);
600
601 if (mOnGroupExpandListener != null) {
602 mOnGroupExpandListener.onGroupExpand(groupPos);
603 }
604
605 return retValue;
606 }
607
608 /**
609 * Collapse a group in the grouped list view
610 *
611 * @param groupPos position of the group to collapse
612 * @return True if the group was collapsed, false otherwise (if the group
613 * was already collapsed, this will return false)
614 */
615 public boolean collapseGroup(int groupPos) {
616 boolean retValue = mConnector.collapseGroup(groupPos);
617
618 if (mOnGroupCollapseListener != null) {
619 mOnGroupCollapseListener.onGroupCollapse(groupPos);
620 }
621
622 return retValue;
623 }
624
625 /** Used for being notified when a group is collapsed */
626 public interface OnGroupCollapseListener {
627 /**
628 * Callback method to be invoked when a group in this expandable list has
629 * been collapsed.
630 *
631 * @param groupPosition The group position that was collapsed
632 */
633 void onGroupCollapse(int groupPosition);
634 }
635
636 private OnGroupCollapseListener mOnGroupCollapseListener;
637
638 public void setOnGroupCollapseListener(
639 OnGroupCollapseListener onGroupCollapseListener) {
640 mOnGroupCollapseListener = onGroupCollapseListener;
641 }
642
643 /** Used for being notified when a group is expanded */
644 public interface OnGroupExpandListener {
645 /**
646 * Callback method to be invoked when a group in this expandable list has
647 * been expanded.
648 *
649 * @param groupPosition The group position that was expanded
650 */
651 void onGroupExpand(int groupPosition);
652 }
653
654 private OnGroupExpandListener mOnGroupExpandListener;
655
656 public void setOnGroupExpandListener(
657 OnGroupExpandListener onGroupExpandListener) {
658 mOnGroupExpandListener = onGroupExpandListener;
659 }
660
661 /**
662 * Interface definition for a callback to be invoked when a group in this
663 * expandable list has been clicked.
664 */
665 public interface OnGroupClickListener {
666 /**
667 * Callback method to be invoked when a group in this expandable list has
668 * been clicked.
669 *
670 * @param parent The ExpandableListConnector where the click happened
671 * @param v The view within the expandable list/ListView that was clicked
672 * @param groupPosition The group position that was clicked
673 * @param id The row id of the group that was clicked
674 * @return True if the click was handled
675 */
676 boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
677 long id);
678 }
679
680 private OnGroupClickListener mOnGroupClickListener;
681
682 public void setOnGroupClickListener(OnGroupClickListener onGroupClickListener) {
683 mOnGroupClickListener = onGroupClickListener;
684 }
685
686 /**
687 * Interface definition for a callback to be invoked when a child in this
688 * expandable list has been clicked.
689 */
690 public interface OnChildClickListener {
691 /**
692 * Callback method to be invoked when a child in this expandable list has
693 * been clicked.
694 *
695 * @param parent The ExpandableListView where the click happened
696 * @param v The view within the expandable list/ListView that was clicked
697 * @param groupPosition The group position that contains the child that
698 * was clicked
699 * @param childPosition The child position within the group
700 * @param id The row id of the child that was clicked
701 * @return True if the click was handled
702 */
703 boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
704 int childPosition, long id);
705 }
706
707 private OnChildClickListener mOnChildClickListener;
708
709 public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
710 mOnChildClickListener = onChildClickListener;
711 }
712
713 /**
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800714 * Converts a flat list position (the raw position of an item (child or group)
715 * in the list) to an group and/or child position (represented in a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 * packed position). This is useful in situations where the caller needs to
717 * use the underlying {@link ListView}'s methods. Use
718 * {@link ExpandableListView#getPackedPositionType} ,
719 * {@link ExpandableListView#getPackedPositionChild},
720 * {@link ExpandableListView#getPackedPositionGroup} to unpack.
721 *
722 * @param flatListPosition The flat list position to be converted.
723 * @return The group and/or child position for the given flat list position
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800724 * in packed position representation. #PACKED_POSITION_VALUE_NULL if
725 * the position corresponds to a header or a footer item.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 */
727 public long getExpandableListPosition(int flatListPosition) {
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800728 if (isHeaderOrFooterPosition(flatListPosition)) {
729 return PACKED_POSITION_VALUE_NULL;
730 }
731
Gilles Debunne272f3a92010-03-03 15:55:13 -0800732 final int adjustedPosition = getFlatPositionForConnector(flatListPosition);
733 PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 long packedPos = pm.position.getPackedPosition();
735 pm.recycle();
736 return packedPos;
737 }
738
739 /**
740 * Converts a group and/or child position to a flat list position. This is
741 * useful in situations where the caller needs to use the underlying
742 * {@link ListView}'s methods.
743 *
744 * @param packedPosition The group and/or child positions to be converted in
745 * packed position representation. Use
746 * {@link #getPackedPositionForChild(int, int)} or
747 * {@link #getPackedPositionForGroup(int)}.
748 * @return The flat list position for the given child or group.
749 */
750 public int getFlatListPosition(long packedPosition) {
751 PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition
752 .obtainPosition(packedPosition));
Gilles Debunne272f3a92010-03-03 15:55:13 -0800753 final int flatListPosition = pm.position.flatListPos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 pm.recycle();
Gilles Debunne272f3a92010-03-03 15:55:13 -0800755 return getAbsoluteFlatPosition(flatListPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 /**
759 * Gets the position of the currently selected group or child (along with
760 * its type). Can return {@link #PACKED_POSITION_VALUE_NULL} if no selection.
761 *
762 * @return A packed position containing the currently selected group or
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800763 * child's position and type. #PACKED_POSITION_VALUE_NULL if no selection
764 * or if selection is on a header or a footer item.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 */
766 public long getSelectedPosition() {
767 final int selectedPos = getSelectedItemPosition();
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800768
769 // The case where there is no selection (selectedPos == -1) is also handled here.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 return getExpandableListPosition(selectedPos);
771 }
772
773 /**
774 * Gets the ID of the currently selected group or child. Can return -1 if no
775 * selection.
776 *
777 * @return The ID of the currently selected group or child. -1 if no
778 * selection.
779 */
780 public long getSelectedId() {
781 long packedPos = getSelectedPosition();
782 if (packedPos == PACKED_POSITION_VALUE_NULL) return -1;
783
784 int groupPos = getPackedPositionGroup(packedPos);
785
786 if (getPackedPositionType(packedPos) == PACKED_POSITION_TYPE_GROUP) {
787 // It's a group
788 return mAdapter.getGroupId(groupPos);
789 } else {
790 // It's a child
791 return mAdapter.getChildId(groupPos, getPackedPositionChild(packedPos));
792 }
793 }
794
795 /**
796 * Sets the selection to the specified group.
797 * @param groupPosition The position of the group that should be selected.
798 */
799 public void setSelectedGroup(int groupPosition) {
800 ExpandableListPosition elGroupPos = ExpandableListPosition
801 .obtainGroupPosition(groupPosition);
802 PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos);
803 elGroupPos.recycle();
Gilles Debunne272f3a92010-03-03 15:55:13 -0800804 final int absoluteFlatPosition = getAbsoluteFlatPosition(pm.position.flatListPos);
805 super.setSelection(absoluteFlatPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 pm.recycle();
807 }
808
809 /**
810 * Sets the selection to the specified child. If the child is in a collapsed
811 * group, the group will only be expanded and child subsequently selected if
812 * shouldExpandGroup is set to true, otherwise the method will return false.
813 *
814 * @param groupPosition The position of the group that contains the child.
815 * @param childPosition The position of the child within the group.
816 * @param shouldExpandGroup Whether the child's group should be expanded if
817 * it is collapsed.
818 * @return Whether the selection was successfully set on the child.
819 */
820 public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) {
821 ExpandableListPosition elChildPos = ExpandableListPosition.obtainChildPosition(
822 groupPosition, childPosition);
823 PositionMetadata flatChildPos = mConnector.getFlattenedPos(elChildPos);
824
825 if (flatChildPos == null) {
826 // The child's group isn't expanded
827
828 // Shouldn't expand the group, so return false for we didn't set the selection
829 if (!shouldExpandGroup) return false;
830
831 expandGroup(groupPosition);
832
833 flatChildPos = mConnector.getFlattenedPos(elChildPos);
834
835 // Sanity check
836 if (flatChildPos == null) {
837 throw new IllegalStateException("Could not find child");
838 }
839 }
840
Gilles Debunne272f3a92010-03-03 15:55:13 -0800841 int absoluteFlatPosition = getAbsoluteFlatPosition(flatChildPos.position.flatListPos);
842 super.setSelection(absoluteFlatPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843
844 elChildPos.recycle();
845 flatChildPos.recycle();
846
847 return true;
848 }
849
850 /**
851 * Whether the given group is currently expanded.
852 *
853 * @param groupPosition The group to check.
854 * @return Whether the group is currently expanded.
855 */
856 public boolean isGroupExpanded(int groupPosition) {
857 return mConnector.isGroupExpanded(groupPosition);
858 }
859
860 /**
861 * Gets the type of a packed position. See
862 * {@link #getPackedPositionForChild(int, int)}.
863 *
864 * @param packedPosition The packed position for which to return the type.
865 * @return The type of the position contained within the packed position,
866 * either {@link #PACKED_POSITION_TYPE_CHILD}, {@link #PACKED_POSITION_TYPE_GROUP}, or
867 * {@link #PACKED_POSITION_TYPE_NULL}.
868 */
869 public static int getPackedPositionType(long packedPosition) {
870 if (packedPosition == PACKED_POSITION_VALUE_NULL) {
871 return PACKED_POSITION_TYPE_NULL;
872 }
873
874 return (packedPosition & PACKED_POSITION_MASK_TYPE) == PACKED_POSITION_MASK_TYPE
875 ? PACKED_POSITION_TYPE_CHILD
876 : PACKED_POSITION_TYPE_GROUP;
877 }
878
879 /**
880 * Gets the group position from a packed position. See
881 * {@link #getPackedPositionForChild(int, int)}.
882 *
883 * @param packedPosition The packed position from which the group position
884 * will be returned.
885 * @return The group position portion of the packed position. If this does
886 * not contain a group, returns -1.
887 */
888 public static int getPackedPositionGroup(long packedPosition) {
889 // Null
890 if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1;
891
892 return (int) ((packedPosition & PACKED_POSITION_MASK_GROUP) >> PACKED_POSITION_SHIFT_GROUP);
893 }
894
895 /**
896 * Gets the child position from a packed position that is of
897 * {@link #PACKED_POSITION_TYPE_CHILD} type (use {@link #getPackedPositionType(long)}).
898 * To get the group that this child belongs to, use
899 * {@link #getPackedPositionGroup(long)}. See
900 * {@link #getPackedPositionForChild(int, int)}.
901 *
902 * @param packedPosition The packed position from which the child position
903 * will be returned.
904 * @return The child position portion of the packed position. If this does
905 * not contain a child, returns -1.
906 */
907 public static int getPackedPositionChild(long packedPosition) {
908 // Null
909 if (packedPosition == PACKED_POSITION_VALUE_NULL) return -1;
910
911 // Group since a group type clears this bit
912 if ((packedPosition & PACKED_POSITION_MASK_TYPE) != PACKED_POSITION_MASK_TYPE) return -1;
913
914 return (int) (packedPosition & PACKED_POSITION_MASK_CHILD);
915 }
916
917 /**
918 * Returns the packed position representation of a child's position.
919 * <p>
920 * In general, a packed position should be used in
921 * situations where the position given to/returned from an
922 * {@link ExpandableListAdapter} or {@link ExpandableListView} method can
923 * either be a child or group. The two positions are packed into a single
924 * long which can be unpacked using
925 * {@link #getPackedPositionChild(long)},
926 * {@link #getPackedPositionGroup(long)}, and
927 * {@link #getPackedPositionType(long)}.
928 *
929 * @param groupPosition The child's parent group's position.
930 * @param childPosition The child position within the group.
931 * @return The packed position representation of the child (and parent group).
932 */
933 public static long getPackedPositionForChild(int groupPosition, int childPosition) {
934 return (((long)PACKED_POSITION_TYPE_CHILD) << PACKED_POSITION_SHIFT_TYPE)
935 | ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP)
936 << PACKED_POSITION_SHIFT_GROUP)
937 | (childPosition & PACKED_POSITION_INT_MASK_CHILD);
938 }
939
940 /**
941 * Returns the packed position representation of a group's position. See
942 * {@link #getPackedPositionForChild(int, int)}.
943 *
944 * @param groupPosition The child's parent group's position.
945 * @return The packed position representation of the group.
946 */
947 public static long getPackedPositionForGroup(int groupPosition) {
948 // No need to OR a type in because PACKED_POSITION_GROUP == 0
949 return ((((long)groupPosition) & PACKED_POSITION_INT_MASK_GROUP)
950 << PACKED_POSITION_SHIFT_GROUP);
951 }
952
953 @Override
954 ContextMenuInfo createContextMenuInfo(View view, int flatListPosition, long id) {
Gilles Debunne47ccdf32010-02-26 11:30:24 -0800955 if (isHeaderOrFooterPosition(flatListPosition)) {
956 // Return normal info for header/footer view context menus
Jeff Sharkeyfd0d6272009-08-17 00:52:46 -0700957 return new AdapterContextMenuInfo(view, flatListPosition, id);
958 }
959
Gilles Debunne272f3a92010-03-03 15:55:13 -0800960 final int adjustedPosition = getFlatPositionForConnector(flatListPosition);
Jeff Sharkeyfd0d6272009-08-17 00:52:46 -0700961 PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 ExpandableListPosition pos = pm.position;
963 pm.recycle();
964
965 id = getChildOrGroupId(pos);
966 long packedPosition = pos.getPackedPosition();
967 pos.recycle();
968
969 return new ExpandableListContextMenuInfo(view, packedPosition, id);
970 }
971
972 /**
973 * Gets the ID of the group or child at the given <code>position</code>.
974 * This is useful since there is no ListAdapter ID -> ExpandableListAdapter
975 * ID conversion mechanism (in some cases, it isn't possible).
976 *
977 * @param position The position of the child or group whose ID should be
978 * returned.
979 */
980 private long getChildOrGroupId(ExpandableListPosition position) {
981 if (position.type == ExpandableListPosition.CHILD) {
982 return mAdapter.getChildId(position.groupPos, position.childPos);
983 } else {
984 return mAdapter.getGroupId(position.groupPos);
985 }
986 }
987
988 /**
989 * Sets the indicator to be drawn next to a child.
990 *
991 * @param childIndicator The drawable to be used as an indicator. If the
992 * child is the last child for a group, the state
993 * {@link android.R.attr#state_last} will be set.
994 */
995 public void setChildIndicator(Drawable childIndicator) {
996 mChildIndicator = childIndicator;
997 }
998
999 /**
1000 * Sets the drawing bounds for the child indicator. For either, you can
1001 * specify {@link #CHILD_INDICATOR_INHERIT} to use inherit from the general
1002 * indicator's bounds.
1003 *
1004 * @see #setIndicatorBounds(int, int)
1005 * @param left The left position (relative to the left bounds of this View)
1006 * to start drawing the indicator.
1007 * @param right The right position (relative to the left bounds of this
1008 * View) to end the drawing of the indicator.
1009 */
1010 public void setChildIndicatorBounds(int left, int right) {
1011 mChildIndicatorLeft = left;
1012 mChildIndicatorRight = right;
1013 }
1014
1015 /**
1016 * Sets the indicator to be drawn next to a group.
1017 *
1018 * @param groupIndicator The drawable to be used as an indicator. If the
1019 * group is empty, the state {@link android.R.attr#state_empty} will be
1020 * set. If the group is expanded, the state
1021 * {@link android.R.attr#state_expanded} will be set.
1022 */
1023 public void setGroupIndicator(Drawable groupIndicator) {
1024 mGroupIndicator = groupIndicator;
Romain Guy875862e2011-01-17 11:37:24 -08001025 if (mIndicatorRight == 0 && mGroupIndicator != null) {
1026 mIndicatorRight = mIndicatorLeft + mGroupIndicator.getIntrinsicWidth();
1027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
Romain Guy875862e2011-01-17 11:37:24 -08001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 /**
1031 * Sets the drawing bounds for the indicators (at minimum, the group indicator
1032 * is affected by this; the child indicator is affected by this if the
1033 * child indicator bounds are set to inherit).
1034 *
1035 * @see #setChildIndicatorBounds(int, int)
1036 * @param left The left position (relative to the left bounds of this View)
1037 * to start drawing the indicator.
1038 * @param right The right position (relative to the left bounds of this
1039 * View) to end the drawing of the indicator.
1040 */
1041 public void setIndicatorBounds(int left, int right) {
1042 mIndicatorLeft = left;
1043 mIndicatorRight = right;
1044 }
1045
1046 /**
1047 * Extra menu information specific to an {@link ExpandableListView} provided
1048 * to the
1049 * {@link android.view.View.OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo) }
1050 * callback when a context menu is brought up for this AdapterView.
1051 */
1052 public static class ExpandableListContextMenuInfo implements ContextMenu.ContextMenuInfo {
1053
1054 public ExpandableListContextMenuInfo(View targetView, long packedPosition, long id) {
1055 this.targetView = targetView;
1056 this.packedPosition = packedPosition;
1057 this.id = id;
1058 }
1059
1060 /**
1061 * The view for which the context menu is being displayed. This
1062 * will be one of the children Views of this {@link ExpandableListView}.
1063 */
1064 public View targetView;
1065
1066 /**
1067 * The packed position in the list represented by the adapter for which
1068 * the context menu is being displayed. Use the methods
1069 * {@link ExpandableListView#getPackedPositionType},
1070 * {@link ExpandableListView#getPackedPositionChild}, and
1071 * {@link ExpandableListView#getPackedPositionGroup} to unpack this.
1072 */
1073 public long packedPosition;
1074
1075 /**
1076 * The ID of the item (group or child) for which the context menu is
1077 * being displayed.
1078 */
1079 public long id;
1080 }
1081
1082 static class SavedState extends BaseSavedState {
1083 ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList;
1084
1085 /**
1086 * Constructor called from {@link ExpandableListView#onSaveInstanceState()}
1087 */
1088 SavedState(
1089 Parcelable superState,
1090 ArrayList<ExpandableListConnector.GroupMetadata> expandedGroupMetadataList) {
1091 super(superState);
1092 this.expandedGroupMetadataList = expandedGroupMetadataList;
1093 }
1094
1095 /**
1096 * Constructor called from {@link #CREATOR}
1097 */
1098 private SavedState(Parcel in) {
1099 super(in);
1100 expandedGroupMetadataList = new ArrayList<ExpandableListConnector.GroupMetadata>();
1101 in.readList(expandedGroupMetadataList, ExpandableListConnector.class.getClassLoader());
1102 }
1103
1104 @Override
1105 public void writeToParcel(Parcel out, int flags) {
1106 super.writeToParcel(out, flags);
1107 out.writeList(expandedGroupMetadataList);
1108 }
1109
1110 public static final Parcelable.Creator<SavedState> CREATOR
1111 = new Parcelable.Creator<SavedState>() {
1112 public SavedState createFromParcel(Parcel in) {
1113 return new SavedState(in);
1114 }
1115
1116 public SavedState[] newArray(int size) {
1117 return new SavedState[size];
1118 }
1119 };
1120 }
1121
1122 @Override
1123 public Parcelable onSaveInstanceState() {
1124 Parcelable superState = super.onSaveInstanceState();
1125 return new SavedState(superState,
1126 mConnector != null ? mConnector.getExpandedGroupMetadataList() : null);
1127 }
1128
1129 @Override
1130 public void onRestoreInstanceState(Parcelable state) {
Romain Guy7444e142009-05-21 12:54:16 -07001131 if (!(state instanceof SavedState)) {
1132 super.onRestoreInstanceState(state);
1133 return;
1134 }
1135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 SavedState ss = (SavedState) state;
1137 super.onRestoreInstanceState(ss.getSuperState());
1138
1139 if (mConnector != null && ss.expandedGroupMetadataList != null) {
1140 mConnector.setExpandedGroupMetadataList(ss.expandedGroupMetadataList);
1141 }
1142 }
1143
1144}