| 1 | /* |
| 2 | * Copyright 2018 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 androidx.recyclerview.widget; |
| 18 | |
| 19 | import android.util.Log; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewGroup; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Helper class to manage children. |
| 28 | * <p> |
| 29 | * It wraps a RecyclerView and adds ability to hide some children. There are two sets of methods |
| 30 | * provided by this class. <b>Regular</b> methods are the ones that replicate ViewGroup methods |
| 31 | * like getChildAt, getChildCount etc. These methods ignore hidden children. |
| 32 | * <p> |
| 33 | * When RecyclerView needs direct access to the view group children, it can call unfiltered |
| 34 | * methods like get getUnfilteredChildCount or getUnfilteredChildAt. |
| 35 | */ |
| 36 | class ChildHelper { |
| 37 | |
| 38 | private static final boolean DEBUG = false; |
| 39 | |
| 40 | private static final String TAG = "ChildrenHelper"; |
| 41 | |
| 42 | final Callback mCallback; |
| 43 | |
| 44 | final Bucket mBucket; |
| 45 | |
| 46 | final List<View> mHiddenViews; |
| 47 | |
| 48 | ChildHelper(Callback callback) { |
| 49 | mCallback = callback; |
| 50 | mBucket = new Bucket(); |
| 51 | mHiddenViews = new ArrayList<View>(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Marks a child view as hidden |
| 56 | * |
| 57 | * @param child View to hide. |
| 58 | */ |
| 59 | private void hideViewInternal(View child) { |
| 60 | mHiddenViews.add(child); |
| 61 | mCallback.onEnteredHiddenState(child); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Unmarks a child view as hidden. |
| 66 | * |
| 67 | * @param child View to hide. |
| 68 | */ |
| 69 | private boolean unhideViewInternal(View child) { |
| 70 | if (mHiddenViews.remove(child)) { |
| 71 | mCallback.onLeftHiddenState(child); |
| 72 | return true; |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | protected int getHiddenChildCount() { |
| 79 | return mHiddenViews.size(); |
| 80 | } |
| 81 | |
| 82 | public View getHiddenChildAt(int index) { |
| 83 | if (index < 0 || index >= mHiddenViews.size()) { |
| 84 | return null; |
| 85 | } |
| 86 | return mHiddenViews.get(index); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds a view to the ViewGroup |
| 91 | * |
| 92 | * @param child View to add. |
| 93 | * @param hidden If set to true, this item will be invisible from regular methods. |
| 94 | */ |
| 95 | void addView(View child, boolean hidden) { |
| 96 | addView(child, -1, hidden); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add a view to the ViewGroup at an index |
| 101 | * |
| 102 | * @param child View to add. |
| 103 | * @param index Index of the child from the regular perspective (excluding hidden views). |
| 104 | * ChildHelper offsets this index to actual ViewGroup index. |
| 105 | * @param hidden If set to true, this item will be invisible from regular methods. |
| 106 | */ |
| 107 | void addView(View child, int index, boolean hidden) { |
| 108 | final int offset; |
| 109 | if (index < 0) { |
| 110 | offset = mCallback.getChildCount(); |
| 111 | } else { |
| 112 | offset = getOffset(index); |
| 113 | } |
| 114 | mBucket.insert(offset, hidden); |
| 115 | if (hidden) { |
| 116 | hideViewInternal(child); |
| 117 | } |
| 118 | mCallback.addView(child, offset); |
| 119 | if (DEBUG) { |
| 120 | Log.d(TAG, "addViewAt " + index + ",h:" + hidden + ", " + this); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private int getOffset(int index) { |
| 125 | if (index < 0) { |
| 126 | return -1; //anything below 0 won't work as diff will be undefined. |
| 127 | } |
| 128 | final int limit = mCallback.getChildCount(); |
| 129 | int offset = index; |
| 130 | while (offset < limit) { |
| 131 | final int removedBefore = mBucket.countOnesBefore(offset); |
| 132 | final int diff = index - (offset - removedBefore); |
| 133 | if (diff == 0) { |
| 134 | while (mBucket.get(offset)) { // ensure this offset is not hidden |
| 135 | offset++; |
| 136 | } |
| 137 | return offset; |
| 138 | } else { |
| 139 | offset += diff; |
| 140 | } |
| 141 | } |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Removes the provided View from underlying RecyclerView. |
| 147 | * |
| 148 | * @param view The view to remove. |
| 149 | */ |
| 150 | void removeView(View view) { |
| 151 | int index = mCallback.indexOfChild(view); |
| 152 | if (index < 0) { |
| 153 | return; |
| 154 | } |
| 155 | if (mBucket.remove(index)) { |
| 156 | unhideViewInternal(view); |
| 157 | } |
| 158 | mCallback.removeViewAt(index); |
| 159 | if (DEBUG) { |
| 160 | Log.d(TAG, "remove View off:" + index + "," + this); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Removes the view at the provided index from RecyclerView. |
| 166 | * |
| 167 | * @param index Index of the child from the regular perspective (excluding hidden views). |
| 168 | * ChildHelper offsets this index to actual ViewGroup index. |
| 169 | */ |
| 170 | void removeViewAt(int index) { |
| 171 | final int offset = getOffset(index); |
| 172 | final View view = mCallback.getChildAt(offset); |
| 173 | if (view == null) { |
| 174 | return; |
| 175 | } |
| 176 | if (mBucket.remove(offset)) { |
| 177 | unhideViewInternal(view); |
| 178 | } |
| 179 | mCallback.removeViewAt(offset); |
| 180 | if (DEBUG) { |
| 181 | Log.d(TAG, "removeViewAt " + index + ", off:" + offset + ", " + this); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns the child at provided index. |
| 187 | * |
| 188 | * @param index Index of the child to return in regular perspective. |
| 189 | */ |
| 190 | View getChildAt(int index) { |
| 191 | final int offset = getOffset(index); |
| 192 | return mCallback.getChildAt(offset); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Removes all views from the ViewGroup including the hidden ones. |
| 197 | */ |
| 198 | void removeAllViewsUnfiltered() { |
| 199 | mBucket.reset(); |
| 200 | for (int i = mHiddenViews.size() - 1; i >= 0; i--) { |
| 201 | mCallback.onLeftHiddenState(mHiddenViews.get(i)); |
| 202 | mHiddenViews.remove(i); |
| 203 | } |
| 204 | mCallback.removeAllViews(); |
| 205 | if (DEBUG) { |
| 206 | Log.d(TAG, "removeAllViewsUnfiltered"); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * This can be used to find a disappearing view by position. |
| 212 | * |
| 213 | * @param position The adapter position of the item. |
| 214 | * @return A hidden view with a valid ViewHolder that matches the position. |
| 215 | */ |
| 216 | View findHiddenNonRemovedView(int position) { |
| 217 | final int count = mHiddenViews.size(); |
| 218 | for (int i = 0; i < count; i++) { |
| 219 | final View view = mHiddenViews.get(i); |
| 220 | RecyclerView.ViewHolder holder = mCallback.getChildViewHolder(view); |
| 221 | if (holder.getLayoutPosition() == position |
| 222 | && !holder.isInvalid() |
| 223 | && !holder.isRemoved()) { |
| 224 | return view; |
| 225 | } |
| 226 | } |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Attaches the provided view to the underlying ViewGroup. |
| 232 | * |
| 233 | * @param child Child to attach. |
| 234 | * @param index Index of the child to attach in regular perspective. |
| 235 | * @param layoutParams LayoutParams for the child. |
| 236 | * @param hidden If set to true, this item will be invisible to the regular methods. |
| 237 | */ |
| 238 | void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams, |
| 239 | boolean hidden) { |
| 240 | final int offset; |
| 241 | if (index < 0) { |
| 242 | offset = mCallback.getChildCount(); |
| 243 | } else { |
| 244 | offset = getOffset(index); |
| 245 | } |
| 246 | mBucket.insert(offset, hidden); |
| 247 | if (hidden) { |
| 248 | hideViewInternal(child); |
| 249 | } |
| 250 | mCallback.attachViewToParent(child, offset, layoutParams); |
| 251 | if (DEBUG) { |
| 252 | Log.d(TAG, "attach view to parent index:" + index + ",off:" + offset + "," |
| 253 | + "h:" + hidden + ", " + this); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns the number of children that are not hidden. |
| 259 | * |
| 260 | * @return Number of children that are not hidden. |
| 261 | * @see #getChildAt(int) |
| 262 | */ |
| 263 | int getChildCount() { |
| 264 | return mCallback.getChildCount() - mHiddenViews.size(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns the total number of children. |
| 269 | * |
| 270 | * @return The total number of children including the hidden views. |
| 271 | * @see #getUnfilteredChildAt(int) |
| 272 | */ |
| 273 | int getUnfilteredChildCount() { |
| 274 | return mCallback.getChildCount(); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Returns a child by ViewGroup offset. ChildHelper won't offset this index. |
| 279 | * |
| 280 | * @param index ViewGroup index of the child to return. |
| 281 | * @return The view in the provided index. |
| 282 | */ |
| 283 | View getUnfilteredChildAt(int index) { |
| 284 | return mCallback.getChildAt(index); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Detaches the view at the provided index. |
| 289 | * |
| 290 | * @param index Index of the child to return in regular perspective. |
| 291 | */ |
| 292 | void detachViewFromParent(int index) { |
| 293 | final int offset = getOffset(index); |
| 294 | mBucket.remove(offset); |
| 295 | mCallback.detachViewFromParent(offset); |
| 296 | if (DEBUG) { |
| 297 | Log.d(TAG, "detach view from parent " + index + ", off:" + offset); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns the index of the child in regular perspective. |
| 303 | * |
| 304 | * @param child The child whose index will be returned. |
| 305 | * @return The regular perspective index of the child or -1 if it does not exists. |
| 306 | */ |
| 307 | int indexOfChild(View child) { |
| 308 | final int index = mCallback.indexOfChild(child); |
| 309 | if (index == -1) { |
| 310 | return -1; |
| 311 | } |
| 312 | if (mBucket.get(index)) { |
| 313 | if (DEBUG) { |
| 314 | throw new IllegalArgumentException("cannot get index of a hidden child"); |
| 315 | } else { |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | // reverse the index |
| 320 | return index - mBucket.countOnesBefore(index); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Returns whether a View is visible to LayoutManager or not. |
| 325 | * |
| 326 | * @param view The child view to check. Should be a child of the Callback. |
| 327 | * @return True if the View is not visible to LayoutManager |
| 328 | */ |
| 329 | boolean isHidden(View view) { |
| 330 | return mHiddenViews.contains(view); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Marks a child view as hidden. |
| 335 | * |
| 336 | * @param view The view to hide. |
| 337 | */ |
| 338 | void hide(View view) { |
| 339 | final int offset = mCallback.indexOfChild(view); |
| 340 | if (offset < 0) { |
| 341 | throw new IllegalArgumentException("view is not a child, cannot hide " + view); |
| 342 | } |
| 343 | if (DEBUG && mBucket.get(offset)) { |
| 344 | throw new RuntimeException("trying to hide same view twice, how come ? " + view); |
| 345 | } |
| 346 | mBucket.set(offset); |
| 347 | hideViewInternal(view); |
| 348 | if (DEBUG) { |
| 349 | Log.d(TAG, "hiding child " + view + " at offset " + offset + ", " + this); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Moves a child view from hidden list to regular list. |
| 355 | * Calling this method should probably be followed by a detach, otherwise, it will suddenly |
| 356 | * show up in LayoutManager's children list. |
| 357 | * |
| 358 | * @param view The hidden View to unhide |
| 359 | */ |
| 360 | void unhide(View view) { |
| 361 | final int offset = mCallback.indexOfChild(view); |
| 362 | if (offset < 0) { |
| 363 | throw new IllegalArgumentException("view is not a child, cannot hide " + view); |
| 364 | } |
| 365 | if (!mBucket.get(offset)) { |
| 366 | throw new RuntimeException("trying to unhide a view that was not hidden" + view); |
| 367 | } |
| 368 | mBucket.clear(offset); |
| 369 | unhideViewInternal(view); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public String toString() { |
| 374 | return mBucket.toString() + ", hidden list:" + mHiddenViews.size(); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Removes a view from the ViewGroup if it is hidden. |
| 379 | * |
| 380 | * @param view The view to remove. |
| 381 | * @return True if the View is found and it is hidden. False otherwise. |
| 382 | */ |
| 383 | boolean removeViewIfHidden(View view) { |
| 384 | final int index = mCallback.indexOfChild(view); |
| 385 | if (index == -1) { |
| 386 | if (unhideViewInternal(view) && DEBUG) { |
| 387 | throw new IllegalStateException("view is in hidden list but not in view group"); |
| 388 | } |
| 389 | return true; |
| 390 | } |
| 391 | if (mBucket.get(index)) { |
| 392 | mBucket.remove(index); |
| 393 | if (!unhideViewInternal(view) && DEBUG) { |
| 394 | throw new IllegalStateException( |
| 395 | "removed a hidden view but it is not in hidden views list"); |
| 396 | } |
| 397 | mCallback.removeViewAt(index); |
| 398 | return true; |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Bitset implementation that provides methods to offset indices. |
| 405 | */ |
| 406 | static class Bucket { |
| 407 | |
| 408 | static final int BITS_PER_WORD = Long.SIZE; |
| 409 | |
| 410 | static final long LAST_BIT = 1L << (Long.SIZE - 1); |
| 411 | |
| 412 | long mData = 0; |
| 413 | |
| 414 | Bucket mNext; |
| 415 | |
| 416 | void set(int index) { |
| 417 | if (index >= BITS_PER_WORD) { |
| 418 | ensureNext(); |
| 419 | mNext.set(index - BITS_PER_WORD); |
| 420 | } else { |
| 421 | mData |= 1L << index; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private void ensureNext() { |
| 426 | if (mNext == null) { |
| 427 | mNext = new Bucket(); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void clear(int index) { |
| 432 | if (index >= BITS_PER_WORD) { |
| 433 | if (mNext != null) { |
| 434 | mNext.clear(index - BITS_PER_WORD); |
| 435 | } |
| 436 | } else { |
| 437 | mData &= ~(1L << index); |
| 438 | } |
| 439 | |
| 440 | } |
| 441 | |
| 442 | boolean get(int index) { |
| 443 | if (index >= BITS_PER_WORD) { |
| 444 | ensureNext(); |
| 445 | return mNext.get(index - BITS_PER_WORD); |
| 446 | } else { |
| 447 | return (mData & (1L << index)) != 0; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void reset() { |
| 452 | mData = 0; |
| 453 | if (mNext != null) { |
| 454 | mNext.reset(); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void insert(int index, boolean value) { |
| 459 | if (index >= BITS_PER_WORD) { |
| 460 | ensureNext(); |
| 461 | mNext.insert(index - BITS_PER_WORD, value); |
| 462 | } else { |
| 463 | final boolean lastBit = (mData & LAST_BIT) != 0; |
| 464 | long mask = (1L << index) - 1; |
| 465 | final long before = mData & mask; |
| 466 | final long after = ((mData & ~mask)) << 1; |
| 467 | mData = before | after; |
| 468 | if (value) { |
| 469 | set(index); |
| 470 | } else { |
| 471 | clear(index); |
| 472 | } |
| 473 | if (lastBit || mNext != null) { |
| 474 | ensureNext(); |
| 475 | mNext.insert(0, lastBit); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | boolean remove(int index) { |
| 481 | if (index >= BITS_PER_WORD) { |
| 482 | ensureNext(); |
| 483 | return mNext.remove(index - BITS_PER_WORD); |
| 484 | } else { |
| 485 | long mask = (1L << index); |
| 486 | final boolean value = (mData & mask) != 0; |
| 487 | mData &= ~mask; |
| 488 | mask = mask - 1; |
| 489 | final long before = mData & mask; |
| 490 | // cannot use >> because it adds one. |
| 491 | final long after = Long.rotateRight(mData & ~mask, 1); |
| 492 | mData = before | after; |
| 493 | if (mNext != null) { |
| 494 | if (mNext.get(0)) { |
| 495 | set(BITS_PER_WORD - 1); |
| 496 | } |
| 497 | mNext.remove(0); |
| 498 | } |
| 499 | return value; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | int countOnesBefore(int index) { |
| 504 | if (mNext == null) { |
| 505 | if (index >= BITS_PER_WORD) { |
| 506 | return Long.bitCount(mData); |
| 507 | } |
| 508 | return Long.bitCount(mData & ((1L << index) - 1)); |
| 509 | } |
| 510 | if (index < BITS_PER_WORD) { |
| 511 | return Long.bitCount(mData & ((1L << index) - 1)); |
| 512 | } else { |
| 513 | return mNext.countOnesBefore(index - BITS_PER_WORD) + Long.bitCount(mData); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public String toString() { |
| 519 | return mNext == null ? Long.toBinaryString(mData) |
| 520 | : mNext.toString() + "xx" + Long.toBinaryString(mData); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | interface Callback { |
| 525 | |
| 526 | int getChildCount(); |
| 527 | |
| 528 | void addView(View child, int index); |
| 529 | |
| 530 | int indexOfChild(View view); |
| 531 | |
| 532 | void removeViewAt(int index); |
| 533 | |
| 534 | View getChildAt(int offset); |
| 535 | |
| 536 | void removeAllViews(); |
| 537 | |
| 538 | RecyclerView.ViewHolder getChildViewHolder(View view); |
| 539 | |
| 540 | void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams); |
| 541 | |
| 542 | void detachViewFromParent(int offset); |
| 543 | |
| 544 | void onEnteredHiddenState(View child); |
| 545 | |
| 546 | void onLeftHiddenState(View child); |
| 547 | } |
| 548 | } |
| 549 | |