1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
| public abstract class HeaderFooterRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_MAX_COUNT = 1000; private static final int HEADER_VIEW_TYPE_OFFSET = 0; private static final int FOOTER_VIEW_TYPE_OFFSET = HEADER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT; private static final int CONTENT_VIEW_TYPE_OFFSET = FOOTER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT;
private int headerItemCount; private int contentItemCount; private int footerItemCount;
* {@inheritDoc} */ @Override public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType >= HEADER_VIEW_TYPE_OFFSET && viewType < HEADER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT) { return onCreateHeaderItemViewHolder(parent, viewType - HEADER_VIEW_TYPE_OFFSET); } else if (viewType >= FOOTER_VIEW_TYPE_OFFSET && viewType < FOOTER_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT) { return onCreateFooterItemViewHolder(parent, viewType - FOOTER_VIEW_TYPE_OFFSET); } else if (viewType >= CONTENT_VIEW_TYPE_OFFSET && viewType < CONTENT_VIEW_TYPE_OFFSET + VIEW_TYPE_MAX_COUNT) { return onCreateContentItemViewHolder(parent, viewType - CONTENT_VIEW_TYPE_OFFSET); } else { throw new IllegalStateException(); } }
* {@inheritDoc} */ @Override public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { if (headerItemCount > 0 && position < headerItemCount) { onBindHeaderItemViewHolder(viewHolder, position); } else if (contentItemCount > 0 && position - headerItemCount < contentItemCount) { onBindContentItemViewHolder(viewHolder, position - headerItemCount); } else { onBindFooterItemViewHolder(viewHolder, position - headerItemCount - contentItemCount); } }
* {@inheritDoc} */ @Override public final int getItemCount() { headerItemCount = getHeaderItemCount(); contentItemCount = getContentItemCount(); footerItemCount = getFooterItemCount(); return headerItemCount + contentItemCount + footerItemCount; }
* {@inheritDoc} */ @Override public final int getItemViewType(int position) { if (headerItemCount > 0 && position < headerItemCount) { return validateViewType(getHeaderItemViewType(position)) + HEADER_VIEW_TYPE_OFFSET; } else if (contentItemCount > 0 && position - headerItemCount < contentItemCount) { return validateViewType(getContentItemViewType(position - headerItemCount)) + CONTENT_VIEW_TYPE_OFFSET; } else { return validateViewType(getFooterItemViewType(position - headerItemCount - contentItemCount)) + FOOTER_VIEW_TYPE_OFFSET; } }
* Validates that the view type is within the valid range. * * @param viewType the view type. * @return the given view type. */ private int validateViewType(int viewType) { if (viewType < 0 || viewType >= VIEW_TYPE_MAX_COUNT) { throw new IllegalStateException("viewType must be between 0 and " + VIEW_TYPE_MAX_COUNT); } return viewType; }
* Notifies that a header item is inserted. * * @param position the position of the header item. */ public final void notifyHeaderItemInserted(int position) { int newHeaderItemCount = getHeaderItemCount(); if (position < 0 || position >= newHeaderItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for header items [0 - " + (newHeaderItemCount - 1) + "]."); } notifyItemInserted(position); }
* Notifies that multiple header items are inserted. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyHeaderItemRangeInserted(int positionStart, int itemCount) { int newHeaderItemCount = getHeaderItemCount(); if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > newHeaderItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for header items [0 - " + (newHeaderItemCount - 1) + "]."); } notifyItemRangeInserted(positionStart, itemCount); }
* Notifies that a header item is changed. * * @param position the position. */ public final void notifyHeaderItemChanged(int position) { if (position < 0 || position >= headerItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for header items [0 - " + (headerItemCount - 1) + "]."); } notifyItemChanged(position); }
* Notifies that multiple header items are changed. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyHeaderItemRangeChanged(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount >= headerItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for header items [0 - " + (headerItemCount - 1) + "]."); } notifyItemRangeChanged(positionStart, itemCount); }
* Notifies that an existing header item is moved to another position. * * @param fromPosition the original position. * @param toPosition the new position. */ public void notifyHeaderItemMoved(int fromPosition, int toPosition) { if (fromPosition < 0 || toPosition < 0 || fromPosition >= headerItemCount || toPosition >= headerItemCount) { throw new IndexOutOfBoundsException("The given fromPosition " + fromPosition + " or toPosition " + toPosition + " is not within the position bounds for header items [0 - " + (headerItemCount - 1) + "]."); } notifyItemMoved(fromPosition, toPosition); }
* Notifies that a header item is removed. * * @param position the position. */ public void notifyHeaderItemRemoved(int position) { if (position < 0 || position >= headerItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for header items [0 - " + (headerItemCount - 1) + "]."); } notifyItemRemoved(position); }
* Notifies that multiple header items are removed. * * @param positionStart the position. * @param itemCount the item count. */ public void notifyHeaderItemRangeRemoved(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > headerItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for header items [0 - " + (headerItemCount - 1) + "]."); } notifyItemRangeRemoved(positionStart, itemCount); }
* Notifies that a content item is inserted. * * @param position the position of the content item. */ public final void notifyContentItemInserted(int position) { int newHeaderItemCount = getHeaderItemCount(); int newContentItemCount = getContentItemCount(); if (position < 0 || position >= newContentItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for content items [0 - " + (newContentItemCount - 1) + "]."); } notifyItemInserted(position + newHeaderItemCount); }
* Notifies that multiple content items are inserted. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyContentItemRangeInserted(int positionStart, int itemCount) { int newHeaderItemCount = getHeaderItemCount(); int newContentItemCount = getContentItemCount(); if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > newContentItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for content items [0 - " + (newContentItemCount - 1) + "]."); } notifyItemRangeInserted(positionStart + newHeaderItemCount, itemCount); }
* Notifies that a content item is changed. * * @param position the position. */ public final void notifyContentItemChanged(int position) { if (position < 0 || position >= contentItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for content items [0 - " + (contentItemCount - 1) + "]."); } notifyItemChanged(position + headerItemCount); }
* Notifies that multiple content items are changed. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyContentItemRangeChanged(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > contentItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for content items [0 - " + (contentItemCount - 1) + "]."); } notifyItemRangeChanged(positionStart + headerItemCount, itemCount); }
* Notifies that an existing content item is moved to another position. * * @param fromPosition the original position. * @param toPosition the new position. */ public final void notifyContentItemMoved(int fromPosition, int toPosition) { if (fromPosition < 0 || toPosition < 0 || fromPosition >= contentItemCount || toPosition >= contentItemCount) { throw new IndexOutOfBoundsException("The given fromPosition " + fromPosition + " or toPosition " + toPosition + " is not within the position bounds for content items [0 - " + (contentItemCount - 1) + "]."); } notifyItemMoved(fromPosition + headerItemCount, toPosition + headerItemCount); }
* Notifies that a content item is removed. * * @param position the position. */ public final void notifyContentItemRemoved(int position) { if (position < 0 || position >= contentItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for content items [0 - " + (contentItemCount - 1) + "]."); } notifyItemRemoved(position + headerItemCount); }
* Notifies that multiple content items are removed. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyContentItemRangeRemoved(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > contentItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for content items [0 - " + (contentItemCount - 1) + "]."); } notifyItemRangeRemoved(positionStart + headerItemCount, itemCount); }
* Notifies that a footer item is inserted. * * @param position the position of the content item. */ public final void notifyFooterItemInserted(int position) { int newHeaderItemCount = getHeaderItemCount(); int newContentItemCount = getContentItemCount(); int newFooterItemCount = getFooterItemCount(); if (position < 0 || position >= newFooterItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for footer items [0 - " + (newFooterItemCount - 1) + "]."); } notifyItemInserted(position + newHeaderItemCount + newContentItemCount); }
* Notifies that multiple footer items are inserted. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyFooterItemRangeInserted(int positionStart, int itemCount) { int newHeaderItemCount = getHeaderItemCount(); int newContentItemCount = getContentItemCount(); int newFooterItemCount = getFooterItemCount(); if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > newFooterItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for footer items [0 - " + (newFooterItemCount - 1) + "]."); } notifyItemRangeInserted(positionStart + newHeaderItemCount + newContentItemCount, itemCount); }
* Notifies that a footer item is changed. * * @param position the position. */ public final void notifyFooterItemChanged(int position) { if (position < 0 || position >= footerItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for footer items [0 - " + (footerItemCount - 1) + "]."); } notifyItemChanged(position + headerItemCount + contentItemCount); }
* Notifies that multiple footer items are changed. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyFooterItemRangeChanged(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > footerItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for footer items [0 - " + (footerItemCount - 1) + "]."); } notifyItemRangeChanged(positionStart + headerItemCount + contentItemCount, itemCount); }
* Notifies that an existing footer item is moved to another position. * * @param fromPosition the original position. * @param toPosition the new position. */ public final void notifyFooterItemMoved(int fromPosition, int toPosition) { if (fromPosition < 0 || toPosition < 0 || fromPosition >= footerItemCount || toPosition >= footerItemCount) { throw new IndexOutOfBoundsException("The given fromPosition " + fromPosition + " or toPosition " + toPosition + " is not within the position bounds for footer items [0 - " + (footerItemCount - 1) + "]."); } notifyItemMoved(fromPosition + headerItemCount + contentItemCount, toPosition + headerItemCount + contentItemCount); }
* Notifies that a footer item is removed. * * @param position the position. */ public final void notifyFooterItemRemoved(int position) { if (position < 0 || position >= footerItemCount) { throw new IndexOutOfBoundsException("The given position " + position + " is not within the position bounds for footer items [0 - " + (footerItemCount - 1) + "]."); } notifyItemRemoved(position + headerItemCount + contentItemCount); }
* Notifies that multiple footer items are removed. * * @param positionStart the position. * @param itemCount the item count. */ public final void notifyFooterItemRangeRemoved(int positionStart, int itemCount) { if (positionStart < 0 || itemCount < 0 || positionStart + itemCount > footerItemCount) { throw new IndexOutOfBoundsException("The given range [" + positionStart + " - " + (positionStart + itemCount - 1) + "] is not within the position bounds for footer items [0 - " + (footerItemCount - 1) + "]."); } notifyItemRangeRemoved(positionStart + headerItemCount + contentItemCount, itemCount); }
* Gets the header item view type. By default, this method returns 0. * * @param position the position. * @return the header item view type (within the range [0 - VIEW_TYPE_MAX_COUNT-1]). */ protected int getHeaderItemViewType(int position) { return 0; }
* Gets the footer item view type. By default, this method returns 0. * * @param position the position. * @return the footer item view type (within the range [0 - VIEW_TYPE_MAX_COUNT-1]). */ protected int getFooterItemViewType(int position) { return 0; }
* Gets the content item view type. By default, this method returns 0. * * @param position the position. * @return the content item view type (within the range [0 - VIEW_TYPE_MAX_COUNT-1]). */ protected int getContentItemViewType(int position) { return 0; }
* Gets the header item count. This method can be called several times, so it should not calculate the count every time. * * @return the header item count. */ protected abstract int getHeaderItemCount();
* Gets the footer item count. This method can be called several times, so it should not calculate the count every time. * * @return the footer item count. */ protected abstract int getFooterItemCount();
* Gets the content item count. This method can be called several times, so it should not calculate the count every time. * * @return the content item count. */ protected abstract int getContentItemCount();
* This method works exactly the same as {@link #onCreateViewHolder(android.view.ViewGroup, int)}, but for header items. * * @param parent the parent view. * @param headerViewType the view type for the header. * @return the view holder. */ protected abstract RecyclerView.ViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType);
* This method works exactly the same as {@link #onCreateViewHolder(android.view.ViewGroup, int)}, but for footer items. * * @param parent the parent view. * @param footerViewType the view type for the footer. * @return the view holder. */ protected abstract RecyclerView.ViewHolder onCreateFooterItemViewHolder(ViewGroup parent, int footerViewType);
* This method works exactly the same as {@link #onCreateViewHolder(android.view.ViewGroup, int)}, but for content items. * * @param parent the parent view. * @param contentViewType the view type for the content. * @return the view holder. */ protected abstract RecyclerView.ViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType);
* This method works exactly the same as {@link #onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder, int)}, but for header items. * * @param headerViewHolder the view holder for the header item. * @param position the position. */ protected abstract void onBindHeaderItemViewHolder(RecyclerView.ViewHolder headerViewHolder, int position);
* This method works exactly the same as {@link #onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder, int)}, but for footer items. * * @param footerViewHolder the view holder for the footer item. * @param position the position. */ protected abstract void onBindFooterItemViewHolder(RecyclerView.ViewHolder footerViewHolder, int position);
* This method works exactly the same as {@link #onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder, int)}, but for content items. * * @param contentViewHolder the view holder for the content item. * @param position the position. */ protected abstract void onBindContentItemViewHolder(RecyclerView.ViewHolder contentViewHolder, int position);
}
|