ExpandableListPosition.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2007 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
19/**
20 * ExpandableListPosition can refer to either a group's position or a child's
21 * position. Referring to a child's position requires both a group position (the
22 * group containing the child) and a child position (the child's position within
23 * that group). To create objects, use {@link #obtainChildPosition(int, int)} or
24 * {@link #obtainGroupPosition(int)}.
25 */
26class ExpandableListPosition {
27    /**
28     * This data type represents a child position
29     */
30    public final static int CHILD = 1;
31
32    /**
33     * This data type represents a group position
34     */
35    public final static int GROUP = 2;
36
37    /**
38     * The position of either the group being referred to, or the parent
39     * group of the child being referred to
40     */
41    public int groupPos;
42
43    /**
44     * The position of the child within its parent group
45     */
46    public int childPos;
47
48    /**
49     * The position of the item in the flat list (optional, used internally when
50     * the corresponding flat list position for the group or child is known)
51     */
52    int flatListPos;
53
54    /**
55     * What type of position this ExpandableListPosition represents
56     */
57    public int type;
58
59    ExpandableListPosition(int type, int groupPos, int childPos, int flatListPos) {
60        this.type = type;
61        this.flatListPos = flatListPos;
62        this.groupPos = groupPos;
63        this.childPos = childPos;
64    }
65
66    /**
67     * Used internally by the {@link #obtainChildPosition} and
68     * {@link #obtainGroupPosition} methods to construct a new object.
69     */
70    private ExpandableListPosition(int type, int groupPos, int childPos) {
71        this.type = type;
72        this.groupPos = groupPos;
73        this.childPos = childPos;
74    }
75
76    long getPackedPosition() {
77        if (type == CHILD) return ExpandableListView.getPackedPositionForChild(groupPos, childPos);
78        else return ExpandableListView.getPackedPositionForGroup(groupPos);
79    }
80
81    static ExpandableListPosition obtainGroupPosition(int groupPosition) {
82        return new ExpandableListPosition(GROUP, groupPosition, 0);
83    }
84
85    static ExpandableListPosition obtainChildPosition(int groupPosition, int childPosition) {
86        return new ExpandableListPosition(CHILD, groupPosition, childPosition);
87    }
88
89    static ExpandableListPosition obtainPosition(long packedPosition) {
90        if (packedPosition == ExpandableListView.PACKED_POSITION_VALUE_NULL) {
91            return null;
92        }
93
94        final int type = ExpandableListView.getPackedPositionType(packedPosition) ==
95            ExpandableListView.PACKED_POSITION_TYPE_CHILD ? CHILD : GROUP;
96
97        return new ExpandableListPosition(type, ExpandableListView
98                .getPackedPositionGroup(packedPosition), ExpandableListView
99                .getPackedPositionChild(packedPosition));
100    }
101
102}
103