| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.Context; |
| 23 |
import android.database.DataSetObserver; |
| 24 |
import android.os.Build; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.os.SystemClock; |
| 27 |
import android.util.AttributeSet; |
| 28 |
import android.util.SparseArray; |
| 29 |
import android.view.ContextMenu; |
| 30 |
import android.view.ContextMenu.ContextMenuInfo; |
| 31 |
import android.view.SoundEffectConstants; |
| 32 |
import android.view.View; |
| 33 |
import android.view.ViewDebug; |
| 34 |
import android.view.ViewGroup; |
| 35 |
import android.view.ViewHierarchyEncoder; |
| 36 |
import android.view.ViewStructure; |
| 37 |
import android.view.accessibility.AccessibilityEvent; |
| 38 |
import android.view.accessibility.AccessibilityManager; |
| 39 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 40 |
import android.view.autofill.AutofillManager; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public abstract class AdapterView<T extends Adapter> extends ViewGroup { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int ITEM_VIEW_TYPE_IGNORE = -1; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static final int ITEM_VIEW_TYPE_HEADER_OR_FOOTER = -2; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@ViewDebug.ExportedProperty(category = "scrolling") |
| 73 |
@UnsupportedAppUsage |
| 74 |
int mFirstPosition = 0; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
int mSpecificTop; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@UnsupportedAppUsage |
| 86 |
int mSyncPosition; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
long mSyncRowId = INVALID_ROW_ID; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
long mSyncHeight; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@UnsupportedAppUsage |
| 102 |
boolean mNeedSync = false; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
int mSyncMode; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private int mLayoutHeight; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static final int SYNC_SELECTED_POSITION = 0; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static final int SYNC_FIRST_POSITION = 1; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
static final int SYNC_MAX_DURATION_MILLIS = 100; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
boolean mInLayout = false; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@UnsupportedAppUsage |
| 140 |
OnItemSelectedListener mOnItemSelectedListener; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@UnsupportedAppUsage |
| 146 |
OnItemClickListener mOnItemClickListener; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
OnItemLongClickListener mOnItemLongClickListener; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123768524) |
| 157 |
boolean mDataChanged; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@ViewDebug.ExportedProperty(category = "list") |
| 164 |
@UnsupportedAppUsage |
| 165 |
int mNextSelectedPosition = INVALID_POSITION; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@UnsupportedAppUsage |
| 171 |
long mNextSelectedRowId = INVALID_ROW_ID; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
@ViewDebug.ExportedProperty(category = "list") |
| 177 |
@UnsupportedAppUsage |
| 178 |
int mSelectedPosition = INVALID_POSITION; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
long mSelectedRowId = INVALID_ROW_ID; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private View mEmptyView; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@ViewDebug.ExportedProperty(category = "list") |
| 194 |
int mItemCount; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
int mOldItemCount; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public static final int INVALID_POSITION = -1; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static final long INVALID_ROW_ID = Long.MIN_VALUE; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@UnsupportedAppUsage |
| 216 |
int mOldSelectedPosition = INVALID_POSITION; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
long mOldSelectedRowId = INVALID_ROW_ID; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private int mDesiredFocusableState = FOCUSABLE_AUTO; |
| 233 |
private boolean mDesiredFocusableInTouchModeState; |
| 234 |
|
| 235 |
|
| 236 |
private SelectionNotifier mSelectionNotifier; |
| 237 |
|
| 238 |
|
| 239 |
private SelectionNotifier mPendingSelectionNotifier; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
boolean mBlockLayoutRequests = false; |
| 246 |
|
| 247 |
public AdapterView(Context context) { |
| 248 |
this(context, null); |
| 249 |
} |
| 250 |
|
| 251 |
public AdapterView(Context context, AttributeSet attrs) { |
| 252 |
this(context, attrs, 0); |
| 253 |
} |
| 254 |
|
| 255 |
public AdapterView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 256 |
this(context, attrs, defStyleAttr, 0); |
| 257 |
} |
| 258 |
|
| 259 |
public AdapterView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 260 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 261 |
|
| 262 |
|
| 263 |
if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 264 |
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 265 |
} |
| 266 |
|
| 267 |
mDesiredFocusableState = getFocusable(); |
| 268 |
if (mDesiredFocusableState == FOCUSABLE_AUTO) { |
| 269 |
|
| 270 |
super.setFocusable(NOT_FOCUSABLE); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public interface OnItemClickListener { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
void onItemClick(AdapterView<?> parent, View view, int position, long id); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void setOnItemClickListener(@Nullable OnItemClickListener listener) { |
| 303 |
mOnItemClickListener = listener; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
@Nullable |
| 311 |
public final OnItemClickListener getOnItemClickListener() { |
| 312 |
return mOnItemClickListener; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public boolean performItemClick(View view, int position, long id) { |
| 327 |
final boolean result; |
| 328 |
if (mOnItemClickListener != null) { |
| 329 |
playSoundEffect(SoundEffectConstants.CLICK); |
| 330 |
mOnItemClickListener.onItemClick(this, view, position, id); |
| 331 |
result = true; |
| 332 |
} else { |
| 333 |
result = false; |
| 334 |
} |
| 335 |
|
| 336 |
if (view != null) { |
| 337 |
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 338 |
} |
| 339 |
return result; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public interface OnItemLongClickListener { |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public void setOnItemLongClickListener(OnItemLongClickListener listener) { |
| 372 |
if (!isLongClickable()) { |
| 373 |
setLongClickable(true); |
| 374 |
} |
| 375 |
mOnItemLongClickListener = listener; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public final OnItemLongClickListener getOnItemLongClickListener() { |
| 383 |
return mOnItemLongClickListener; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public interface OnItemSelectedListener { |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
void onItemSelected(AdapterView<?> parent, View view, int position, long id); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
void onNothingSelected(AdapterView<?> parent); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) { |
| 425 |
mOnItemSelectedListener = listener; |
| 426 |
} |
| 427 |
|
| 428 |
@Nullable |
| 429 |
public final OnItemSelectedListener getOnItemSelectedListener() { |
| 430 |
return mOnItemSelectedListener; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public static class AdapterContextMenuInfo implements ContextMenu.ContextMenuInfo { |
| 440 |
|
| 441 |
public AdapterContextMenuInfo(View targetView, int position, long id) { |
| 442 |
this.targetView = targetView; |
| 443 |
this.position = position; |
| 444 |
this.id = id; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public View targetView; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public int position; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public long id; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public abstract T getAdapter(); |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public abstract void setAdapter(T adapter); |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
@Override |
| 488 |
public void addView(View child) { |
| 489 |
throw new UnsupportedOperationException("addView(View) is not supported in AdapterView"); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
@Override |
| 501 |
public void addView(View child, int index) { |
| 502 |
throw new UnsupportedOperationException("addView(View, int) is not supported in AdapterView"); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
@Override |
| 514 |
public void addView(View child, LayoutParams params) { |
| 515 |
throw new UnsupportedOperationException("addView(View, LayoutParams) " |
| 516 |
+ "is not supported in AdapterView"); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
@Override |
| 529 |
public void addView(View child, int index, LayoutParams params) { |
| 530 |
throw new UnsupportedOperationException("addView(View, int, LayoutParams) " |
| 531 |
+ "is not supported in AdapterView"); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
@Override |
| 542 |
public void removeView(View child) { |
| 543 |
throw new UnsupportedOperationException("removeView(View) is not supported in AdapterView"); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
@Override |
| 554 |
public void removeViewAt(int index) { |
| 555 |
throw new UnsupportedOperationException("removeViewAt(int) is not supported in AdapterView"); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
@Override |
| 564 |
public void removeAllViews() { |
| 565 |
throw new UnsupportedOperationException("removeAllViews() is not supported in AdapterView"); |
| 566 |
} |
| 567 |
|
| 568 |
@Override |
| 569 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 570 |
mLayoutHeight = getHeight(); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
@ViewDebug.CapturedViewProperty |
| 579 |
public int getSelectedItemPosition() { |
| 580 |
return mNextSelectedPosition; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
@ViewDebug.CapturedViewProperty |
| 588 |
public long getSelectedItemId() { |
| 589 |
return mNextSelectedRowId; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public abstract View getSelectedView(); |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public Object getSelectedItem() { |
| 603 |
T adapter = getAdapter(); |
| 604 |
int selection = getSelectedItemPosition(); |
| 605 |
if (adapter != null && adapter.getCount() > 0 && selection >= 0) { |
| 606 |
return adapter.getItem(selection); |
| 607 |
} else { |
| 608 |
return null; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
@ViewDebug.CapturedViewProperty |
| 618 |
public int getCount() { |
| 619 |
return mItemCount; |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public int getPositionForView(View view) { |
| 639 |
View listItem = view; |
| 640 |
try { |
| 641 |
View v; |
| 642 |
while ((v = (View) listItem.getParent()) != null && !v.equals(this)) { |
| 643 |
listItem = v; |
| 644 |
} |
| 645 |
} catch (ClassCastException e) { |
| 646 |
|
| 647 |
return INVALID_POSITION; |
| 648 |
} |
| 649 |
|
| 650 |
if (listItem != null) { |
| 651 |
|
| 652 |
final int childCount = getChildCount(); |
| 653 |
for (int i = 0; i < childCount; i++) { |
| 654 |
if (getChildAt(i).equals(listItem)) { |
| 655 |
return mFirstPosition + i; |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
return INVALID_POSITION; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public int getFirstVisiblePosition() { |
| 671 |
return mFirstPosition; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
public int getLastVisiblePosition() { |
| 681 |
return mFirstPosition + getChildCount() - 1; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public abstract void setSelection(int position); |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
@android.view.RemotableViewMethod |
| 696 |
public void setEmptyView(View emptyView) { |
| 697 |
mEmptyView = emptyView; |
| 698 |
|
| 699 |
|
| 700 |
if (emptyView != null |
| 701 |
&& emptyView.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 702 |
emptyView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 703 |
} |
| 704 |
|
| 705 |
final T adapter = getAdapter(); |
| 706 |
final boolean empty = ((adapter == null) || adapter.isEmpty()); |
| 707 |
updateEmptyStatus(empty); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
public View getEmptyView() { |
| 718 |
return mEmptyView; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
boolean isInFilterMode() { |
| 728 |
return false; |
| 729 |
} |
| 730 |
|
| 731 |
@Override |
| 732 |
public void setFocusable(@Focusable int focusable) { |
| 733 |
final T adapter = getAdapter(); |
| 734 |
final boolean empty = adapter == null || adapter.getCount() == 0; |
| 735 |
|
| 736 |
mDesiredFocusableState = focusable; |
| 737 |
if ((focusable & (FOCUSABLE_AUTO | FOCUSABLE)) == 0) { |
| 738 |
mDesiredFocusableInTouchModeState = false; |
| 739 |
} |
| 740 |
|
| 741 |
super.setFocusable((!empty || isInFilterMode()) ? focusable : NOT_FOCUSABLE); |
| 742 |
} |
| 743 |
|
| 744 |
@Override |
| 745 |
public void setFocusableInTouchMode(boolean focusable) { |
| 746 |
final T adapter = getAdapter(); |
| 747 |
final boolean empty = adapter == null || adapter.getCount() == 0; |
| 748 |
|
| 749 |
mDesiredFocusableInTouchModeState = focusable; |
| 750 |
if (focusable) { |
| 751 |
mDesiredFocusableState = FOCUSABLE; |
| 752 |
} |
| 753 |
|
| 754 |
super.setFocusableInTouchMode(focusable && (!empty || isInFilterMode())); |
| 755 |
} |
| 756 |
|
| 757 |
void checkFocus() { |
| 758 |
final T adapter = getAdapter(); |
| 759 |
final boolean empty = adapter == null || adapter.getCount() == 0; |
| 760 |
final boolean focusable = !empty || isInFilterMode(); |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
super.setFocusableInTouchMode(focusable && mDesiredFocusableInTouchModeState); |
| 765 |
super.setFocusable(focusable ? mDesiredFocusableState : NOT_FOCUSABLE); |
| 766 |
if (mEmptyView != null) { |
| 767 |
updateEmptyStatus((adapter == null) || adapter.isEmpty()); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
private void updateEmptyStatus(boolean empty) { |
| 777 |
if (isInFilterMode()) { |
| 778 |
empty = false; |
| 779 |
} |
| 780 |
|
| 781 |
if (empty) { |
| 782 |
if (mEmptyView != null) { |
| 783 |
mEmptyView.setVisibility(View.VISIBLE); |
| 784 |
setVisibility(View.GONE); |
| 785 |
} else { |
| 786 |
|
| 787 |
setVisibility(View.VISIBLE); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
if (mDataChanged) { |
| 794 |
this.onLayout(false, mLeft, mTop, mRight, mBottom); |
| 795 |
} |
| 796 |
} else { |
| 797 |
if (mEmptyView != null) mEmptyView.setVisibility(View.GONE); |
| 798 |
setVisibility(View.VISIBLE); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
public Object getItemAtPosition(int position) { |
| 809 |
T adapter = getAdapter(); |
| 810 |
return (adapter == null || position < 0) ? null : adapter.getItem(position); |
| 811 |
} |
| 812 |
|
| 813 |
public long getItemIdAtPosition(int position) { |
| 814 |
T adapter = getAdapter(); |
| 815 |
return (adapter == null || position < 0) ? INVALID_ROW_ID : adapter.getItemId(position); |
| 816 |
} |
| 817 |
|
| 818 |
@Override |
| 819 |
public void setOnClickListener(OnClickListener l) { |
| 820 |
throw new RuntimeException("Don't call setOnClickListener for an AdapterView. " |
| 821 |
+ "You probably want setOnItemClickListener instead"); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
@Override |
| 828 |
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 829 |
dispatchFreezeSelfOnly(container); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
@Override |
| 836 |
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 837 |
dispatchThawSelfOnly(container); |
| 838 |
} |
| 839 |
|
| 840 |
class AdapterDataSetObserver extends DataSetObserver { |
| 841 |
|
| 842 |
private Parcelable mInstanceState = null; |
| 843 |
|
| 844 |
@Override |
| 845 |
public void onChanged() { |
| 846 |
mDataChanged = true; |
| 847 |
mOldItemCount = mItemCount; |
| 848 |
mItemCount = getAdapter().getCount(); |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
if (AdapterView.this.getAdapter().hasStableIds() && mInstanceState != null |
| 853 |
&& mOldItemCount == 0 && mItemCount > 0) { |
| 854 |
AdapterView.this.onRestoreInstanceState(mInstanceState); |
| 855 |
mInstanceState = null; |
| 856 |
} else { |
| 857 |
rememberSyncState(); |
| 858 |
} |
| 859 |
checkFocus(); |
| 860 |
requestLayout(); |
| 861 |
} |
| 862 |
|
| 863 |
@Override |
| 864 |
public void onInvalidated() { |
| 865 |
mDataChanged = true; |
| 866 |
|
| 867 |
if (AdapterView.this.getAdapter().hasStableIds()) { |
| 868 |
|
| 869 |
|
| 870 |
mInstanceState = AdapterView.this.onSaveInstanceState(); |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
mOldItemCount = mItemCount; |
| 875 |
mItemCount = 0; |
| 876 |
mSelectedPosition = INVALID_POSITION; |
| 877 |
mSelectedRowId = INVALID_ROW_ID; |
| 878 |
mNextSelectedPosition = INVALID_POSITION; |
| 879 |
mNextSelectedRowId = INVALID_ROW_ID; |
| 880 |
mNeedSync = false; |
| 881 |
|
| 882 |
checkFocus(); |
| 883 |
requestLayout(); |
| 884 |
} |
| 885 |
|
| 886 |
public void clearSavedState() { |
| 887 |
mInstanceState = null; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
@Override |
| 892 |
protected void onDetachedFromWindow() { |
| 893 |
super.onDetachedFromWindow(); |
| 894 |
removeCallbacks(mSelectionNotifier); |
| 895 |
} |
| 896 |
|
| 897 |
private class SelectionNotifier implements Runnable { |
| 898 |
public void run() { |
| 899 |
mPendingSelectionNotifier = null; |
| 900 |
|
| 901 |
if (mDataChanged && getViewRootImpl() != null |
| 902 |
&& getViewRootImpl().isLayoutRequested()) { |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
if (getAdapter() != null) { |
| 907 |
mPendingSelectionNotifier = this; |
| 908 |
} |
| 909 |
} else { |
| 910 |
dispatchOnItemSelected(); |
| 911 |
} |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
@UnsupportedAppUsage |
| 916 |
void selectionChanged() { |
| 917 |
|
| 918 |
|
| 919 |
mPendingSelectionNotifier = null; |
| 920 |
|
| 921 |
if (mOnItemSelectedListener != null |
| 922 |
|| AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 923 |
if (mInLayout || mBlockLayoutRequests) { |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
if (mSelectionNotifier == null) { |
| 929 |
mSelectionNotifier = new SelectionNotifier(); |
| 930 |
} else { |
| 931 |
removeCallbacks(mSelectionNotifier); |
| 932 |
} |
| 933 |
post(mSelectionNotifier); |
| 934 |
} else { |
| 935 |
dispatchOnItemSelected(); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
final AutofillManager afm = mContext.getSystemService(AutofillManager.class); |
| 940 |
if (afm != null) { |
| 941 |
afm.notifyValueChanged(this); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
private void dispatchOnItemSelected() { |
| 946 |
fireOnSelected(); |
| 947 |
performAccessibilityActionsOnSelected(); |
| 948 |
} |
| 949 |
|
| 950 |
private void fireOnSelected() { |
| 951 |
if (mOnItemSelectedListener == null) { |
| 952 |
return; |
| 953 |
} |
| 954 |
final int selection = getSelectedItemPosition(); |
| 955 |
if (selection >= 0) { |
| 956 |
View v = getSelectedView(); |
| 957 |
mOnItemSelectedListener.onItemSelected(this, v, selection, |
| 958 |
getAdapter().getItemId(selection)); |
| 959 |
} else { |
| 960 |
mOnItemSelectedListener.onNothingSelected(this); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
private void performAccessibilityActionsOnSelected() { |
| 965 |
if (!AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 966 |
return; |
| 967 |
} |
| 968 |
final int position = getSelectedItemPosition(); |
| 969 |
if (position >= 0) { |
| 970 |
|
| 971 |
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
@Override |
| 977 |
public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 978 |
View selectedView = getSelectedView(); |
| 979 |
if (selectedView != null && selectedView.getVisibility() == VISIBLE |
| 980 |
&& selectedView.dispatchPopulateAccessibilityEvent(event)) { |
| 981 |
return true; |
| 982 |
} |
| 983 |
return false; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
@Override |
| 988 |
public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
| 989 |
if (super.onRequestSendAccessibilityEventInternal(child, event)) { |
| 990 |
|
| 991 |
AccessibilityEvent record = AccessibilityEvent.obtain(); |
| 992 |
onInitializeAccessibilityEvent(record); |
| 993 |
|
| 994 |
child.dispatchPopulateAccessibilityEvent(record); |
| 995 |
event.appendRecord(record); |
| 996 |
return true; |
| 997 |
} |
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
@Override |
| 1002 |
public CharSequence getAccessibilityClassName() { |
| 1003 |
return AdapterView.class.getName(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
@Override |
| 1008 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 1009 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 1010 |
info.setScrollable(isScrollableForAccessibility()); |
| 1011 |
View selectedView = getSelectedView(); |
| 1012 |
if (selectedView != null) { |
| 1013 |
info.setEnabled(selectedView.isEnabled()); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
@Override |
| 1019 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 1020 |
super.onInitializeAccessibilityEventInternal(event); |
| 1021 |
event.setScrollable(isScrollableForAccessibility()); |
| 1022 |
View selectedView = getSelectedView(); |
| 1023 |
if (selectedView != null) { |
| 1024 |
event.setEnabled(selectedView.isEnabled()); |
| 1025 |
} |
| 1026 |
event.setCurrentItemIndex(getSelectedItemPosition()); |
| 1027 |
event.setFromIndex(getFirstVisiblePosition()); |
| 1028 |
event.setToIndex(getLastVisiblePosition()); |
| 1029 |
event.setItemCount(getCount()); |
| 1030 |
} |
| 1031 |
|
| 1032 |
private boolean isScrollableForAccessibility() { |
| 1033 |
T adapter = getAdapter(); |
| 1034 |
if (adapter != null) { |
| 1035 |
final int itemCount = adapter.getCount(); |
| 1036 |
return itemCount > 0 |
| 1037 |
&& (getFirstVisiblePosition() > 0 || getLastVisiblePosition() < itemCount - 1); |
| 1038 |
} |
| 1039 |
return false; |
| 1040 |
} |
| 1041 |
|
| 1042 |
@Override |
| 1043 |
protected boolean canAnimate() { |
| 1044 |
return super.canAnimate() && mItemCount > 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
void handleDataChanged() { |
| 1048 |
final int count = mItemCount; |
| 1049 |
boolean found = false; |
| 1050 |
|
| 1051 |
if (count > 0) { |
| 1052 |
|
| 1053 |
int newPos; |
| 1054 |
|
| 1055 |
|
| 1056 |
if (mNeedSync) { |
| 1057 |
|
| 1058 |
|
| 1059 |
mNeedSync = false; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
newPos = findSyncPosition(); |
| 1064 |
if (newPos >= 0) { |
| 1065 |
|
| 1066 |
int selectablePos = lookForSelectablePosition(newPos, true); |
| 1067 |
if (selectablePos == newPos) { |
| 1068 |
|
| 1069 |
setNextSelectedPositionInt(newPos); |
| 1070 |
found = true; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
if (!found) { |
| 1075 |
|
| 1076 |
newPos = getSelectedItemPosition(); |
| 1077 |
|
| 1078 |
|
| 1079 |
if (newPos >= count) { |
| 1080 |
newPos = count - 1; |
| 1081 |
} |
| 1082 |
if (newPos < 0) { |
| 1083 |
newPos = 0; |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
int selectablePos = lookForSelectablePosition(newPos, true); |
| 1088 |
if (selectablePos < 0) { |
| 1089 |
|
| 1090 |
selectablePos = lookForSelectablePosition(newPos, false); |
| 1091 |
} |
| 1092 |
if (selectablePos >= 0) { |
| 1093 |
setNextSelectedPositionInt(selectablePos); |
| 1094 |
checkSelectionChanged(); |
| 1095 |
found = true; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
if (!found) { |
| 1100 |
|
| 1101 |
mSelectedPosition = INVALID_POSITION; |
| 1102 |
mSelectedRowId = INVALID_ROW_ID; |
| 1103 |
mNextSelectedPosition = INVALID_POSITION; |
| 1104 |
mNextSelectedRowId = INVALID_ROW_ID; |
| 1105 |
mNeedSync = false; |
| 1106 |
checkSelectionChanged(); |
| 1107 |
} |
| 1108 |
|
| 1109 |
notifySubtreeAccessibilityStateChangedIfNeeded(); |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
void checkSelectionChanged() { |
| 1117 |
if ((mSelectedPosition != mOldSelectedPosition) || (mSelectedRowId != mOldSelectedRowId)) { |
| 1118 |
selectionChanged(); |
| 1119 |
mOldSelectedPosition = mSelectedPosition; |
| 1120 |
mOldSelectedRowId = mSelectedRowId; |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
if (mPendingSelectionNotifier != null) { |
| 1126 |
mPendingSelectionNotifier.run(); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
int findSyncPosition() { |
| 1139 |
int count = mItemCount; |
| 1140 |
|
| 1141 |
if (count == 0) { |
| 1142 |
return INVALID_POSITION; |
| 1143 |
} |
| 1144 |
|
| 1145 |
long idToMatch = mSyncRowId; |
| 1146 |
int seed = mSyncPosition; |
| 1147 |
|
| 1148 |
|
| 1149 |
if (idToMatch == INVALID_ROW_ID) { |
| 1150 |
return INVALID_POSITION; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
seed = Math.max(0, seed); |
| 1155 |
seed = Math.min(count - 1, seed); |
| 1156 |
|
| 1157 |
long endTime = SystemClock.uptimeMillis() + SYNC_MAX_DURATION_MILLIS; |
| 1158 |
|
| 1159 |
long rowId; |
| 1160 |
|
| 1161 |
|
| 1162 |
int first = seed; |
| 1163 |
|
| 1164 |
|
| 1165 |
int last = seed; |
| 1166 |
|
| 1167 |
|
| 1168 |
boolean next = false; |
| 1169 |
|
| 1170 |
|
| 1171 |
boolean hitFirst; |
| 1172 |
|
| 1173 |
|
| 1174 |
boolean hitLast; |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
T adapter = getAdapter(); |
| 1179 |
if (adapter == null) { |
| 1180 |
return INVALID_POSITION; |
| 1181 |
} |
| 1182 |
|
| 1183 |
while (SystemClock.uptimeMillis() <= endTime) { |
| 1184 |
rowId = adapter.getItemId(seed); |
| 1185 |
if (rowId == idToMatch) { |
| 1186 |
|
| 1187 |
return seed; |
| 1188 |
} |
| 1189 |
|
| 1190 |
hitLast = last == count - 1; |
| 1191 |
hitFirst = first == 0; |
| 1192 |
|
| 1193 |
if (hitLast && hitFirst) { |
| 1194 |
|
| 1195 |
break; |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (hitFirst || (next && !hitLast)) { |
| 1199 |
|
| 1200 |
last++; |
| 1201 |
seed = last; |
| 1202 |
|
| 1203 |
next = false; |
| 1204 |
} else if (hitLast || (!next && !hitFirst)) { |
| 1205 |
|
| 1206 |
first--; |
| 1207 |
seed = first; |
| 1208 |
|
| 1209 |
next = true; |
| 1210 |
} |
| 1211 |
|
| 1212 |
} |
| 1213 |
|
| 1214 |
return INVALID_POSITION; |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
int lookForSelectablePosition(int position, boolean lookDown) { |
| 1226 |
return position; |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
@UnsupportedAppUsage |
| 1234 |
void setSelectedPositionInt(int position) { |
| 1235 |
mSelectedPosition = position; |
| 1236 |
mSelectedRowId = getItemIdAtPosition(position); |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
@UnsupportedAppUsage |
| 1245 |
void setNextSelectedPositionInt(int position) { |
| 1246 |
mNextSelectedPosition = position; |
| 1247 |
mNextSelectedRowId = getItemIdAtPosition(position); |
| 1248 |
|
| 1249 |
if (mNeedSync && mSyncMode == SYNC_SELECTED_POSITION && position >= 0) { |
| 1250 |
mSyncPosition = position; |
| 1251 |
mSyncRowId = mNextSelectedRowId; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
void rememberSyncState() { |
| 1261 |
if (getChildCount() > 0) { |
| 1262 |
mNeedSync = true; |
| 1263 |
mSyncHeight = mLayoutHeight; |
| 1264 |
if (mSelectedPosition >= 0) { |
| 1265 |
|
| 1266 |
View v = getChildAt(mSelectedPosition - mFirstPosition); |
| 1267 |
mSyncRowId = mNextSelectedRowId; |
| 1268 |
mSyncPosition = mNextSelectedPosition; |
| 1269 |
if (v != null) { |
| 1270 |
mSpecificTop = v.getTop(); |
| 1271 |
} |
| 1272 |
mSyncMode = SYNC_SELECTED_POSITION; |
| 1273 |
} else { |
| 1274 |
|
| 1275 |
View v = getChildAt(0); |
| 1276 |
T adapter = getAdapter(); |
| 1277 |
if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) { |
| 1278 |
mSyncRowId = adapter.getItemId(mFirstPosition); |
| 1279 |
} else { |
| 1280 |
mSyncRowId = NO_ID; |
| 1281 |
} |
| 1282 |
mSyncPosition = mFirstPosition; |
| 1283 |
if (v != null) { |
| 1284 |
mSpecificTop = v.getTop(); |
| 1285 |
} |
| 1286 |
mSyncMode = SYNC_FIRST_POSITION; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
@Override |
| 1293 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 1294 |
super.encodeProperties(encoder); |
| 1295 |
|
| 1296 |
encoder.addProperty("scrolling:firstPosition", mFirstPosition); |
| 1297 |
encoder.addProperty("list:nextSelectedPosition", mNextSelectedPosition); |
| 1298 |
encoder.addProperty("list:nextSelectedRowId", mNextSelectedRowId); |
| 1299 |
encoder.addProperty("list:selectedPosition", mSelectedPosition); |
| 1300 |
encoder.addProperty("list:itemCount", mItemCount); |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
@Override |
| 1311 |
public void onProvideAutofillStructure(ViewStructure structure, int flags) { |
| 1312 |
super.onProvideAutofillStructure(structure, flags); |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
@Override |
| 1317 |
protected void onProvideStructure(@NonNull ViewStructure structure, |
| 1318 |
@ViewStructureType int viewFor, int flags) { |
| 1319 |
super.onProvideStructure(structure, viewFor, flags); |
| 1320 |
|
| 1321 |
if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL |
| 1322 |
|| viewFor == VIEW_STRUCTURE_FOR_CONTENT_CAPTURE) { |
| 1323 |
final Adapter adapter = getAdapter(); |
| 1324 |
if (adapter == null) return; |
| 1325 |
|
| 1326 |
final CharSequence[] options = adapter.getAutofillOptions(); |
| 1327 |
if (options != null) { |
| 1328 |
structure.setAutofillOptions(options); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
} |