15
15
*/
16
16
package com .edmondapps .utils .android .ui ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .Iterator ;
20
- import java .util .List ;
21
-
22
18
import android .database .DataSetObserver ;
23
19
import android .view .View ;
24
20
import android .view .ViewGroup ;
25
21
import android .widget .BaseAdapter ;
26
22
import android .widget .ListAdapter ;
27
23
24
+ import java .util .ArrayList ;
25
+ import java .util .Iterator ;
26
+ import java .util .List ;
27
+
28
28
/**
29
29
* An adapter that combines multiple adapters.
30
30
* </p>
37
37
* that is responsible for the position, and use
38
38
* {@link #getPositionForAdapter(int)} to get the position for the adapter
39
39
* (instead of the position in the {@code CoumpoundAdapter}).
40
- * <p>
40
+ * <p/ >
41
41
* You may also use {@link #getAdapterInfo(int)} and
42
42
* {@link #getPositionForAdapter(int, AdapterInfo)} if the situation requires.
43
- *
43
+ *
44
44
* @author Edmond
45
- *
46
45
*/
47
46
public class CompoundAdapter extends BaseAdapter {
48
47
@@ -52,16 +51,15 @@ public class CompoundAdapter extends BaseAdapter {
52
51
* Most of the time you don't have to interact with it, however, a common
53
52
* use case is shown below, <code>
54
53
* <pre>
55
- public Object getItem(int position) {
56
- AdapterInfo info = getAdapterInfo(position);
57
- int positionForAdapter = getPositionForAdapter(position, info);
58
- return info.getAdapter().getItem(positionForAdapter);
59
- }
54
+ * public Object getItem(int position) {
55
+ * AdapterInfo info = getAdapterInfo(position);
56
+ * int positionForAdapter = getPositionForAdapter(position, info);
57
+ * return info.getAdapter().getItem(positionForAdapter);
58
+ * }
60
59
* </pre>
61
60
* </code>
62
- *
61
+ *
63
62
* @author Edmond
64
- *
65
63
*/
66
64
public static final class AdapterInfo {
67
65
private final BaseAdapter mAdapter ;
@@ -77,7 +75,6 @@ private AdapterInfo(BaseAdapter adapter) {
77
75
}
78
76
79
77
/**
80
- *
81
78
* @return the adapter that this {@code AdapterInfo} is about
82
79
*/
83
80
public BaseAdapter getAdapter () {
@@ -97,19 +94,17 @@ private int getViewTypeCount() {
97
94
}
98
95
99
96
/**
100
- *
101
97
* @return the position of this adapter in the {@code CompoundAdapter}
102
98
*/
103
99
public final int getPosition () {
104
100
return mPosition ;
105
101
}
106
102
107
103
/**
108
- *
109
- * @see ListAdapter#getItemViewType(int)
110
104
* @return the internal view type offset that is used to distinguish
111
- * view types
112
- * of different adapters
105
+ * view types
106
+ * of different adapters
107
+ * @see ListAdapter#getItemViewType(int)
113
108
*/
114
109
public final int getViewTypeOffset () {
115
110
return mViewTypeOffset ;
@@ -129,9 +124,8 @@ private void setViewTypeOffset(int viewTypeOffset) {
129
124
/**
130
125
* Constructs a {@code CompoundAdapter} that contains a single adapter. You
131
126
* may use {@link #addAdapter(BaseAdapter)} later on.
132
- *
133
- * @param adapter
134
- * a non-null instance of a {@code BaseAdapter}
127
+ *
128
+ * @param adapter a non-null instance of a {@code BaseAdapter}
135
129
*/
136
130
public CompoundAdapter (BaseAdapter adapter ) {
137
131
AdapterInfo info = new AdapterInfo (adapter );
@@ -143,11 +137,9 @@ public CompoundAdapter(BaseAdapter adapter) {
143
137
/**
144
138
* Constructs a {@code CompoundAdapter} that contains a 2 adapters. The
145
139
* parameters order determines the order of the list from top to bottom.
146
- *
147
- * @param adapter
148
- * the first non-null instance of a {@code BaseAdapter}
149
- * @param adapter2
150
- * the second non-null instance of a {@code BaseAdapter}
140
+ *
141
+ * @param adapter the first non-null instance of a {@code BaseAdapter}
142
+ * @param adapter2 the second non-null instance of a {@code BaseAdapter}
151
143
*/
152
144
public CompoundAdapter (BaseAdapter adapter , BaseAdapter adapter2 ) {
153
145
AdapterInfo info = new AdapterInfo (adapter );
@@ -161,13 +153,10 @@ public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2) {
161
153
/**
162
154
* Constructs a {@code CompoundAdapter} that contains more than 2 adapters.
163
155
* The parameters order determines the order of the list from top to bottom.
164
- *
165
- * @param adapter
166
- * the first non-null instance of a {@code BaseAdapter}
167
- * @param adapter2
168
- * the second non-null instance of a {@code BaseAdapter}
169
- * @param adapters
170
- * the other instances of {@code BaseAdapter}
156
+ *
157
+ * @param adapter the first non-null instance of a {@code BaseAdapter}
158
+ * @param adapter2 the second non-null instance of a {@code BaseAdapter}
159
+ * @param adapters the other instances of {@code BaseAdapter}
171
160
*/
172
161
public CompoundAdapter (BaseAdapter adapter , BaseAdapter adapter2 , BaseAdapter ... adapters ) {
173
162
AdapterInfo info = new AdapterInfo (adapter );
@@ -184,11 +173,8 @@ public CompoundAdapter(BaseAdapter adapter, BaseAdapter adapter2, BaseAdapter...
184
173
}
185
174
186
175
/**
187
- *
188
- * @param position
189
- * the position of the {@code CompoundAdapter}
190
- * @param info
191
- * usually retrieved by {@link #getAdapterInfo(int)}
176
+ * @param position the position of the {@code CompoundAdapter}
177
+ * @param info usually retrieved by {@link #getAdapterInfo(int)}
192
178
* @return the position of the adapter contained in the {@code AdapterInfo}
193
179
*/
194
180
public final static int getPositionForAdapter (int position , AdapterInfo info ) {
@@ -204,11 +190,8 @@ public final int getPositionForAdapter(int position) {
204
190
}
205
191
206
192
/**
207
- *
208
- * @param position
209
- * the position of the {@code CompoundAdapter}
210
- * @param info
211
- * usually retrieved by {@link #getAdapterInfo(int)}
193
+ * @param position the position of the {@code CompoundAdapter}
194
+ * @param info usually retrieved by {@link #getAdapterInfo(int)}
212
195
* @return the view type of the adapter contained in the {@code AdapterInfo}
213
196
*/
214
197
public final static int getViewTypeForAdapter (int position , AdapterInfo info ) {
@@ -227,10 +210,9 @@ public final int getViewTypeForAdapter(int position) {
227
210
/**
228
211
* Append an adapter to the end of this {@code CompoundAdapter}.<br>
229
212
* This {@code CompoundAdapter} will also refresh itself.
230
- *
213
+ *
214
+ * @param adapter a non-null instance of a {@code BaseAdapter}
231
215
* @see #notifyDataSetChanged()
232
- * @param adapter
233
- * a non-null instance of a {@code BaseAdapter}
234
216
*/
235
217
public void addAdapter (BaseAdapter adapter ) {
236
218
mAdapters .add (new AdapterInfo (adapter ));
@@ -241,11 +223,10 @@ public void addAdapter(BaseAdapter adapter) {
241
223
* Performs a linear search of the adapters in this {@code CompoundAdapter}
242
224
* to remove the provided adapter. If it does remove the adapter, this
243
225
* adapter will refresh itself.
244
- *
245
- * @see #notifyDataSetChanged()
246
- * @param adapter
247
- * an instance of {@code BaseAdapter}
226
+ *
227
+ * @param adapter an instance of {@code BaseAdapter}
248
228
* @return if it has removed the provided adapter
229
+ * @see #notifyDataSetChanged()
249
230
*/
250
231
public boolean removeAdapter (BaseAdapter adapter ) {
251
232
Iterator <AdapterInfo > it = mAdapters .iterator ();
@@ -269,17 +250,13 @@ private void updateOffset() {
269
250
info .setPosition (count );
270
251
info .setViewTypeOffset (viewTypeCount );
271
252
272
- if (!info .isEmpty ()) {
273
- count += info .getCount ();
274
- }
253
+ count += info .getCount ();
275
254
viewTypeCount += info .getViewTypeCount ();
276
255
}
277
256
}
278
257
279
258
/**
280
- *
281
- * @param position
282
- * position of the {@code CompoundAdapter}
259
+ * @param position position of the {@code CompoundAdapter}
283
260
* @return the info about the adapter that is managing this position
284
261
*/
285
262
public final AdapterInfo getAdapterInfo (int position ) {
0 commit comments