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