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