37
37
* @param <C> The Child or content {@link ViewHolder}
38
38
*/
39
39
public abstract class RecyclerHeaderCursorAdapter <H extends ViewHolder , C extends ViewHolder > extends RecyclerCursorAdapter <ViewHolder > {
40
- public static final int VIEW_TYPE_CHILD = 1 ;
41
- public static final int VIEW_TYPE_HEADER = 10 ;
40
+ public static final int HEADER_VIEW_TYPE_MASK = 0x80000000 ;
42
41
43
42
private Observer observer = new Observer ();
44
43
protected Map <Long , Integer > headerChildCountMap = new HashMap <>();
@@ -48,17 +47,19 @@ public abstract class RecyclerHeaderCursorAdapter<H extends ViewHolder, C extend
48
47
* Called when the RecyclerView needs a new {@link H} ViewHolder
49
48
*
50
49
* @param parent The ViewGroup into which the new View will be added
50
+ * @param viewType The type for the header view
51
51
* @return The view type of the new View
52
52
*/
53
- public abstract H onCreateHeaderViewHolder (ViewGroup parent );
53
+ public abstract H onCreateHeaderViewHolder (ViewGroup parent , int viewType );
54
54
55
55
/**
56
56
* Called when the RecyclerView needs a new {@link C} ViewHolder
57
57
*
58
58
* @param parent The ViewGroup into which the new View will be added
59
+ * @param viewType The type for the child view
59
60
* @return The view type of the new View
60
61
*/
61
- public abstract C onCreateChildViewHolder (ViewGroup parent );
62
+ public abstract C onCreateChildViewHolder (ViewGroup parent , int viewType );
62
63
63
64
/**
64
65
* Called to display the header information with the <code>firstChildPosition</code> being the
@@ -97,22 +98,20 @@ public RecyclerHeaderCursorAdapter(Cursor cursor, String idColumnName) {
97
98
98
99
/**
99
100
* This method shouldn't be used directly, instead use
100
- * {@link #onCreateHeaderViewHolder(ViewGroup)} and
101
- * {@link #onCreateChildViewHolder(ViewGroup)}
101
+ * {@link #onCreateHeaderViewHolder(ViewGroup, int )} and
102
+ * {@link #onCreateChildViewHolder(ViewGroup, int )}
102
103
*
103
104
* @param parent The parent ViewGroup for the ViewHolder
104
105
* @param viewType The type for the ViewHolder
105
106
* @return The correct ViewHolder for the specified viewType
106
107
*/
107
108
@ Override
108
109
public ViewHolder onCreateViewHolder (ViewGroup parent , int viewType ) {
109
- if (viewType == VIEW_TYPE_CHILD ) {
110
- return onCreateChildViewHolder (parent );
111
- } else if (viewType == VIEW_TYPE_HEADER ) {
112
- return onCreateHeaderViewHolder (parent );
110
+ if ((viewType & HEADER_VIEW_TYPE_MASK ) != 0 ) {
111
+ return onCreateHeaderViewHolder (parent , viewType );
113
112
}
114
113
115
- return null ;
114
+ return onCreateChildViewHolder ( parent , viewType ) ;
116
115
}
117
116
118
117
/**
@@ -129,15 +128,16 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
129
128
public void onBindViewHolder (ViewHolder holder , Cursor cursor , int position ) {
130
129
int viewType = getItemViewType (position );
131
130
int childPosition = determineChildPosition (position );
132
-
133
131
Cursor c = getCursor (childPosition );
134
- if (viewType == VIEW_TYPE_CHILD ) {
135
- onBindChildViewHolder ((C ) holder , c , childPosition );
136
- holder .itemView .setTag (R .id .recyclerext_view_child_position , childPosition );
137
- } else if (viewType == VIEW_TYPE_HEADER ) {
132
+
133
+ if ((viewType & HEADER_VIEW_TYPE_MASK ) != 0 ) {
138
134
onBindHeaderViewHolder ((H ) holder , c , childPosition );
139
135
holder .itemView .setTag (R .id .recyclerext_view_child_position , childPosition );
136
+ return ;
140
137
}
138
+
139
+ onBindChildViewHolder ((C ) holder , c , childPosition );
140
+ holder .itemView .setTag (R .id .recyclerext_view_child_position , childPosition );
141
141
}
142
142
143
143
/**
@@ -148,15 +148,42 @@ public void onBindViewHolder(ViewHolder holder, Cursor cursor, int position) {
148
148
*/
149
149
@ Override
150
150
public int getItemViewType (int position ) {
151
+ int childPosition = determineChildPosition (position );
152
+
151
153
for (HeaderItem item : headerItems ) {
152
154
if (item .getViewPosition () == position ) {
153
- return VIEW_TYPE_HEADER ;
155
+ return getHeaderViewType ( childPosition ) | HEADER_VIEW_TYPE_MASK ;
154
156
} else if (item .getViewPosition () > position ) {
155
157
break ;
156
158
}
157
159
}
158
160
159
- return VIEW_TYPE_CHILD ;
161
+ return getChildViewType (childPosition ) & ~HEADER_VIEW_TYPE_MASK ;
162
+ }
163
+
164
+ /**
165
+ * Retrieves the view type for the header whos first child view
166
+ * has the <code>childPosition</code>. This value will be |'d with
167
+ * the {@link #HEADER_VIEW_TYPE_MASK} to make sure the header and child
168
+ * view types don't overlap
169
+ *
170
+ * @param childPosition The position for the fist child underneath the header
171
+ * @return The view type for the header view
172
+ */
173
+ public int getHeaderViewType (int childPosition ) {
174
+ return 0 ;
175
+ }
176
+
177
+ /**
178
+ * Retrieves the view type for the child view at the specified
179
+ * <code>childPosition</code>. This value will be &'ed with the
180
+ * inverse of {@link #HEADER_VIEW_TYPE_MASK} to make sure the header
181
+ * and child view types don't overlap.
182
+ * @param childPosition The position of the child to get the type for
183
+ * @return The view type for the child view
184
+ */
185
+ public int getChildViewType (int childPosition ) {
186
+ return 0 ;
160
187
}
161
188
162
189
/**
0 commit comments