| 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.IdRes; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.graphics.Canvas; |
| 27 |
import android.graphics.Paint; |
| 28 |
import android.graphics.PixelFormat; |
| 29 |
import android.graphics.Rect; |
| 30 |
import android.graphics.drawable.Drawable; |
| 31 |
import android.os.Build; |
| 32 |
import android.os.Bundle; |
| 33 |
import android.os.Trace; |
| 34 |
import android.util.AttributeSet; |
| 35 |
import android.util.Log; |
| 36 |
import android.util.MathUtils; |
| 37 |
import android.util.SparseBooleanArray; |
| 38 |
import android.view.FocusFinder; |
| 39 |
import android.view.KeyEvent; |
| 40 |
import android.view.SoundEffectConstants; |
| 41 |
import android.view.View; |
| 42 |
import android.view.ViewDebug; |
| 43 |
import android.view.ViewGroup; |
| 44 |
import android.view.ViewHierarchyEncoder; |
| 45 |
import android.view.ViewParent; |
| 46 |
import android.view.ViewRootImpl; |
| 47 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 48 |
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 49 |
import android.view.accessibility.AccessibilityNodeInfo.CollectionInfo; |
| 50 |
import android.view.accessibility.AccessibilityNodeInfo.CollectionItemInfo; |
| 51 |
import android.view.accessibility.AccessibilityNodeProvider; |
| 52 |
import android.view.inspector.InspectableProperty; |
| 53 |
import android.widget.RemoteViews.RemoteView; |
| 54 |
|
| 55 |
import com.android.internal.R; |
| 56 |
|
| 57 |
import com.google.android.collect.Lists; |
| 58 |
|
| 59 |
import java.util.ArrayList; |
| 60 |
import java.util.List; |
| 61 |
import java.util.function.Predicate; |
| 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 |
@RemoteView |
| 147 |
public class ListView extends AbsListView { |
| 148 |
static final String TAG = "ListView"; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static final int NO_POSITION = -1; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private static final float MAX_SCROLL_FACTOR = 0.33f; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private static final int MIN_SCROLL_PREVIEW_PIXELS = 2; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public class FixedViewInfo { |
| 173 |
|
| 174 |
public View view; |
| 175 |
|
| 176 |
public Object data; |
| 177 |
|
| 178 |
public boolean isSelectable; |
| 179 |
} |
| 180 |
|
| 181 |
@UnsupportedAppUsage |
| 182 |
ArrayList<FixedViewInfo> mHeaderViewInfos = Lists.newArrayList(); |
| 183 |
@UnsupportedAppUsage |
| 184 |
ArrayList<FixedViewInfo> mFooterViewInfos = Lists.newArrayList(); |
| 185 |
|
| 186 |
@UnsupportedAppUsage |
| 187 |
Drawable mDivider; |
| 188 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 189 |
int mDividerHeight; |
| 190 |
|
| 191 |
Drawable mOverScrollHeader; |
| 192 |
Drawable mOverScrollFooter; |
| 193 |
|
| 194 |
private boolean mIsCacheColorOpaque; |
| 195 |
private boolean mDividerIsOpaque; |
| 196 |
|
| 197 |
private boolean mHeaderDividersEnabled; |
| 198 |
private boolean mFooterDividersEnabled; |
| 199 |
|
| 200 |
@UnsupportedAppUsage |
| 201 |
private boolean mAreAllItemsSelectable = true; |
| 202 |
|
| 203 |
private boolean mItemsCanFocus = false; |
| 204 |
|
| 205 |
|
| 206 |
private final Rect mTempRect = new Rect(); |
| 207 |
private Paint mDividerPaint; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private final ArrowScrollFocusResult mArrowScrollFocusResult = new ArrowScrollFocusResult(); |
| 212 |
|
| 213 |
|
| 214 |
private FocusSelector mFocusSelector; |
| 215 |
|
| 216 |
public ListView(Context context) { |
| 217 |
this(context, null); |
| 218 |
} |
| 219 |
|
| 220 |
public ListView(Context context, AttributeSet attrs) { |
| 221 |
this(context, attrs, R.attr.listViewStyle); |
| 222 |
} |
| 223 |
|
| 224 |
public ListView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 225 |
this(context, attrs, defStyleAttr, 0); |
| 226 |
} |
| 227 |
|
| 228 |
public ListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 229 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 230 |
|
| 231 |
final TypedArray a = context.obtainStyledAttributes( |
| 232 |
attrs, R.styleable.ListView, defStyleAttr, defStyleRes); |
| 233 |
saveAttributeDataForStyleable(context, R.styleable.ListView, |
| 234 |
attrs, a, defStyleAttr, defStyleRes); |
| 235 |
|
| 236 |
final CharSequence[] entries = a.getTextArray(R.styleable.ListView_entries); |
| 237 |
if (entries != null) { |
| 238 |
setAdapter(new ArrayAdapter<>(context, R.layout.simple_list_item_1, entries)); |
| 239 |
} |
| 240 |
|
| 241 |
final Drawable d = a.getDrawable(R.styleable.ListView_divider); |
| 242 |
if (d != null) { |
| 243 |
|
| 244 |
|
| 245 |
setDivider(d); |
| 246 |
} |
| 247 |
|
| 248 |
final Drawable osHeader = a.getDrawable(R.styleable.ListView_overScrollHeader); |
| 249 |
if (osHeader != null) { |
| 250 |
setOverscrollHeader(osHeader); |
| 251 |
} |
| 252 |
|
| 253 |
final Drawable osFooter = a.getDrawable(R.styleable.ListView_overScrollFooter); |
| 254 |
if (osFooter != null) { |
| 255 |
setOverscrollFooter(osFooter); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
if (a.hasValueOrEmpty(R.styleable.ListView_dividerHeight)) { |
| 260 |
final int dividerHeight = a.getDimensionPixelSize( |
| 261 |
R.styleable.ListView_dividerHeight, 0); |
| 262 |
if (dividerHeight != 0) { |
| 263 |
setDividerHeight(dividerHeight); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
mHeaderDividersEnabled = a.getBoolean(R.styleable.ListView_headerDividersEnabled, true); |
| 268 |
mFooterDividersEnabled = a.getBoolean(R.styleable.ListView_footerDividersEnabled, true); |
| 269 |
|
| 270 |
a.recycle(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public int getMaxScrollAmount() { |
| 278 |
return (int) (MAX_SCROLL_FACTOR * (mBottom - mTop)); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private void adjustViewsUpOrDown() { |
| 286 |
final int childCount = getChildCount(); |
| 287 |
int delta; |
| 288 |
|
| 289 |
if (childCount > 0) { |
| 290 |
View child; |
| 291 |
|
| 292 |
if (!mStackFromBottom) { |
| 293 |
|
| 294 |
|
| 295 |
child = getChildAt(0); |
| 296 |
delta = child.getTop() - mListPadding.top; |
| 297 |
if (mFirstPosition != 0) { |
| 298 |
|
| 299 |
|
| 300 |
delta -= mDividerHeight; |
| 301 |
} |
| 302 |
if (delta < 0) { |
| 303 |
|
| 304 |
delta = 0; |
| 305 |
} |
| 306 |
} else { |
| 307 |
|
| 308 |
child = getChildAt(childCount - 1); |
| 309 |
delta = child.getBottom() - (getHeight() - mListPadding.bottom); |
| 310 |
|
| 311 |
if (mFirstPosition + childCount < mItemCount) { |
| 312 |
|
| 313 |
|
| 314 |
delta += mDividerHeight; |
| 315 |
} |
| 316 |
|
| 317 |
if (delta > 0) { |
| 318 |
delta = 0; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
if (delta != 0) { |
| 323 |
offsetChildrenTopAndBottom(-delta); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public void addHeaderView(View v, Object data, boolean isSelectable) { |
| 345 |
if (v.getParent() != null && v.getParent() != this) { |
| 346 |
if (Log.isLoggable(TAG, Log.WARN)) { |
| 347 |
Log.w(TAG, "The specified child already has a parent. " |
| 348 |
+ "You must call removeView() on the child's parent first."); |
| 349 |
} |
| 350 |
} |
| 351 |
final FixedViewInfo info = new FixedViewInfo(); |
| 352 |
info.view = v; |
| 353 |
info.data = data; |
| 354 |
info.isSelectable = isSelectable; |
| 355 |
mHeaderViewInfos.add(info); |
| 356 |
mAreAllItemsSelectable &= isSelectable; |
| 357 |
|
| 358 |
|
| 359 |
if (mAdapter != null) { |
| 360 |
if (!(mAdapter instanceof HeaderViewListAdapter)) { |
| 361 |
wrapHeaderListAdapterInternal(); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if (mDataSetObserver != null) { |
| 367 |
mDataSetObserver.onChanged(); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public void addHeaderView(View v) { |
| 387 |
addHeaderView(v, null, true); |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public int getHeaderViewsCount() { |
| 392 |
return mHeaderViewInfos.size(); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public boolean removeHeaderView(View v) { |
| 403 |
if (mHeaderViewInfos.size() > 0) { |
| 404 |
boolean result = false; |
| 405 |
if (mAdapter != null && ((HeaderViewListAdapter) mAdapter).removeHeader(v)) { |
| 406 |
if (mDataSetObserver != null) { |
| 407 |
mDataSetObserver.onChanged(); |
| 408 |
} |
| 409 |
result = true; |
| 410 |
} |
| 411 |
removeFixedViewInfo(v, mHeaderViewInfos); |
| 412 |
return result; |
| 413 |
} |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
private void removeFixedViewInfo(View v, ArrayList<FixedViewInfo> where) { |
| 418 |
int len = where.size(); |
| 419 |
for (int i = 0; i < len; ++i) { |
| 420 |
FixedViewInfo info = where.get(i); |
| 421 |
if (info.view == v) { |
| 422 |
where.remove(i); |
| 423 |
break; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public void addFooterView(View v, Object data, boolean isSelectable) { |
| 445 |
if (v.getParent() != null && v.getParent() != this) { |
| 446 |
if (Log.isLoggable(TAG, Log.WARN)) { |
| 447 |
Log.w(TAG, "The specified child already has a parent. " |
| 448 |
+ "You must call removeView() on the child's parent first."); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
final FixedViewInfo info = new FixedViewInfo(); |
| 453 |
info.view = v; |
| 454 |
info.data = data; |
| 455 |
info.isSelectable = isSelectable; |
| 456 |
mFooterViewInfos.add(info); |
| 457 |
mAreAllItemsSelectable &= isSelectable; |
| 458 |
|
| 459 |
|
| 460 |
if (mAdapter != null) { |
| 461 |
if (!(mAdapter instanceof HeaderViewListAdapter)) { |
| 462 |
wrapHeaderListAdapterInternal(); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
if (mDataSetObserver != null) { |
| 468 |
mDataSetObserver.onChanged(); |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public void addFooterView(View v) { |
| 488 |
addFooterView(v, null, true); |
| 489 |
} |
| 490 |
|
| 491 |
@Override |
| 492 |
public int getFooterViewsCount() { |
| 493 |
return mFooterViewInfos.size(); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public boolean removeFooterView(View v) { |
| 504 |
if (mFooterViewInfos.size() > 0) { |
| 505 |
boolean result = false; |
| 506 |
if (mAdapter != null && ((HeaderViewListAdapter) mAdapter).removeFooter(v)) { |
| 507 |
if (mDataSetObserver != null) { |
| 508 |
mDataSetObserver.onChanged(); |
| 509 |
} |
| 510 |
result = true; |
| 511 |
} |
| 512 |
removeFixedViewInfo(v, mFooterViewInfos); |
| 513 |
return result; |
| 514 |
} |
| 515 |
return false; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
@Override |
| 528 |
public ListAdapter getAdapter() { |
| 529 |
return mAdapter; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
@android.view.RemotableViewMethod(asyncImpl="setRemoteViewsAdapterAsync") |
| 538 |
public void setRemoteViewsAdapter(Intent intent) { |
| 539 |
super.setRemoteViewsAdapter(intent); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
@Override |
| 556 |
public void setAdapter(ListAdapter adapter) { |
| 557 |
if (mAdapter != null && mDataSetObserver != null) { |
| 558 |
mAdapter.unregisterDataSetObserver(mDataSetObserver); |
| 559 |
} |
| 560 |
|
| 561 |
resetList(); |
| 562 |
mRecycler.clear(); |
| 563 |
|
| 564 |
if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) { |
| 565 |
mAdapter = wrapHeaderListAdapterInternal(mHeaderViewInfos, mFooterViewInfos, adapter); |
| 566 |
} else { |
| 567 |
mAdapter = adapter; |
| 568 |
} |
| 569 |
|
| 570 |
mOldSelectedPosition = INVALID_POSITION; |
| 571 |
mOldSelectedRowId = INVALID_ROW_ID; |
| 572 |
|
| 573 |
|
| 574 |
super.setAdapter(adapter); |
| 575 |
|
| 576 |
if (mAdapter != null) { |
| 577 |
mAreAllItemsSelectable = mAdapter.areAllItemsEnabled(); |
| 578 |
mOldItemCount = mItemCount; |
| 579 |
mItemCount = mAdapter.getCount(); |
| 580 |
checkFocus(); |
| 581 |
|
| 582 |
mDataSetObserver = new AdapterDataSetObserver(); |
| 583 |
mAdapter.registerDataSetObserver(mDataSetObserver); |
| 584 |
|
| 585 |
mRecycler.setViewTypeCount(mAdapter.getViewTypeCount()); |
| 586 |
|
| 587 |
int position; |
| 588 |
if (mStackFromBottom) { |
| 589 |
position = lookForSelectablePosition(mItemCount - 1, false); |
| 590 |
} else { |
| 591 |
position = lookForSelectablePosition(0, true); |
| 592 |
} |
| 593 |
setSelectedPositionInt(position); |
| 594 |
setNextSelectedPositionInt(position); |
| 595 |
|
| 596 |
if (mItemCount == 0) { |
| 597 |
|
| 598 |
checkSelectionChanged(); |
| 599 |
} |
| 600 |
} else { |
| 601 |
mAreAllItemsSelectable = true; |
| 602 |
checkFocus(); |
| 603 |
|
| 604 |
checkSelectionChanged(); |
| 605 |
} |
| 606 |
|
| 607 |
requestLayout(); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
@Override |
| 614 |
void resetList() { |
| 615 |
|
| 616 |
|
| 617 |
clearRecycledState(mHeaderViewInfos); |
| 618 |
clearRecycledState(mFooterViewInfos); |
| 619 |
|
| 620 |
super.resetList(); |
| 621 |
|
| 622 |
mLayoutMode = LAYOUT_NORMAL; |
| 623 |
} |
| 624 |
|
| 625 |
private void clearRecycledState(ArrayList<FixedViewInfo> infos) { |
| 626 |
if (infos != null) { |
| 627 |
final int count = infos.size(); |
| 628 |
|
| 629 |
for (int i = 0; i < count; i++) { |
| 630 |
final View child = infos.get(i).view; |
| 631 |
final ViewGroup.LayoutParams params = child.getLayoutParams(); |
| 632 |
if (checkLayoutParams(params)) { |
| 633 |
((LayoutParams) params).recycledHeaderFooter = false; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
private boolean showingTopFadingEdge() { |
| 643 |
final int listTop = mScrollY + mListPadding.top; |
| 644 |
return (mFirstPosition > 0) || (getChildAt(0).getTop() > listTop); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
private boolean showingBottomFadingEdge() { |
| 651 |
final int childCount = getChildCount(); |
| 652 |
final int bottomOfBottomChild = getChildAt(childCount - 1).getBottom(); |
| 653 |
final int lastVisiblePosition = mFirstPosition + childCount - 1; |
| 654 |
|
| 655 |
final int listBottom = mScrollY + getHeight() - mListPadding.bottom; |
| 656 |
|
| 657 |
return (lastVisiblePosition < mItemCount - 1) |
| 658 |
|| (bottomOfBottomChild < listBottom); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
@Override |
| 663 |
public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) { |
| 664 |
|
| 665 |
int rectTopWithinChild = rect.top; |
| 666 |
|
| 667 |
|
| 668 |
rect.offset(child.getLeft(), child.getTop()); |
| 669 |
rect.offset(-child.getScrollX(), -child.getScrollY()); |
| 670 |
|
| 671 |
final int height = getHeight(); |
| 672 |
int listUnfadedTop = getScrollY(); |
| 673 |
int listUnfadedBottom = listUnfadedTop + height; |
| 674 |
final int fadingEdge = getVerticalFadingEdgeLength(); |
| 675 |
|
| 676 |
if (showingTopFadingEdge()) { |
| 677 |
|
| 678 |
if ((mSelectedPosition > 0) || (rectTopWithinChild > fadingEdge)) { |
| 679 |
listUnfadedTop += fadingEdge; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
int childCount = getChildCount(); |
| 684 |
int bottomOfBottomChild = getChildAt(childCount - 1).getBottom(); |
| 685 |
|
| 686 |
if (showingBottomFadingEdge()) { |
| 687 |
|
| 688 |
if ((mSelectedPosition < mItemCount - 1) |
| 689 |
|| (rect.bottom < (bottomOfBottomChild - fadingEdge))) { |
| 690 |
listUnfadedBottom -= fadingEdge; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
int scrollYDelta = 0; |
| 695 |
|
| 696 |
if (rect.bottom > listUnfadedBottom && rect.top > listUnfadedTop) { |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
if (rect.height() > height) { |
| 702 |
|
| 703 |
scrollYDelta += (rect.top - listUnfadedTop); |
| 704 |
} else { |
| 705 |
|
| 706 |
scrollYDelta += (rect.bottom - listUnfadedBottom); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
int distanceToBottom = bottomOfBottomChild - listUnfadedBottom; |
| 711 |
scrollYDelta = Math.min(scrollYDelta, distanceToBottom); |
| 712 |
} else if (rect.top < listUnfadedTop && rect.bottom < listUnfadedBottom) { |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
if (rect.height() > height) { |
| 718 |
|
| 719 |
scrollYDelta -= (listUnfadedBottom - rect.bottom); |
| 720 |
} else { |
| 721 |
|
| 722 |
scrollYDelta -= (listUnfadedTop - rect.top); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
int top = getChildAt(0).getTop(); |
| 727 |
int deltaToTop = top - listUnfadedTop; |
| 728 |
scrollYDelta = Math.max(scrollYDelta, deltaToTop); |
| 729 |
} |
| 730 |
|
| 731 |
final boolean scroll = scrollYDelta != 0; |
| 732 |
if (scroll) { |
| 733 |
scrollListItemsBy(-scrollYDelta); |
| 734 |
positionSelector(INVALID_POSITION, child); |
| 735 |
mSelectedTop = child.getTop(); |
| 736 |
invalidate(); |
| 737 |
} |
| 738 |
return scroll; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
@Override |
| 745 |
void fillGap(boolean down) { |
| 746 |
final int count = getChildCount(); |
| 747 |
if (down) { |
| 748 |
int paddingTop = 0; |
| 749 |
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
| 750 |
paddingTop = getListPaddingTop(); |
| 751 |
} |
| 752 |
final int startOffset = count > 0 ? getChildAt(count - 1).getBottom() + mDividerHeight : |
| 753 |
paddingTop; |
| 754 |
fillDown(mFirstPosition + count, startOffset); |
| 755 |
correctTooHigh(getChildCount()); |
| 756 |
} else { |
| 757 |
int paddingBottom = 0; |
| 758 |
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
| 759 |
paddingBottom = getListPaddingBottom(); |
| 760 |
} |
| 761 |
final int startOffset = count > 0 ? getChildAt(0).getTop() - mDividerHeight : |
| 762 |
getHeight() - paddingBottom; |
| 763 |
fillUp(mFirstPosition - 1, startOffset); |
| 764 |
correctTooLow(getChildCount()); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 780 |
private View fillDown(int pos, int nextTop) { |
| 781 |
View selectedView = null; |
| 782 |
|
| 783 |
int end = (mBottom - mTop); |
| 784 |
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
| 785 |
end -= mListPadding.bottom; |
| 786 |
} |
| 787 |
|
| 788 |
while (nextTop < end && pos < mItemCount) { |
| 789 |
|
| 790 |
boolean selected = pos == mSelectedPosition; |
| 791 |
View child = makeAndAddView(pos, nextTop, true, mListPadding.left, selected); |
| 792 |
|
| 793 |
nextTop = child.getBottom() + mDividerHeight; |
| 794 |
if (selected) { |
| 795 |
selectedView = child; |
| 796 |
} |
| 797 |
pos++; |
| 798 |
} |
| 799 |
|
| 800 |
setVisibleRangeHint(mFirstPosition, mFirstPosition + getChildCount() - 1); |
| 801 |
return selectedView; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 815 |
private View fillUp(int pos, int nextBottom) { |
| 816 |
View selectedView = null; |
| 817 |
|
| 818 |
int end = 0; |
| 819 |
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
| 820 |
end = mListPadding.top; |
| 821 |
} |
| 822 |
|
| 823 |
while (nextBottom > end && pos >= 0) { |
| 824 |
|
| 825 |
boolean selected = pos == mSelectedPosition; |
| 826 |
View child = makeAndAddView(pos, nextBottom, false, mListPadding.left, selected); |
| 827 |
nextBottom = child.getTop() - mDividerHeight; |
| 828 |
if (selected) { |
| 829 |
selectedView = child; |
| 830 |
} |
| 831 |
pos--; |
| 832 |
} |
| 833 |
|
| 834 |
mFirstPosition = pos + 1; |
| 835 |
setVisibleRangeHint(mFirstPosition, mFirstPosition + getChildCount() - 1); |
| 836 |
return selectedView; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
private View fillFromTop(int nextTop) { |
| 848 |
mFirstPosition = Math.min(mFirstPosition, mSelectedPosition); |
| 849 |
mFirstPosition = Math.min(mFirstPosition, mItemCount - 1); |
| 850 |
if (mFirstPosition < 0) { |
| 851 |
mFirstPosition = 0; |
| 852 |
} |
| 853 |
return fillDown(mFirstPosition, nextTop); |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
private View fillFromMiddle(int childrenTop, int childrenBottom) { |
| 868 |
int height = childrenBottom - childrenTop; |
| 869 |
|
| 870 |
int position = reconcileSelectedPosition(); |
| 871 |
|
| 872 |
View sel = makeAndAddView(position, childrenTop, true, |
| 873 |
mListPadding.left, true); |
| 874 |
mFirstPosition = position; |
| 875 |
|
| 876 |
int selHeight = sel.getMeasuredHeight(); |
| 877 |
if (selHeight <= height) { |
| 878 |
sel.offsetTopAndBottom((height - selHeight) / 2); |
| 879 |
} |
| 880 |
|
| 881 |
fillAboveAndBelow(sel, position); |
| 882 |
|
| 883 |
if (!mStackFromBottom) { |
| 884 |
correctTooHigh(getChildCount()); |
| 885 |
} else { |
| 886 |
correctTooLow(getChildCount()); |
| 887 |
} |
| 888 |
|
| 889 |
return sel; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
private void fillAboveAndBelow(View sel, int position) { |
| 900 |
final int dividerHeight = mDividerHeight; |
| 901 |
if (!mStackFromBottom) { |
| 902 |
fillUp(position - 1, sel.getTop() - dividerHeight); |
| 903 |
adjustViewsUpOrDown(); |
| 904 |
fillDown(position + 1, sel.getBottom() + dividerHeight); |
| 905 |
} else { |
| 906 |
fillDown(position + 1, sel.getBottom() + dividerHeight); |
| 907 |
adjustViewsUpOrDown(); |
| 908 |
fillUp(position - 1, sel.getTop() - dividerHeight); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
private View fillFromSelection(int selectedTop, int childrenTop, int childrenBottom) { |
| 924 |
int fadingEdgeLength = getVerticalFadingEdgeLength(); |
| 925 |
final int selectedPosition = mSelectedPosition; |
| 926 |
|
| 927 |
View sel; |
| 928 |
|
| 929 |
final int topSelectionPixel = getTopSelectionPixel(childrenTop, fadingEdgeLength, |
| 930 |
selectedPosition); |
| 931 |
final int bottomSelectionPixel = getBottomSelectionPixel(childrenBottom, fadingEdgeLength, |
| 932 |
selectedPosition); |
| 933 |
|
| 934 |
sel = makeAndAddView(selectedPosition, selectedTop, true, mListPadding.left, true); |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
if (sel.getBottom() > bottomSelectionPixel) { |
| 939 |
|
| 940 |
|
| 941 |
final int spaceAbove = sel.getTop() - topSelectionPixel; |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
final int spaceBelow = sel.getBottom() - bottomSelectionPixel; |
| 946 |
final int offset = Math.min(spaceAbove, spaceBelow); |
| 947 |
|
| 948 |
|
| 949 |
sel.offsetTopAndBottom(-offset); |
| 950 |
} else if (sel.getTop() < topSelectionPixel) { |
| 951 |
|
| 952 |
|
| 953 |
final int spaceAbove = topSelectionPixel - sel.getTop(); |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
final int spaceBelow = bottomSelectionPixel - sel.getBottom(); |
| 958 |
final int offset = Math.min(spaceAbove, spaceBelow); |
| 959 |
|
| 960 |
|
| 961 |
sel.offsetTopAndBottom(offset); |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
fillAboveAndBelow(sel, selectedPosition); |
| 966 |
|
| 967 |
if (!mStackFromBottom) { |
| 968 |
correctTooHigh(getChildCount()); |
| 969 |
} else { |
| 970 |
correctTooLow(getChildCount()); |
| 971 |
} |
| 972 |
|
| 973 |
return sel; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
private int getBottomSelectionPixel(int childrenBottom, int fadingEdgeLength, |
| 985 |
int selectedPosition) { |
| 986 |
int bottomSelectionPixel = childrenBottom; |
| 987 |
if (selectedPosition != mItemCount - 1) { |
| 988 |
bottomSelectionPixel -= fadingEdgeLength; |
| 989 |
} |
| 990 |
return bottomSelectionPixel; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
private int getTopSelectionPixel(int childrenTop, int fadingEdgeLength, int selectedPosition) { |
| 1002 |
|
| 1003 |
int topSelectionPixel = childrenTop; |
| 1004 |
if (selectedPosition > 0) { |
| 1005 |
topSelectionPixel += fadingEdgeLength; |
| 1006 |
} |
| 1007 |
return topSelectionPixel; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
@android.view.RemotableViewMethod |
| 1016 |
public void smoothScrollToPosition(int position) { |
| 1017 |
super.smoothScrollToPosition(position); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
@android.view.RemotableViewMethod |
| 1026 |
public void smoothScrollByOffset(int offset) { |
| 1027 |
super.smoothScrollByOffset(offset); |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
private View moveSelection(View oldSel, View newSel, int delta, int childrenTop, |
| 1048 |
int childrenBottom) { |
| 1049 |
int fadingEdgeLength = getVerticalFadingEdgeLength(); |
| 1050 |
final int selectedPosition = mSelectedPosition; |
| 1051 |
|
| 1052 |
View sel; |
| 1053 |
|
| 1054 |
final int topSelectionPixel = getTopSelectionPixel(childrenTop, fadingEdgeLength, |
| 1055 |
selectedPosition); |
| 1056 |
final int bottomSelectionPixel = getBottomSelectionPixel(childrenTop, fadingEdgeLength, |
| 1057 |
selectedPosition); |
| 1058 |
|
| 1059 |
if (delta > 0) { |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
oldSel = makeAndAddView(selectedPosition - 1, oldSel.getTop(), true, |
| 1082 |
mListPadding.left, false); |
| 1083 |
|
| 1084 |
final int dividerHeight = mDividerHeight; |
| 1085 |
|
| 1086 |
|
| 1087 |
sel = makeAndAddView(selectedPosition, oldSel.getBottom() + dividerHeight, true, |
| 1088 |
mListPadding.left, true); |
| 1089 |
|
| 1090 |
|
| 1091 |
if (sel.getBottom() > bottomSelectionPixel) { |
| 1092 |
|
| 1093 |
|
| 1094 |
int spaceAbove = sel.getTop() - topSelectionPixel; |
| 1095 |
|
| 1096 |
|
| 1097 |
int spaceBelow = sel.getBottom() - bottomSelectionPixel; |
| 1098 |
|
| 1099 |
|
| 1100 |
int halfVerticalSpace = (childrenBottom - childrenTop) / 2; |
| 1101 |
int offset = Math.min(spaceAbove, spaceBelow); |
| 1102 |
offset = Math.min(offset, halfVerticalSpace); |
| 1103 |
|
| 1104 |
|
| 1105 |
oldSel.offsetTopAndBottom(-offset); |
| 1106 |
|
| 1107 |
sel.offsetTopAndBottom(-offset); |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
if (!mStackFromBottom) { |
| 1112 |
fillUp(mSelectedPosition - 2, sel.getTop() - dividerHeight); |
| 1113 |
adjustViewsUpOrDown(); |
| 1114 |
fillDown(mSelectedPosition + 1, sel.getBottom() + dividerHeight); |
| 1115 |
} else { |
| 1116 |
fillDown(mSelectedPosition + 1, sel.getBottom() + dividerHeight); |
| 1117 |
adjustViewsUpOrDown(); |
| 1118 |
fillUp(mSelectedPosition - 2, sel.getTop() - dividerHeight); |
| 1119 |
} |
| 1120 |
} else if (delta < 0) { |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
if (newSel != null) { |
| 1142 |
|
| 1143 |
sel = makeAndAddView(selectedPosition, newSel.getTop(), true, mListPadding.left, |
| 1144 |
true); |
| 1145 |
} else { |
| 1146 |
|
| 1147 |
|
| 1148 |
sel = makeAndAddView(selectedPosition, oldSel.getTop(), false, mListPadding.left, |
| 1149 |
true); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
if (sel.getTop() < topSelectionPixel) { |
| 1154 |
|
| 1155 |
int spaceAbove = topSelectionPixel - sel.getTop(); |
| 1156 |
|
| 1157 |
|
| 1158 |
int spaceBelow = bottomSelectionPixel - sel.getBottom(); |
| 1159 |
|
| 1160 |
|
| 1161 |
int halfVerticalSpace = (childrenBottom - childrenTop) / 2; |
| 1162 |
int offset = Math.min(spaceAbove, spaceBelow); |
| 1163 |
offset = Math.min(offset, halfVerticalSpace); |
| 1164 |
|
| 1165 |
|
| 1166 |
sel.offsetTopAndBottom(offset); |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
fillAboveAndBelow(sel, selectedPosition); |
| 1171 |
} else { |
| 1172 |
|
| 1173 |
int oldTop = oldSel.getTop(); |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
sel = makeAndAddView(selectedPosition, oldTop, true, mListPadding.left, true); |
| 1179 |
|
| 1180 |
|
| 1181 |
if (oldTop < childrenTop) { |
| 1182 |
|
| 1183 |
|
| 1184 |
int newBottom = sel.getBottom(); |
| 1185 |
if (newBottom < childrenTop + 20) { |
| 1186 |
|
| 1187 |
sel.offsetTopAndBottom(childrenTop - sel.getTop()); |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
fillAboveAndBelow(sel, selectedPosition); |
| 1193 |
} |
| 1194 |
|
| 1195 |
return sel; |
| 1196 |
} |
| 1197 |
|
| 1198 |
private class FocusSelector implements Runnable { |
| 1199 |
|
| 1200 |
private static final int STATE_SET_SELECTION = 1; |
| 1201 |
|
| 1202 |
private static final int STATE_WAIT_FOR_LAYOUT = 2; |
| 1203 |
|
| 1204 |
|
| 1205 |
private static final int STATE_REQUEST_FOCUS = 3; |
| 1206 |
|
| 1207 |
private int mAction; |
| 1208 |
private int mPosition; |
| 1209 |
private int mPositionTop; |
| 1210 |
|
| 1211 |
FocusSelector setupForSetSelection(int position, int top) { |
| 1212 |
mPosition = position; |
| 1213 |
mPositionTop = top; |
| 1214 |
mAction = STATE_SET_SELECTION; |
| 1215 |
return this; |
| 1216 |
} |
| 1217 |
|
| 1218 |
public void run() { |
| 1219 |
if (mAction == STATE_SET_SELECTION) { |
| 1220 |
setSelectionFromTop(mPosition, mPositionTop); |
| 1221 |
mAction = STATE_WAIT_FOR_LAYOUT; |
| 1222 |
} else if (mAction == STATE_REQUEST_FOCUS) { |
| 1223 |
final int childIndex = mPosition - mFirstPosition; |
| 1224 |
final View child = getChildAt(childIndex); |
| 1225 |
if (child != null) { |
| 1226 |
child.requestFocus(); |
| 1227 |
} |
| 1228 |
mAction = -1; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
@Nullable Runnable setupFocusIfValid(int position) { |
| 1233 |
if (mAction != STATE_WAIT_FOR_LAYOUT || position != mPosition) { |
| 1234 |
return null; |
| 1235 |
} |
| 1236 |
mAction = STATE_REQUEST_FOCUS; |
| 1237 |
return this; |
| 1238 |
} |
| 1239 |
|
| 1240 |
void onLayoutComplete() { |
| 1241 |
if (mAction == STATE_WAIT_FOR_LAYOUT) { |
| 1242 |
mAction = -1; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
@Override |
| 1248 |
protected void onDetachedFromWindow() { |
| 1249 |
if (mFocusSelector != null) { |
| 1250 |
removeCallbacks(mFocusSelector); |
| 1251 |
mFocusSelector = null; |
| 1252 |
} |
| 1253 |
super.onDetachedFromWindow(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
@Override |
| 1257 |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 1258 |
if (getChildCount() > 0) { |
| 1259 |
View focusedChild = getFocusedChild(); |
| 1260 |
if (focusedChild != null) { |
| 1261 |
final int childPosition = mFirstPosition + indexOfChild(focusedChild); |
| 1262 |
final int childBottom = focusedChild.getBottom(); |
| 1263 |
final int offset = Math.max(0, childBottom - (h - mPaddingTop)); |
| 1264 |
final int top = focusedChild.getTop() - offset; |
| 1265 |
if (mFocusSelector == null) { |
| 1266 |
mFocusSelector = new FocusSelector(); |
| 1267 |
} |
| 1268 |
post(mFocusSelector.setupForSetSelection(childPosition, top)); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
super.onSizeChanged(w, h, oldw, oldh); |
| 1272 |
} |
| 1273 |
|
| 1274 |
@Override |
| 1275 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1276 |
|
| 1277 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 1278 |
|
| 1279 |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1280 |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1281 |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 1282 |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 1283 |
|
| 1284 |
int childWidth = 0; |
| 1285 |
int childHeight = 0; |
| 1286 |
int childState = 0; |
| 1287 |
|
| 1288 |
mItemCount = mAdapter == null ? 0 : mAdapter.getCount(); |
| 1289 |
if (mItemCount > 0 && (widthMode == MeasureSpec.UNSPECIFIED |
| 1290 |
|| heightMode == MeasureSpec.UNSPECIFIED)) { |
| 1291 |
final View child = obtainView(0, mIsScrap); |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
measureScrapChild(child, 0, widthMeasureSpec, heightSize); |
| 1296 |
|
| 1297 |
childWidth = child.getMeasuredWidth(); |
| 1298 |
childHeight = child.getMeasuredHeight(); |
| 1299 |
childState = combineMeasuredStates(childState, child.getMeasuredState()); |
| 1300 |
|
| 1301 |
if (recycleOnMeasure() && mRecycler.shouldRecycleViewType( |
| 1302 |
((LayoutParams) child.getLayoutParams()).viewType)) { |
| 1303 |
mRecycler.addScrapView(child, 0); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (widthMode == MeasureSpec.UNSPECIFIED) { |
| 1308 |
widthSize = mListPadding.left + mListPadding.right + childWidth + |
| 1309 |
getVerticalScrollbarWidth(); |
| 1310 |
} else { |
| 1311 |
widthSize |= (childState & MEASURED_STATE_MASK); |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (heightMode == MeasureSpec.UNSPECIFIED) { |
| 1315 |
heightSize = mListPadding.top + mListPadding.bottom + childHeight + |
| 1316 |
getVerticalFadingEdgeLength() * 2; |
| 1317 |
} |
| 1318 |
|
| 1319 |
if (heightMode == MeasureSpec.AT_MOST) { |
| 1320 |
|
| 1321 |
heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1); |
| 1322 |
} |
| 1323 |
|
| 1324 |
setMeasuredDimension(widthSize, heightSize); |
| 1325 |
|
| 1326 |
mWidthMeasureSpec = widthMeasureSpec; |
| 1327 |
} |
| 1328 |
|
| 1329 |
private void measureScrapChild(View child, int position, int widthMeasureSpec, int heightHint) { |
| 1330 |
LayoutParams p = (LayoutParams) child.getLayoutParams(); |
| 1331 |
if (p == null) { |
| 1332 |
p = (AbsListView.LayoutParams) generateDefaultLayoutParams(); |
| 1333 |
child.setLayoutParams(p); |
| 1334 |
} |
| 1335 |
p.viewType = mAdapter.getItemViewType(position); |
| 1336 |
p.isEnabled = mAdapter.isEnabled(position); |
| 1337 |
p.forceAdd = true; |
| 1338 |
|
| 1339 |
final int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec, |
| 1340 |
mListPadding.left + mListPadding.right, p.width); |
| 1341 |
final int lpHeight = p.height; |
| 1342 |
final int childHeightSpec; |
| 1343 |
if (lpHeight > 0) { |
| 1344 |
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); |
| 1345 |
} else { |
| 1346 |
childHeightSpec = MeasureSpec.makeSafeMeasureSpec(heightHint, MeasureSpec.UNSPECIFIED); |
| 1347 |
} |
| 1348 |
child.measure(childWidthSpec, childHeightSpec); |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
child.forceLayout(); |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
@ViewDebug.ExportedProperty(category = "list") |
| 1361 |
protected boolean recycleOnMeasure() { |
| 1362 |
return true; |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 1392 |
final int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition, |
| 1393 |
int maxHeight, int disallowPartialChildPosition) { |
| 1394 |
final ListAdapter adapter = mAdapter; |
| 1395 |
if (adapter == null) { |
| 1396 |
return mListPadding.top + mListPadding.bottom; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
int returnedHeight = mListPadding.top + mListPadding.bottom; |
| 1401 |
final int dividerHeight = mDividerHeight; |
| 1402 |
|
| 1403 |
|
| 1404 |
int prevHeightWithoutPartialChild = 0; |
| 1405 |
int i; |
| 1406 |
View child; |
| 1407 |
|
| 1408 |
|
| 1409 |
endPosition = (endPosition == NO_POSITION) ? adapter.getCount() - 1 : endPosition; |
| 1410 |
final AbsListView.RecycleBin recycleBin = mRecycler; |
| 1411 |
final boolean recyle = recycleOnMeasure(); |
| 1412 |
final boolean[] isScrap = mIsScrap; |
| 1413 |
|
| 1414 |
for (i = startPosition; i <= endPosition; ++i) { |
| 1415 |
child = obtainView(i, isScrap); |
| 1416 |
|
| 1417 |
measureScrapChild(child, i, widthMeasureSpec, maxHeight); |
| 1418 |
|
| 1419 |
if (i > 0) { |
| 1420 |
|
| 1421 |
returnedHeight += dividerHeight; |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
if (recyle && recycleBin.shouldRecycleViewType( |
| 1426 |
((LayoutParams) child.getLayoutParams()).viewType)) { |
| 1427 |
recycleBin.addScrapView(child, -1); |
| 1428 |
} |
| 1429 |
|
| 1430 |
returnedHeight += child.getMeasuredHeight(); |
| 1431 |
|
| 1432 |
if (returnedHeight >= maxHeight) { |
| 1433 |
|
| 1434 |
|
| 1435 |
return (disallowPartialChildPosition >= 0) |
| 1436 |
&& (i > disallowPartialChildPosition) |
| 1437 |
&& (prevHeightWithoutPartialChild > 0) |
| 1438 |
&& (returnedHeight != maxHeight) |
| 1439 |
? prevHeightWithoutPartialChild |
| 1440 |
: maxHeight; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) { |
| 1444 |
prevHeightWithoutPartialChild = returnedHeight; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
return returnedHeight; |
| 1451 |
} |
| 1452 |
|
| 1453 |
@Override |
| 1454 |
int findMotionRow(int y) { |
| 1455 |
int childCount = getChildCount(); |
| 1456 |
if (childCount > 0) { |
| 1457 |
if (!mStackFromBottom) { |
| 1458 |
for (int i = 0; i < childCount; i++) { |
| 1459 |
View v = getChildAt(i); |
| 1460 |
if (y <= v.getBottom()) { |
| 1461 |
return mFirstPosition + i; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
} else { |
| 1465 |
for (int i = childCount - 1; i >= 0; i--) { |
| 1466 |
View v = getChildAt(i); |
| 1467 |
if (y >= v.getTop()) { |
| 1468 |
return mFirstPosition + i; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
} |
| 1472 |
} |
| 1473 |
return INVALID_POSITION; |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 1488 |
private View fillSpecific(int position, int top) { |
| 1489 |
boolean tempIsSelected = position == mSelectedPosition; |
| 1490 |
View temp = makeAndAddView(position, top, true, mListPadding.left, tempIsSelected); |
| 1491 |
|
| 1492 |
mFirstPosition = position; |
| 1493 |
|
| 1494 |
View above; |
| 1495 |
View below; |
| 1496 |
|
| 1497 |
final int dividerHeight = mDividerHeight; |
| 1498 |
if (!mStackFromBottom) { |
| 1499 |
above = fillUp(position - 1, temp.getTop() - dividerHeight); |
| 1500 |
|
| 1501 |
adjustViewsUpOrDown(); |
| 1502 |
below = fillDown(position + 1, temp.getBottom() + dividerHeight); |
| 1503 |
int childCount = getChildCount(); |
| 1504 |
if (childCount > 0) { |
| 1505 |
correctTooHigh(childCount); |
| 1506 |
} |
| 1507 |
} else { |
| 1508 |
below = fillDown(position + 1, temp.getBottom() + dividerHeight); |
| 1509 |
|
| 1510 |
adjustViewsUpOrDown(); |
| 1511 |
above = fillUp(position - 1, temp.getTop() - dividerHeight); |
| 1512 |
int childCount = getChildCount(); |
| 1513 |
if (childCount > 0) { |
| 1514 |
correctTooLow(childCount); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
if (tempIsSelected) { |
| 1519 |
return temp; |
| 1520 |
} else if (above != null) { |
| 1521 |
return above; |
| 1522 |
} else { |
| 1523 |
return below; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 1535 |
private void correctTooHigh(int childCount) { |
| 1536 |
|
| 1537 |
|
| 1538 |
int lastPosition = mFirstPosition + childCount - 1; |
| 1539 |
if (lastPosition == mItemCount - 1 && childCount > 0) { |
| 1540 |
|
| 1541 |
|
| 1542 |
final View lastChild = getChildAt(childCount - 1); |
| 1543 |
|
| 1544 |
|
| 1545 |
final int lastBottom = lastChild.getBottom(); |
| 1546 |
|
| 1547 |
|
| 1548 |
final int end = (mBottom - mTop) - mListPadding.bottom; |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
int bottomOffset = end - lastBottom; |
| 1553 |
View firstChild = getChildAt(0); |
| 1554 |
final int firstTop = firstChild.getTop(); |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
if (bottomOffset > 0 && (mFirstPosition > 0 || firstTop < mListPadding.top)) { |
| 1559 |
if (mFirstPosition == 0) { |
| 1560 |
|
| 1561 |
bottomOffset = Math.min(bottomOffset, mListPadding.top - firstTop); |
| 1562 |
} |
| 1563 |
|
| 1564 |
offsetChildrenTopAndBottom(bottomOffset); |
| 1565 |
if (mFirstPosition > 0) { |
| 1566 |
|
| 1567 |
|
| 1568 |
fillUp(mFirstPosition - 1, firstChild.getTop() - mDividerHeight); |
| 1569 |
|
| 1570 |
adjustViewsUpOrDown(); |
| 1571 |
} |
| 1572 |
|
| 1573 |
} |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 1585 |
private void correctTooLow(int childCount) { |
| 1586 |
|
| 1587 |
|
| 1588 |
if (mFirstPosition == 0 && childCount > 0) { |
| 1589 |
|
| 1590 |
|
| 1591 |
final View firstChild = getChildAt(0); |
| 1592 |
|
| 1593 |
|
| 1594 |
final int firstTop = firstChild.getTop(); |
| 1595 |
|
| 1596 |
|
| 1597 |
final int start = mListPadding.top; |
| 1598 |
|
| 1599 |
|
| 1600 |
final int end = (mBottom - mTop) - mListPadding.bottom; |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
int topOffset = firstTop - start; |
| 1605 |
View lastChild = getChildAt(childCount - 1); |
| 1606 |
final int lastBottom = lastChild.getBottom(); |
| 1607 |
int lastPosition = mFirstPosition + childCount - 1; |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
if (topOffset > 0) { |
| 1612 |
if (lastPosition < mItemCount - 1 || lastBottom > end) { |
| 1613 |
if (lastPosition == mItemCount - 1) { |
| 1614 |
|
| 1615 |
topOffset = Math.min(topOffset, lastBottom - end); |
| 1616 |
} |
| 1617 |
|
| 1618 |
offsetChildrenTopAndBottom(-topOffset); |
| 1619 |
if (lastPosition < mItemCount - 1) { |
| 1620 |
|
| 1621 |
|
| 1622 |
fillDown(lastPosition + 1, lastChild.getBottom() + mDividerHeight); |
| 1623 |
|
| 1624 |
adjustViewsUpOrDown(); |
| 1625 |
} |
| 1626 |
} else if (lastPosition == mItemCount - 1) { |
| 1627 |
adjustViewsUpOrDown(); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
@Override |
| 1634 |
protected void layoutChildren() { |
| 1635 |
final boolean blockLayoutRequests = mBlockLayoutRequests; |
| 1636 |
if (blockLayoutRequests) { |
| 1637 |
return; |
| 1638 |
} |
| 1639 |
|
| 1640 |
mBlockLayoutRequests = true; |
| 1641 |
|
| 1642 |
try { |
| 1643 |
super.layoutChildren(); |
| 1644 |
|
| 1645 |
invalidate(); |
| 1646 |
|
| 1647 |
if (mAdapter == null) { |
| 1648 |
resetList(); |
| 1649 |
invokeOnItemScrollListener(); |
| 1650 |
return; |
| 1651 |
} |
| 1652 |
|
| 1653 |
final int childrenTop = mListPadding.top; |
| 1654 |
final int childrenBottom = mBottom - mTop - mListPadding.bottom; |
| 1655 |
final int childCount = getChildCount(); |
| 1656 |
|
| 1657 |
int index = 0; |
| 1658 |
int delta = 0; |
| 1659 |
|
| 1660 |
View sel; |
| 1661 |
View oldSel = null; |
| 1662 |
View oldFirst = null; |
| 1663 |
View newSel = null; |
| 1664 |
|
| 1665 |
|
| 1666 |
switch (mLayoutMode) { |
| 1667 |
case LAYOUT_SET_SELECTION: |
| 1668 |
index = mNextSelectedPosition - mFirstPosition; |
| 1669 |
if (index >= 0 && index < childCount) { |
| 1670 |
newSel = getChildAt(index); |
| 1671 |
} |
| 1672 |
break; |
| 1673 |
case LAYOUT_FORCE_TOP: |
| 1674 |
case LAYOUT_FORCE_BOTTOM: |
| 1675 |
case LAYOUT_SPECIFIC: |
| 1676 |
case LAYOUT_SYNC: |
| 1677 |
break; |
| 1678 |
case LAYOUT_MOVE_SELECTION: |
| 1679 |
default: |
| 1680 |
|
| 1681 |
index = mSelectedPosition - mFirstPosition; |
| 1682 |
if (index >= 0 && index < childCount) { |
| 1683 |
oldSel = getChildAt(index); |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
oldFirst = getChildAt(0); |
| 1688 |
|
| 1689 |
if (mNextSelectedPosition >= 0) { |
| 1690 |
delta = mNextSelectedPosition - mSelectedPosition; |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
newSel = getChildAt(index + delta); |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
boolean dataChanged = mDataChanged; |
| 1699 |
if (dataChanged) { |
| 1700 |
handleDataChanged(); |
| 1701 |
} |
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
if (mItemCount == 0) { |
| 1706 |
resetList(); |
| 1707 |
invokeOnItemScrollListener(); |
| 1708 |
return; |
| 1709 |
} else if (mItemCount != mAdapter.getCount()) { |
| 1710 |
throw new IllegalStateException("The content of the adapter has changed but " |
| 1711 |
+ "ListView did not receive a notification. Make sure the content of " |
| 1712 |
+ "your adapter is not modified from a background thread, but only from " |
| 1713 |
+ "the UI thread. Make sure your adapter calls notifyDataSetChanged() " |
| 1714 |
+ "when its content changes. [in ListView(" + getId() + ", " + getClass() |
| 1715 |
+ ") with Adapter(" + mAdapter.getClass() + ")]"); |
| 1716 |
} |
| 1717 |
|
| 1718 |
setSelectedPositionInt(mNextSelectedPosition); |
| 1719 |
|
| 1720 |
AccessibilityNodeInfo accessibilityFocusLayoutRestoreNode = null; |
| 1721 |
View accessibilityFocusLayoutRestoreView = null; |
| 1722 |
int accessibilityFocusPosition = INVALID_POSITION; |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
final ViewRootImpl viewRootImpl = getViewRootImpl(); |
| 1728 |
if (viewRootImpl != null) { |
| 1729 |
final View focusHost = viewRootImpl.getAccessibilityFocusedHost(); |
| 1730 |
if (focusHost != null) { |
| 1731 |
final View focusChild = getAccessibilityFocusedChild(focusHost); |
| 1732 |
if (focusChild != null) { |
| 1733 |
if (!dataChanged || isDirectChildHeaderOrFooter(focusChild) |
| 1734 |
|| (focusChild.hasTransientState() && mAdapterHasStableIds)) { |
| 1735 |
|
| 1736 |
|
| 1737 |
accessibilityFocusLayoutRestoreView = focusHost; |
| 1738 |
accessibilityFocusLayoutRestoreNode = viewRootImpl |
| 1739 |
.getAccessibilityFocusedVirtualView(); |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
accessibilityFocusPosition = getPositionForView(focusChild); |
| 1745 |
} |
| 1746 |
} |
| 1747 |
} |
| 1748 |
|
| 1749 |
View focusLayoutRestoreDirectChild = null; |
| 1750 |
View focusLayoutRestoreView = null; |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
final View focusedChild = getFocusedChild(); |
| 1756 |
if (focusedChild != null) { |
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
if (!dataChanged || isDirectChildHeaderOrFooter(focusedChild) |
| 1763 |
|| focusedChild.hasTransientState() || mAdapterHasStableIds) { |
| 1764 |
focusLayoutRestoreDirectChild = focusedChild; |
| 1765 |
|
| 1766 |
focusLayoutRestoreView = findFocus(); |
| 1767 |
if (focusLayoutRestoreView != null) { |
| 1768 |
|
| 1769 |
focusLayoutRestoreView.dispatchStartTemporaryDetach(); |
| 1770 |
} |
| 1771 |
} |
| 1772 |
requestFocus(); |
| 1773 |
} |
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
final int firstPosition = mFirstPosition; |
| 1778 |
final RecycleBin recycleBin = mRecycler; |
| 1779 |
if (dataChanged) { |
| 1780 |
for (int i = 0; i < childCount; i++) { |
| 1781 |
recycleBin.addScrapView(getChildAt(i), firstPosition+i); |
| 1782 |
} |
| 1783 |
} else { |
| 1784 |
recycleBin.fillActiveViews(childCount, firstPosition); |
| 1785 |
} |
| 1786 |
|
| 1787 |
|
| 1788 |
detachAllViewsFromParent(); |
| 1789 |
recycleBin.removeSkippedScrap(); |
| 1790 |
|
| 1791 |
switch (mLayoutMode) { |
| 1792 |
case LAYOUT_SET_SELECTION: |
| 1793 |
if (newSel != null) { |
| 1794 |
sel = fillFromSelection(newSel.getTop(), childrenTop, childrenBottom); |
| 1795 |
} else { |
| 1796 |
sel = fillFromMiddle(childrenTop, childrenBottom); |
| 1797 |
} |
| 1798 |
break; |
| 1799 |
case LAYOUT_SYNC: |
| 1800 |
sel = fillSpecific(mSyncPosition, mSpecificTop); |
| 1801 |
break; |
| 1802 |
case LAYOUT_FORCE_BOTTOM: |
| 1803 |
sel = fillUp(mItemCount - 1, childrenBottom); |
| 1804 |
adjustViewsUpOrDown(); |
| 1805 |
break; |
| 1806 |
case LAYOUT_FORCE_TOP: |
| 1807 |
mFirstPosition = 0; |
| 1808 |
sel = fillFromTop(childrenTop); |
| 1809 |
adjustViewsUpOrDown(); |
| 1810 |
break; |
| 1811 |
case LAYOUT_SPECIFIC: |
| 1812 |
final int selectedPosition = reconcileSelectedPosition(); |
| 1813 |
sel = fillSpecific(selectedPosition, mSpecificTop); |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
if (sel == null && mFocusSelector != null) { |
| 1821 |
final Runnable focusRunnable = mFocusSelector |
| 1822 |
.setupFocusIfValid(selectedPosition); |
| 1823 |
if (focusRunnable != null) { |
| 1824 |
post(focusRunnable); |
| 1825 |
} |
| 1826 |
} |
| 1827 |
break; |
| 1828 |
case LAYOUT_MOVE_SELECTION: |
| 1829 |
sel = moveSelection(oldSel, newSel, delta, childrenTop, childrenBottom); |
| 1830 |
break; |
| 1831 |
default: |
| 1832 |
if (childCount == 0) { |
| 1833 |
if (!mStackFromBottom) { |
| 1834 |
final int position = lookForSelectablePosition(0, true); |
| 1835 |
setSelectedPositionInt(position); |
| 1836 |
sel = fillFromTop(childrenTop); |
| 1837 |
} else { |
| 1838 |
final int position = lookForSelectablePosition(mItemCount - 1, false); |
| 1839 |
setSelectedPositionInt(position); |
| 1840 |
sel = fillUp(mItemCount - 1, childrenBottom); |
| 1841 |
} |
| 1842 |
} else { |
| 1843 |
if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) { |
| 1844 |
sel = fillSpecific(mSelectedPosition, |
| 1845 |
oldSel == null ? childrenTop : oldSel.getTop()); |
| 1846 |
} else if (mFirstPosition < mItemCount) { |
| 1847 |
sel = fillSpecific(mFirstPosition, |
| 1848 |
oldFirst == null ? childrenTop : oldFirst.getTop()); |
| 1849 |
} else { |
| 1850 |
sel = fillSpecific(0, childrenTop); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
break; |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
recycleBin.scrapActiveViews(); |
| 1858 |
|
| 1859 |
|
| 1860 |
removeUnusedFixedViews(mHeaderViewInfos); |
| 1861 |
removeUnusedFixedViews(mFooterViewInfos); |
| 1862 |
|
| 1863 |
if (sel != null) { |
| 1864 |
|
| 1865 |
|
| 1866 |
if (mItemsCanFocus && hasFocus() && !sel.hasFocus()) { |
| 1867 |
final boolean focusWasTaken = (sel == focusLayoutRestoreDirectChild && |
| 1868 |
focusLayoutRestoreView != null && |
| 1869 |
focusLayoutRestoreView.requestFocus()) || sel.requestFocus(); |
| 1870 |
if (!focusWasTaken) { |
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
final View focused = getFocusedChild(); |
| 1875 |
if (focused != null) { |
| 1876 |
focused.clearFocus(); |
| 1877 |
} |
| 1878 |
positionSelector(INVALID_POSITION, sel); |
| 1879 |
} else { |
| 1880 |
sel.setSelected(false); |
| 1881 |
mSelectorRect.setEmpty(); |
| 1882 |
} |
| 1883 |
} else { |
| 1884 |
positionSelector(INVALID_POSITION, sel); |
| 1885 |
} |
| 1886 |
mSelectedTop = sel.getTop(); |
| 1887 |
} else { |
| 1888 |
final boolean inTouchMode = mTouchMode == TOUCH_MODE_TAP |
| 1889 |
|| mTouchMode == TOUCH_MODE_DONE_WAITING; |
| 1890 |
if (inTouchMode) { |
| 1891 |
|
| 1892 |
final View child = getChildAt(mMotionPosition - mFirstPosition); |
| 1893 |
if (child != null) { |
| 1894 |
positionSelector(mMotionPosition, child); |
| 1895 |
} |
| 1896 |
} else if (mSelectorPosition != INVALID_POSITION) { |
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
final View child = getChildAt(mSelectorPosition - mFirstPosition); |
| 1901 |
if (child != null) { |
| 1902 |
positionSelector(mSelectorPosition, child); |
| 1903 |
} |
| 1904 |
} else { |
| 1905 |
|
| 1906 |
mSelectedTop = 0; |
| 1907 |
mSelectorRect.setEmpty(); |
| 1908 |
} |
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
if (hasFocus() && focusLayoutRestoreView != null) { |
| 1913 |
focusLayoutRestoreView.requestFocus(); |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
if (viewRootImpl != null) { |
| 1919 |
final View newAccessibilityFocusedView = viewRootImpl.getAccessibilityFocusedHost(); |
| 1920 |
if (newAccessibilityFocusedView == null) { |
| 1921 |
if (accessibilityFocusLayoutRestoreView != null |
| 1922 |
&& accessibilityFocusLayoutRestoreView.isAttachedToWindow()) { |
| 1923 |
final AccessibilityNodeProvider provider = |
| 1924 |
accessibilityFocusLayoutRestoreView.getAccessibilityNodeProvider(); |
| 1925 |
if (accessibilityFocusLayoutRestoreNode != null && provider != null) { |
| 1926 |
final int virtualViewId = AccessibilityNodeInfo.getVirtualDescendantId( |
| 1927 |
accessibilityFocusLayoutRestoreNode.getSourceNodeId()); |
| 1928 |
provider.performAction(virtualViewId, |
| 1929 |
AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null); |
| 1930 |
} else { |
| 1931 |
accessibilityFocusLayoutRestoreView.requestAccessibilityFocus(); |
| 1932 |
} |
| 1933 |
} else if (accessibilityFocusPosition != INVALID_POSITION) { |
| 1934 |
|
| 1935 |
final int position = MathUtils.constrain( |
| 1936 |
accessibilityFocusPosition - mFirstPosition, 0, |
| 1937 |
getChildCount() - 1); |
| 1938 |
final View restoreView = getChildAt(position); |
| 1939 |
if (restoreView != null) { |
| 1940 |
restoreView.requestAccessibilityFocus(); |
| 1941 |
} |
| 1942 |
} |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
if (focusLayoutRestoreView != null |
| 1949 |
&& focusLayoutRestoreView.getWindowToken() != null) { |
| 1950 |
focusLayoutRestoreView.dispatchFinishTemporaryDetach(); |
| 1951 |
} |
| 1952 |
|
| 1953 |
mLayoutMode = LAYOUT_NORMAL; |
| 1954 |
mDataChanged = false; |
| 1955 |
if (mPositionScrollAfterLayout != null) { |
| 1956 |
post(mPositionScrollAfterLayout); |
| 1957 |
mPositionScrollAfterLayout = null; |
| 1958 |
} |
| 1959 |
mNeedSync = false; |
| 1960 |
setNextSelectedPositionInt(mSelectedPosition); |
| 1961 |
|
| 1962 |
updateScrollIndicators(); |
| 1963 |
|
| 1964 |
if (mItemCount > 0) { |
| 1965 |
checkSelectionChanged(); |
| 1966 |
} |
| 1967 |
|
| 1968 |
invokeOnItemScrollListener(); |
| 1969 |
} finally { |
| 1970 |
if (mFocusSelector != null) { |
| 1971 |
mFocusSelector.onLayoutComplete(); |
| 1972 |
} |
| 1973 |
if (!blockLayoutRequests) { |
| 1974 |
mBlockLayoutRequests = false; |
| 1975 |
} |
| 1976 |
} |
| 1977 |
} |
| 1978 |
|
| 1979 |
@Override |
| 1980 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1981 |
boolean trackMotionScroll(int deltaY, int incrementalDeltaY) { |
| 1982 |
final boolean result = super.trackMotionScroll(deltaY, incrementalDeltaY); |
| 1983 |
removeUnusedFixedViews(mHeaderViewInfos); |
| 1984 |
removeUnusedFixedViews(mFooterViewInfos); |
| 1985 |
return result; |
| 1986 |
} |
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
private void removeUnusedFixedViews(@Nullable List<FixedViewInfo> infoList) { |
| 1995 |
if (infoList == null) { |
| 1996 |
return; |
| 1997 |
} |
| 1998 |
for (int i = infoList.size() - 1; i >= 0; i--) { |
| 1999 |
final FixedViewInfo fixedViewInfo = infoList.get(i); |
| 2000 |
final View view = fixedViewInfo.view; |
| 2001 |
final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 2002 |
if (view.getParent() == null && lp != null && lp.recycledHeaderFooter) { |
| 2003 |
removeDetachedView(view, false); |
| 2004 |
lp.recycledHeaderFooter = false; |
| 2005 |
} |
| 2006 |
|
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
@UnsupportedAppUsage |
| 2015 |
private boolean isDirectChildHeaderOrFooter(View child) { |
| 2016 |
final ArrayList<FixedViewInfo> headers = mHeaderViewInfos; |
| 2017 |
final int numHeaders = headers.size(); |
| 2018 |
for (int i = 0; i < numHeaders; i++) { |
| 2019 |
if (child == headers.get(i).view) { |
| 2020 |
return true; |
| 2021 |
} |
| 2022 |
} |
| 2023 |
|
| 2024 |
final ArrayList<FixedViewInfo> footers = mFooterViewInfos; |
| 2025 |
final int numFooters = footers.size(); |
| 2026 |
for (int i = 0; i < numFooters; i++) { |
| 2027 |
if (child == footers.get(i).view) { |
| 2028 |
return true; |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
return false; |
| 2033 |
} |
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
@UnsupportedAppUsage |
| 2050 |
private View makeAndAddView(int position, int y, boolean flow, int childrenLeft, |
| 2051 |
boolean selected) { |
| 2052 |
if (!mDataChanged) { |
| 2053 |
|
| 2054 |
final View activeView = mRecycler.getActiveView(position); |
| 2055 |
if (activeView != null) { |
| 2056 |
|
| 2057 |
|
| 2058 |
setupChild(activeView, position, y, flow, childrenLeft, selected, true); |
| 2059 |
return activeView; |
| 2060 |
} |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
final View child = obtainView(position, mIsScrap); |
| 2066 |
|
| 2067 |
|
| 2068 |
setupChild(child, position, y, flow, childrenLeft, selected, mIsScrap[0]); |
| 2069 |
|
| 2070 |
return child; |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft, |
| 2090 |
boolean selected, boolean isAttachedToWindow) { |
| 2091 |
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "setupListItem"); |
| 2092 |
|
| 2093 |
final boolean isSelected = selected && shouldShowSelector(); |
| 2094 |
final boolean updateChildSelected = isSelected != child.isSelected(); |
| 2095 |
final int mode = mTouchMode; |
| 2096 |
final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL |
| 2097 |
&& mMotionPosition == position; |
| 2098 |
final boolean updateChildPressed = isPressed != child.isPressed(); |
| 2099 |
final boolean needToMeasure = !isAttachedToWindow || updateChildSelected |
| 2100 |
|| child.isLayoutRequested(); |
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams(); |
| 2105 |
if (p == null) { |
| 2106 |
p = (AbsListView.LayoutParams) generateDefaultLayoutParams(); |
| 2107 |
} |
| 2108 |
p.viewType = mAdapter.getItemViewType(position); |
| 2109 |
p.isEnabled = mAdapter.isEnabled(position); |
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
if (updateChildSelected) { |
| 2115 |
child.setSelected(isSelected); |
| 2116 |
} |
| 2117 |
|
| 2118 |
if (updateChildPressed) { |
| 2119 |
child.setPressed(isPressed); |
| 2120 |
} |
| 2121 |
|
| 2122 |
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) { |
| 2123 |
if (child instanceof Checkable) { |
| 2124 |
((Checkable) child).setChecked(mCheckStates.get(position)); |
| 2125 |
} else if (getContext().getApplicationInfo().targetSdkVersion |
| 2126 |
>= android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 2127 |
child.setActivated(mCheckStates.get(position)); |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
if ((isAttachedToWindow && !p.forceAdd) || (p.recycledHeaderFooter |
| 2132 |
&& p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) { |
| 2133 |
attachViewToParent(child, flowDown ? -1 : 0, p); |
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
if (isAttachedToWindow |
| 2138 |
&& (((AbsListView.LayoutParams) child.getLayoutParams()).scrappedFromPosition) |
| 2139 |
!= position) { |
| 2140 |
child.jumpDrawablesToCurrentState(); |
| 2141 |
} |
| 2142 |
} else { |
| 2143 |
p.forceAdd = false; |
| 2144 |
if (p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) { |
| 2145 |
p.recycledHeaderFooter = true; |
| 2146 |
} |
| 2147 |
addViewInLayout(child, flowDown ? -1 : 0, p, true); |
| 2148 |
|
| 2149 |
child.resolveRtlPropertiesIfNeeded(); |
| 2150 |
} |
| 2151 |
|
| 2152 |
if (needToMeasure) { |
| 2153 |
final int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, |
| 2154 |
mListPadding.left + mListPadding.right, p.width); |
| 2155 |
final int lpHeight = p.height; |
| 2156 |
final int childHeightSpec; |
| 2157 |
if (lpHeight > 0) { |
| 2158 |
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); |
| 2159 |
} else { |
| 2160 |
childHeightSpec = MeasureSpec.makeSafeMeasureSpec(getMeasuredHeight(), |
| 2161 |
MeasureSpec.UNSPECIFIED); |
| 2162 |
} |
| 2163 |
child.measure(childWidthSpec, childHeightSpec); |
| 2164 |
} else { |
| 2165 |
cleanupLayoutState(child); |
| 2166 |
} |
| 2167 |
|
| 2168 |
final int w = child.getMeasuredWidth(); |
| 2169 |
final int h = child.getMeasuredHeight(); |
| 2170 |
final int childTop = flowDown ? y : y - h; |
| 2171 |
|
| 2172 |
if (needToMeasure) { |
| 2173 |
final int childRight = childrenLeft + w; |
| 2174 |
final int childBottom = childTop + h; |
| 2175 |
child.layout(childrenLeft, childTop, childRight, childBottom); |
| 2176 |
} else { |
| 2177 |
child.offsetLeftAndRight(childrenLeft - child.getLeft()); |
| 2178 |
child.offsetTopAndBottom(childTop - child.getTop()); |
| 2179 |
} |
| 2180 |
|
| 2181 |
if (mCachingStarted && !child.isDrawingCacheEnabled()) { |
| 2182 |
child.setDrawingCacheEnabled(true); |
| 2183 |
} |
| 2184 |
|
| 2185 |
Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 2186 |
} |
| 2187 |
|
| 2188 |
@Override |
| 2189 |
protected boolean canAnimate() { |
| 2190 |
return super.canAnimate() && mItemCount > 0; |
| 2191 |
} |
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
@Override |
| 2201 |
public void setSelection(int position) { |
| 2202 |
setSelectionFromTop(position, 0); |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
@Override |
| 2211 |
@UnsupportedAppUsage |
| 2212 |
void setSelectionInt(int position) { |
| 2213 |
setNextSelectedPositionInt(position); |
| 2214 |
boolean awakeScrollbars = false; |
| 2215 |
|
| 2216 |
final int selectedPosition = mSelectedPosition; |
| 2217 |
|
| 2218 |
if (selectedPosition >= 0) { |
| 2219 |
if (position == selectedPosition - 1) { |
| 2220 |
awakeScrollbars = true; |
| 2221 |
} else if (position == selectedPosition + 1) { |
| 2222 |
awakeScrollbars = true; |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
if (mPositionScroller != null) { |
| 2227 |
mPositionScroller.stop(); |
| 2228 |
} |
| 2229 |
|
| 2230 |
layoutChildren(); |
| 2231 |
|
| 2232 |
if (awakeScrollbars) { |
| 2233 |
awakenScrollBars(); |
| 2234 |
} |
| 2235 |
} |
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
@Override |
| 2246 |
@UnsupportedAppUsage |
| 2247 |
int lookForSelectablePosition(int position, boolean lookDown) { |
| 2248 |
final ListAdapter adapter = mAdapter; |
| 2249 |
if (adapter == null || isInTouchMode()) { |
| 2250 |
return INVALID_POSITION; |
| 2251 |
} |
| 2252 |
|
| 2253 |
final int count = adapter.getCount(); |
| 2254 |
if (!mAreAllItemsSelectable) { |
| 2255 |
if (lookDown) { |
| 2256 |
position = Math.max(0, position); |
| 2257 |
while (position < count && !adapter.isEnabled(position)) { |
| 2258 |
position++; |
| 2259 |
} |
| 2260 |
} else { |
| 2261 |
position = Math.min(position, count - 1); |
| 2262 |
while (position >= 0 && !adapter.isEnabled(position)) { |
| 2263 |
position--; |
| 2264 |
} |
| 2265 |
} |
| 2266 |
} |
| 2267 |
|
| 2268 |
if (position < 0 || position >= count) { |
| 2269 |
return INVALID_POSITION; |
| 2270 |
} |
| 2271 |
|
| 2272 |
return position; |
| 2273 |
} |
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
int lookForSelectablePositionAfter(int current, int position, boolean lookDown) { |
| 2288 |
final ListAdapter adapter = mAdapter; |
| 2289 |
if (adapter == null || isInTouchMode()) { |
| 2290 |
return INVALID_POSITION; |
| 2291 |
} |
| 2292 |
|
| 2293 |
|
| 2294 |
final int after = lookForSelectablePosition(position, lookDown); |
| 2295 |
if (after != INVALID_POSITION) { |
| 2296 |
return after; |
| 2297 |
} |
| 2298 |
|
| 2299 |
|
| 2300 |
final int count = adapter.getCount(); |
| 2301 |
current = MathUtils.constrain(current, -1, count - 1); |
| 2302 |
if (lookDown) { |
| 2303 |
position = Math.min(position - 1, count - 1); |
| 2304 |
while ((position > current) && !adapter.isEnabled(position)) { |
| 2305 |
position--; |
| 2306 |
} |
| 2307 |
if (position <= current) { |
| 2308 |
return INVALID_POSITION; |
| 2309 |
} |
| 2310 |
} else { |
| 2311 |
position = Math.max(0, position + 1); |
| 2312 |
while ((position < current) && !adapter.isEnabled(position)) { |
| 2313 |
position++; |
| 2314 |
} |
| 2315 |
if (position >= current) { |
| 2316 |
return INVALID_POSITION; |
| 2317 |
} |
| 2318 |
} |
| 2319 |
|
| 2320 |
return position; |
| 2321 |
} |
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
public void setSelectionAfterHeaderView() { |
| 2328 |
final int count = getHeaderViewsCount(); |
| 2329 |
if (count > 0) { |
| 2330 |
mNextSelectedPosition = 0; |
| 2331 |
return; |
| 2332 |
} |
| 2333 |
|
| 2334 |
if (mAdapter != null) { |
| 2335 |
setSelection(count); |
| 2336 |
} else { |
| 2337 |
mNextSelectedPosition = count; |
| 2338 |
mLayoutMode = LAYOUT_SET_SELECTION; |
| 2339 |
} |
| 2340 |
|
| 2341 |
} |
| 2342 |
|
| 2343 |
@Override |
| 2344 |
public boolean dispatchKeyEvent(KeyEvent event) { |
| 2345 |
|
| 2346 |
boolean handled = super.dispatchKeyEvent(event); |
| 2347 |
if (!handled) { |
| 2348 |
|
| 2349 |
View focused = getFocusedChild(); |
| 2350 |
if (focused != null && event.getAction() == KeyEvent.ACTION_DOWN) { |
| 2351 |
|
| 2352 |
|
| 2353 |
handled = onKeyDown(event.getKeyCode(), event); |
| 2354 |
} |
| 2355 |
} |
| 2356 |
return handled; |
| 2357 |
} |
| 2358 |
|
| 2359 |
@Override |
| 2360 |
public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 2361 |
return commonKey(keyCode, 1, event); |
| 2362 |
} |
| 2363 |
|
| 2364 |
@Override |
| 2365 |
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2366 |
return commonKey(keyCode, repeatCount, event); |
| 2367 |
} |
| 2368 |
|
| 2369 |
@Override |
| 2370 |
public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 2371 |
return commonKey(keyCode, 1, event); |
| 2372 |
} |
| 2373 |
|
| 2374 |
private boolean commonKey(int keyCode, int count, KeyEvent event) { |
| 2375 |
if (mAdapter == null || !isAttachedToWindow()) { |
| 2376 |
return false; |
| 2377 |
} |
| 2378 |
|
| 2379 |
if (mDataChanged) { |
| 2380 |
layoutChildren(); |
| 2381 |
} |
| 2382 |
|
| 2383 |
boolean handled = false; |
| 2384 |
int action = event.getAction(); |
| 2385 |
if (KeyEvent.isConfirmKey(keyCode) |
| 2386 |
&& event.hasNoModifiers() && action != KeyEvent.ACTION_UP) { |
| 2387 |
handled = resurrectSelectionIfNeeded(); |
| 2388 |
if (!handled && event.getRepeatCount() == 0 && getChildCount() > 0) { |
| 2389 |
keyPressed(); |
| 2390 |
handled = true; |
| 2391 |
} |
| 2392 |
} |
| 2393 |
|
| 2394 |
|
| 2395 |
if (!handled && action != KeyEvent.ACTION_UP) { |
| 2396 |
switch (keyCode) { |
| 2397 |
case KeyEvent.KEYCODE_DPAD_UP: |
| 2398 |
if (event.hasNoModifiers()) { |
| 2399 |
handled = resurrectSelectionIfNeeded(); |
| 2400 |
if (!handled) { |
| 2401 |
while (count-- > 0) { |
| 2402 |
if (arrowScroll(FOCUS_UP)) { |
| 2403 |
handled = true; |
| 2404 |
} else { |
| 2405 |
break; |
| 2406 |
} |
| 2407 |
} |
| 2408 |
} |
| 2409 |
} else if (event.hasModifiers(KeyEvent.META_ALT_ON)) { |
| 2410 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP); |
| 2411 |
} |
| 2412 |
break; |
| 2413 |
|
| 2414 |
case KeyEvent.KEYCODE_DPAD_DOWN: |
| 2415 |
if (event.hasNoModifiers()) { |
| 2416 |
handled = resurrectSelectionIfNeeded(); |
| 2417 |
if (!handled) { |
| 2418 |
while (count-- > 0) { |
| 2419 |
if (arrowScroll(FOCUS_DOWN)) { |
| 2420 |
handled = true; |
| 2421 |
} else { |
| 2422 |
break; |
| 2423 |
} |
| 2424 |
} |
| 2425 |
} |
| 2426 |
} else if (event.hasModifiers(KeyEvent.META_ALT_ON)) { |
| 2427 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN); |
| 2428 |
} |
| 2429 |
break; |
| 2430 |
|
| 2431 |
case KeyEvent.KEYCODE_DPAD_LEFT: |
| 2432 |
if (event.hasNoModifiers()) { |
| 2433 |
handled = handleHorizontalFocusWithinListItem(View.FOCUS_LEFT); |
| 2434 |
} |
| 2435 |
break; |
| 2436 |
|
| 2437 |
case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 2438 |
if (event.hasNoModifiers()) { |
| 2439 |
handled = handleHorizontalFocusWithinListItem(View.FOCUS_RIGHT); |
| 2440 |
} |
| 2441 |
break; |
| 2442 |
|
| 2443 |
case KeyEvent.KEYCODE_PAGE_UP: |
| 2444 |
if (event.hasNoModifiers()) { |
| 2445 |
handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_UP); |
| 2446 |
} else if (event.hasModifiers(KeyEvent.META_ALT_ON)) { |
| 2447 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP); |
| 2448 |
} |
| 2449 |
break; |
| 2450 |
|
| 2451 |
case KeyEvent.KEYCODE_PAGE_DOWN: |
| 2452 |
if (event.hasNoModifiers()) { |
| 2453 |
handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_DOWN); |
| 2454 |
} else if (event.hasModifiers(KeyEvent.META_ALT_ON)) { |
| 2455 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN); |
| 2456 |
} |
| 2457 |
break; |
| 2458 |
|
| 2459 |
case KeyEvent.KEYCODE_MOVE_HOME: |
| 2460 |
if (event.hasNoModifiers()) { |
| 2461 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP); |
| 2462 |
} |
| 2463 |
break; |
| 2464 |
|
| 2465 |
case KeyEvent.KEYCODE_MOVE_END: |
| 2466 |
if (event.hasNoModifiers()) { |
| 2467 |
handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN); |
| 2468 |
} |
| 2469 |
break; |
| 2470 |
|
| 2471 |
case KeyEvent.KEYCODE_TAB: |
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
if (event.hasNoModifiers()) { |
| 2477 |
handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_DOWN); |
| 2478 |
} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) { |
| 2479 |
handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_UP); |
| 2480 |
} |
| 2481 |
break; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
if (handled) { |
| 2486 |
return true; |
| 2487 |
} |
| 2488 |
|
| 2489 |
if (sendToTextFilter(keyCode, count, event)) { |
| 2490 |
return true; |
| 2491 |
} |
| 2492 |
|
| 2493 |
switch (action) { |
| 2494 |
case KeyEvent.ACTION_DOWN: |
| 2495 |
return super.onKeyDown(keyCode, event); |
| 2496 |
|
| 2497 |
case KeyEvent.ACTION_UP: |
| 2498 |
return super.onKeyUp(keyCode, event); |
| 2499 |
|
| 2500 |
case KeyEvent.ACTION_MULTIPLE: |
| 2501 |
return super.onKeyMultiple(keyCode, count, event); |
| 2502 |
|
| 2503 |
default: |
| 2504 |
return false; |
| 2505 |
} |
| 2506 |
} |
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
boolean pageScroll(int direction) { |
| 2515 |
final int nextPage; |
| 2516 |
final boolean down; |
| 2517 |
|
| 2518 |
if (direction == FOCUS_UP) { |
| 2519 |
nextPage = Math.max(0, mSelectedPosition - getChildCount() - 1); |
| 2520 |
down = false; |
| 2521 |
} else if (direction == FOCUS_DOWN) { |
| 2522 |
nextPage = Math.min(mItemCount - 1, mSelectedPosition + getChildCount() - 1); |
| 2523 |
down = true; |
| 2524 |
} else { |
| 2525 |
return false; |
| 2526 |
} |
| 2527 |
|
| 2528 |
if (nextPage >= 0) { |
| 2529 |
final int position = lookForSelectablePositionAfter(mSelectedPosition, nextPage, down); |
| 2530 |
if (position >= 0) { |
| 2531 |
mLayoutMode = LAYOUT_SPECIFIC; |
| 2532 |
mSpecificTop = mPaddingTop + getVerticalFadingEdgeLength(); |
| 2533 |
|
| 2534 |
if (down && (position > (mItemCount - getChildCount()))) { |
| 2535 |
mLayoutMode = LAYOUT_FORCE_BOTTOM; |
| 2536 |
} |
| 2537 |
|
| 2538 |
if (!down && (position < getChildCount())) { |
| 2539 |
mLayoutMode = LAYOUT_FORCE_TOP; |
| 2540 |
} |
| 2541 |
|
| 2542 |
setSelectionInt(position); |
| 2543 |
invokeOnItemScrollListener(); |
| 2544 |
if (!awakenScrollBars()) { |
| 2545 |
invalidate(); |
| 2546 |
} |
| 2547 |
|
| 2548 |
return true; |
| 2549 |
} |
| 2550 |
} |
| 2551 |
|
| 2552 |
return false; |
| 2553 |
} |
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
boolean fullScroll(int direction) { |
| 2564 |
boolean moved = false; |
| 2565 |
if (direction == FOCUS_UP) { |
| 2566 |
if (mSelectedPosition != 0) { |
| 2567 |
final int position = lookForSelectablePositionAfter(mSelectedPosition, 0, true); |
| 2568 |
if (position >= 0) { |
| 2569 |
mLayoutMode = LAYOUT_FORCE_TOP; |
| 2570 |
setSelectionInt(position); |
| 2571 |
invokeOnItemScrollListener(); |
| 2572 |
} |
| 2573 |
moved = true; |
| 2574 |
} |
| 2575 |
} else if (direction == FOCUS_DOWN) { |
| 2576 |
final int lastItem = (mItemCount - 1); |
| 2577 |
if (mSelectedPosition < lastItem) { |
| 2578 |
final int position = lookForSelectablePositionAfter( |
| 2579 |
mSelectedPosition, lastItem, false); |
| 2580 |
if (position >= 0) { |
| 2581 |
mLayoutMode = LAYOUT_FORCE_BOTTOM; |
| 2582 |
setSelectionInt(position); |
| 2583 |
invokeOnItemScrollListener(); |
| 2584 |
} |
| 2585 |
moved = true; |
| 2586 |
} |
| 2587 |
} |
| 2588 |
|
| 2589 |
if (moved && !awakenScrollBars()) { |
| 2590 |
awakenScrollBars(); |
| 2591 |
invalidate(); |
| 2592 |
} |
| 2593 |
|
| 2594 |
return moved; |
| 2595 |
} |
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
private boolean handleHorizontalFocusWithinListItem(int direction) { |
| 2605 |
if (direction != View.FOCUS_LEFT && direction != View.FOCUS_RIGHT) { |
| 2606 |
throw new IllegalArgumentException("direction must be one of" |
| 2607 |
+ " {View.FOCUS_LEFT, View.FOCUS_RIGHT}"); |
| 2608 |
} |
| 2609 |
|
| 2610 |
final int numChildren = getChildCount(); |
| 2611 |
if (mItemsCanFocus && numChildren > 0 && mSelectedPosition != INVALID_POSITION) { |
| 2612 |
final View selectedView = getSelectedView(); |
| 2613 |
if (selectedView != null && selectedView.hasFocus() && |
| 2614 |
selectedView instanceof ViewGroup) { |
| 2615 |
|
| 2616 |
final View currentFocus = selectedView.findFocus(); |
| 2617 |
final View nextFocus = FocusFinder.getInstance().findNextFocus( |
| 2618 |
(ViewGroup) selectedView, currentFocus, direction); |
| 2619 |
if (nextFocus != null) { |
| 2620 |
|
| 2621 |
Rect focusedRect = mTempRect; |
| 2622 |
if (currentFocus != null) { |
| 2623 |
currentFocus.getFocusedRect(focusedRect); |
| 2624 |
offsetDescendantRectToMyCoords(currentFocus, focusedRect); |
| 2625 |
offsetRectIntoDescendantCoords(nextFocus, focusedRect); |
| 2626 |
} else { |
| 2627 |
focusedRect = null; |
| 2628 |
} |
| 2629 |
if (nextFocus.requestFocus(direction, focusedRect)) { |
| 2630 |
return true; |
| 2631 |
} |
| 2632 |
} |
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
final View globalNextFocus = FocusFinder.getInstance().findNextFocus( |
| 2638 |
(ViewGroup) getRootView(), currentFocus, direction); |
| 2639 |
if (globalNextFocus != null) { |
| 2640 |
return isViewAncestorOf(globalNextFocus, this); |
| 2641 |
} |
| 2642 |
} |
| 2643 |
} |
| 2644 |
return false; |
| 2645 |
} |
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
@UnsupportedAppUsage |
| 2655 |
boolean arrowScroll(int direction) { |
| 2656 |
try { |
| 2657 |
mInLayout = true; |
| 2658 |
final boolean handled = arrowScrollImpl(direction); |
| 2659 |
if (handled) { |
| 2660 |
playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction)); |
| 2661 |
} |
| 2662 |
return handled; |
| 2663 |
} finally { |
| 2664 |
mInLayout = false; |
| 2665 |
} |
| 2666 |
} |
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
private final int nextSelectedPositionForDirection( |
| 2679 |
View selectedView, int selectedPos, int direction) { |
| 2680 |
int nextSelected; |
| 2681 |
|
| 2682 |
if (direction == View.FOCUS_DOWN) { |
| 2683 |
final int listBottom = getHeight() - mListPadding.bottom; |
| 2684 |
if (selectedView != null && selectedView.getBottom() <= listBottom) { |
| 2685 |
nextSelected = selectedPos != INVALID_POSITION && selectedPos >= mFirstPosition ? |
| 2686 |
selectedPos + 1 : |
| 2687 |
mFirstPosition; |
| 2688 |
} else { |
| 2689 |
return INVALID_POSITION; |
| 2690 |
} |
| 2691 |
} else { |
| 2692 |
final int listTop = mListPadding.top; |
| 2693 |
if (selectedView != null && selectedView.getTop() >= listTop) { |
| 2694 |
final int lastPos = mFirstPosition + getChildCount() - 1; |
| 2695 |
nextSelected = selectedPos != INVALID_POSITION && selectedPos <= lastPos ? |
| 2696 |
selectedPos - 1 : |
| 2697 |
lastPos; |
| 2698 |
} else { |
| 2699 |
return INVALID_POSITION; |
| 2700 |
} |
| 2701 |
} |
| 2702 |
|
| 2703 |
if (nextSelected < 0 || nextSelected >= mAdapter.getCount()) { |
| 2704 |
return INVALID_POSITION; |
| 2705 |
} |
| 2706 |
return lookForSelectablePosition(nextSelected, direction == View.FOCUS_DOWN); |
| 2707 |
} |
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
private boolean arrowScrollImpl(int direction) { |
| 2717 |
if (getChildCount() <= 0) { |
| 2718 |
return false; |
| 2719 |
} |
| 2720 |
|
| 2721 |
View selectedView = getSelectedView(); |
| 2722 |
int selectedPos = mSelectedPosition; |
| 2723 |
|
| 2724 |
int nextSelectedPosition = nextSelectedPositionForDirection(selectedView, selectedPos, direction); |
| 2725 |
int amountToScroll = amountToScroll(direction, nextSelectedPosition); |
| 2726 |
|
| 2727 |
|
| 2728 |
final ArrowScrollFocusResult focusResult = mItemsCanFocus ? arrowScrollFocused(direction) : null; |
| 2729 |
if (focusResult != null) { |
| 2730 |
nextSelectedPosition = focusResult.getSelectedPosition(); |
| 2731 |
amountToScroll = focusResult.getAmountToScroll(); |
| 2732 |
} |
| 2733 |
|
| 2734 |
boolean needToRedraw = focusResult != null; |
| 2735 |
if (nextSelectedPosition != INVALID_POSITION) { |
| 2736 |
handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null); |
| 2737 |
setSelectedPositionInt(nextSelectedPosition); |
| 2738 |
setNextSelectedPositionInt(nextSelectedPosition); |
| 2739 |
selectedView = getSelectedView(); |
| 2740 |
selectedPos = nextSelectedPosition; |
| 2741 |
if (mItemsCanFocus && focusResult == null) { |
| 2742 |
|
| 2743 |
|
| 2744 |
final View focused = getFocusedChild(); |
| 2745 |
if (focused != null) { |
| 2746 |
focused.clearFocus(); |
| 2747 |
} |
| 2748 |
} |
| 2749 |
needToRedraw = true; |
| 2750 |
checkSelectionChanged(); |
| 2751 |
} |
| 2752 |
|
| 2753 |
if (amountToScroll > 0) { |
| 2754 |
scrollListItemsBy((direction == View.FOCUS_UP) ? amountToScroll : -amountToScroll); |
| 2755 |
needToRedraw = true; |
| 2756 |
} |
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
if (mItemsCanFocus && (focusResult == null) |
| 2761 |
&& selectedView != null && selectedView.hasFocus()) { |
| 2762 |
final View focused = selectedView.findFocus(); |
| 2763 |
if (focused != null) { |
| 2764 |
if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) { |
| 2765 |
focused.clearFocus(); |
| 2766 |
} |
| 2767 |
} |
| 2768 |
} |
| 2769 |
|
| 2770 |
|
| 2771 |
if (nextSelectedPosition == INVALID_POSITION && selectedView != null |
| 2772 |
&& !isViewAncestorOf(selectedView, this)) { |
| 2773 |
selectedView = null; |
| 2774 |
hideSelector(); |
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
mResurrectToPosition = INVALID_POSITION; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (needToRedraw) { |
| 2782 |
if (selectedView != null) { |
| 2783 |
positionSelectorLikeFocus(selectedPos, selectedView); |
| 2784 |
mSelectedTop = selectedView.getTop(); |
| 2785 |
} |
| 2786 |
if (!awakenScrollBars()) { |
| 2787 |
invalidate(); |
| 2788 |
} |
| 2789 |
invokeOnItemScrollListener(); |
| 2790 |
return true; |
| 2791 |
} |
| 2792 |
|
| 2793 |
return false; |
| 2794 |
} |
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
private void handleNewSelectionChange(View selectedView, int direction, int newSelectedPosition, |
| 2810 |
boolean newFocusAssigned) { |
| 2811 |
if (newSelectedPosition == INVALID_POSITION) { |
| 2812 |
throw new IllegalArgumentException("newSelectedPosition needs to be valid"); |
| 2813 |
} |
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
View topView; |
| 2820 |
View bottomView; |
| 2821 |
int topViewIndex, bottomViewIndex; |
| 2822 |
boolean topSelected = false; |
| 2823 |
final int selectedIndex = mSelectedPosition - mFirstPosition; |
| 2824 |
final int nextSelectedIndex = newSelectedPosition - mFirstPosition; |
| 2825 |
if (direction == View.FOCUS_UP) { |
| 2826 |
topViewIndex = nextSelectedIndex; |
| 2827 |
bottomViewIndex = selectedIndex; |
| 2828 |
topView = getChildAt(topViewIndex); |
| 2829 |
bottomView = selectedView; |
| 2830 |
topSelected = true; |
| 2831 |
} else { |
| 2832 |
topViewIndex = selectedIndex; |
| 2833 |
bottomViewIndex = nextSelectedIndex; |
| 2834 |
topView = selectedView; |
| 2835 |
bottomView = getChildAt(bottomViewIndex); |
| 2836 |
} |
| 2837 |
|
| 2838 |
final int numChildren = getChildCount(); |
| 2839 |
|
| 2840 |
|
| 2841 |
if (topView != null) { |
| 2842 |
topView.setSelected(!newFocusAssigned && topSelected); |
| 2843 |
measureAndAdjustDown(topView, topViewIndex, numChildren); |
| 2844 |
} |
| 2845 |
|
| 2846 |
|
| 2847 |
if (bottomView != null) { |
| 2848 |
bottomView.setSelected(!newFocusAssigned && !topSelected); |
| 2849 |
measureAndAdjustDown(bottomView, bottomViewIndex, numChildren); |
| 2850 |
} |
| 2851 |
} |
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
|
| 2857 |
|
| 2858 |
|
| 2859 |
|
| 2860 |
private void measureAndAdjustDown(View child, int childIndex, int numChildren) { |
| 2861 |
int oldHeight = child.getHeight(); |
| 2862 |
measureItem(child); |
| 2863 |
if (child.getMeasuredHeight() != oldHeight) { |
| 2864 |
|
| 2865 |
relayoutMeasuredItem(child); |
| 2866 |
|
| 2867 |
|
| 2868 |
final int heightDelta = child.getMeasuredHeight() - oldHeight; |
| 2869 |
for (int i = childIndex + 1; i < numChildren; i++) { |
| 2870 |
getChildAt(i).offsetTopAndBottom(heightDelta); |
| 2871 |
} |
| 2872 |
} |
| 2873 |
} |
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
|
| 2879 |
|
| 2880 |
private void measureItem(View child) { |
| 2881 |
ViewGroup.LayoutParams p = child.getLayoutParams(); |
| 2882 |
if (p == null) { |
| 2883 |
p = new ViewGroup.LayoutParams( |
| 2884 |
ViewGroup.LayoutParams.MATCH_PARENT, |
| 2885 |
ViewGroup.LayoutParams.WRAP_CONTENT); |
| 2886 |
} |
| 2887 |
|
| 2888 |
int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, |
| 2889 |
mListPadding.left + mListPadding.right, p.width); |
| 2890 |
int lpHeight = p.height; |
| 2891 |
int childHeightSpec; |
| 2892 |
if (lpHeight > 0) { |
| 2893 |
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); |
| 2894 |
} else { |
| 2895 |
childHeightSpec = MeasureSpec.makeSafeMeasureSpec(getMeasuredHeight(), |
| 2896 |
MeasureSpec.UNSPECIFIED); |
| 2897 |
} |
| 2898 |
child.measure(childWidthSpec, childHeightSpec); |
| 2899 |
} |
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
private void relayoutMeasuredItem(View child) { |
| 2907 |
final int w = child.getMeasuredWidth(); |
| 2908 |
final int h = child.getMeasuredHeight(); |
| 2909 |
final int childLeft = mListPadding.left; |
| 2910 |
final int childRight = childLeft + w; |
| 2911 |
final int childTop = child.getTop(); |
| 2912 |
final int childBottom = childTop + h; |
| 2913 |
child.layout(childLeft, childTop, childRight, childBottom); |
| 2914 |
} |
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
private int getArrowScrollPreviewLength() { |
| 2920 |
return Math.max(MIN_SCROLL_PREVIEW_PIXELS, getVerticalFadingEdgeLength()); |
| 2921 |
} |
| 2922 |
|
| 2923 |
|
| 2924 |
|
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
private int amountToScroll(int direction, int nextSelectedPosition) { |
| 2936 |
final int listBottom = getHeight() - mListPadding.bottom; |
| 2937 |
final int listTop = mListPadding.top; |
| 2938 |
|
| 2939 |
int numChildren = getChildCount(); |
| 2940 |
|
| 2941 |
if (direction == View.FOCUS_DOWN) { |
| 2942 |
int indexToMakeVisible = numChildren - 1; |
| 2943 |
if (nextSelectedPosition != INVALID_POSITION) { |
| 2944 |
indexToMakeVisible = nextSelectedPosition - mFirstPosition; |
| 2945 |
} |
| 2946 |
while (numChildren <= indexToMakeVisible) { |
| 2947 |
|
| 2948 |
addViewBelow(getChildAt(numChildren - 1), mFirstPosition + numChildren - 1); |
| 2949 |
numChildren++; |
| 2950 |
} |
| 2951 |
final int positionToMakeVisible = mFirstPosition + indexToMakeVisible; |
| 2952 |
final View viewToMakeVisible = getChildAt(indexToMakeVisible); |
| 2953 |
|
| 2954 |
int goalBottom = listBottom; |
| 2955 |
if (positionToMakeVisible < mItemCount - 1) { |
| 2956 |
goalBottom -= getArrowScrollPreviewLength(); |
| 2957 |
} |
| 2958 |
|
| 2959 |
if (viewToMakeVisible.getBottom() <= goalBottom) { |
| 2960 |
|
| 2961 |
return 0; |
| 2962 |
} |
| 2963 |
|
| 2964 |
if (nextSelectedPosition != INVALID_POSITION |
| 2965 |
&& (goalBottom - viewToMakeVisible.getTop()) >= getMaxScrollAmount()) { |
| 2966 |
|
| 2967 |
return 0; |
| 2968 |
} |
| 2969 |
|
| 2970 |
int amountToScroll = (viewToMakeVisible.getBottom() - goalBottom); |
| 2971 |
|
| 2972 |
if ((mFirstPosition + numChildren) == mItemCount) { |
| 2973 |
|
| 2974 |
final int max = getChildAt(numChildren - 1).getBottom() - listBottom; |
| 2975 |
amountToScroll = Math.min(amountToScroll, max); |
| 2976 |
} |
| 2977 |
|
| 2978 |
return Math.min(amountToScroll, getMaxScrollAmount()); |
| 2979 |
} else { |
| 2980 |
int indexToMakeVisible = 0; |
| 2981 |
if (nextSelectedPosition != INVALID_POSITION) { |
| 2982 |
indexToMakeVisible = nextSelectedPosition - mFirstPosition; |
| 2983 |
} |
| 2984 |
while (indexToMakeVisible < 0) { |
| 2985 |
|
| 2986 |
addViewAbove(getChildAt(0), mFirstPosition); |
| 2987 |
mFirstPosition--; |
| 2988 |
indexToMakeVisible = nextSelectedPosition - mFirstPosition; |
| 2989 |
} |
| 2990 |
final int positionToMakeVisible = mFirstPosition + indexToMakeVisible; |
| 2991 |
final View viewToMakeVisible = getChildAt(indexToMakeVisible); |
| 2992 |
int goalTop = listTop; |
| 2993 |
if (positionToMakeVisible > 0) { |
| 2994 |
goalTop += getArrowScrollPreviewLength(); |
| 2995 |
} |
| 2996 |
if (viewToMakeVisible.getTop() >= goalTop) { |
| 2997 |
|
| 2998 |
return 0; |
| 2999 |
} |
| 3000 |
|
| 3001 |
if (nextSelectedPosition != INVALID_POSITION && |
| 3002 |
(viewToMakeVisible.getBottom() - goalTop) >= getMaxScrollAmount()) { |
| 3003 |
|
| 3004 |
return 0; |
| 3005 |
} |
| 3006 |
|
| 3007 |
int amountToScroll = (goalTop - viewToMakeVisible.getTop()); |
| 3008 |
if (mFirstPosition == 0) { |
| 3009 |
|
| 3010 |
final int max = listTop - getChildAt(0).getTop(); |
| 3011 |
amountToScroll = Math.min(amountToScroll, max); |
| 3012 |
} |
| 3013 |
return Math.min(amountToScroll, getMaxScrollAmount()); |
| 3014 |
} |
| 3015 |
} |
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
static private class ArrowScrollFocusResult { |
| 3021 |
private int mSelectedPosition; |
| 3022 |
private int mAmountToScroll; |
| 3023 |
|
| 3024 |
|
| 3025 |
|
| 3026 |
|
| 3027 |
void populate(int selectedPosition, int amountToScroll) { |
| 3028 |
mSelectedPosition = selectedPosition; |
| 3029 |
mAmountToScroll = amountToScroll; |
| 3030 |
} |
| 3031 |
|
| 3032 |
public int getSelectedPosition() { |
| 3033 |
return mSelectedPosition; |
| 3034 |
} |
| 3035 |
|
| 3036 |
public int getAmountToScroll() { |
| 3037 |
return mAmountToScroll; |
| 3038 |
} |
| 3039 |
} |
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
private int lookForSelectablePositionOnScreen(int direction) { |
| 3050 |
final int firstPosition = mFirstPosition; |
| 3051 |
if (direction == View.FOCUS_DOWN) { |
| 3052 |
int startPos = (mSelectedPosition != INVALID_POSITION) ? |
| 3053 |
mSelectedPosition + 1 : |
| 3054 |
firstPosition; |
| 3055 |
if (startPos >= mAdapter.getCount()) { |
| 3056 |
return INVALID_POSITION; |
| 3057 |
} |
| 3058 |
if (startPos < firstPosition) { |
| 3059 |
startPos = firstPosition; |
| 3060 |
} |
| 3061 |
|
| 3062 |
final int lastVisiblePos = getLastVisiblePosition(); |
| 3063 |
final ListAdapter adapter = getAdapter(); |
| 3064 |
for (int pos = startPos; pos <= lastVisiblePos; pos++) { |
| 3065 |
if (adapter.isEnabled(pos) |
| 3066 |
&& getChildAt(pos - firstPosition).getVisibility() == View.VISIBLE) { |
| 3067 |
return pos; |
| 3068 |
} |
| 3069 |
} |
| 3070 |
} else { |
| 3071 |
int last = firstPosition + getChildCount() - 1; |
| 3072 |
int startPos = (mSelectedPosition != INVALID_POSITION) ? |
| 3073 |
mSelectedPosition - 1 : |
| 3074 |
firstPosition + getChildCount() - 1; |
| 3075 |
if (startPos < 0 || startPos >= mAdapter.getCount()) { |
| 3076 |
return INVALID_POSITION; |
| 3077 |
} |
| 3078 |
if (startPos > last) { |
| 3079 |
startPos = last; |
| 3080 |
} |
| 3081 |
|
| 3082 |
final ListAdapter adapter = getAdapter(); |
| 3083 |
for (int pos = startPos; pos >= firstPosition; pos--) { |
| 3084 |
if (adapter.isEnabled(pos) |
| 3085 |
&& getChildAt(pos - firstPosition).getVisibility() == View.VISIBLE) { |
| 3086 |
return pos; |
| 3087 |
} |
| 3088 |
} |
| 3089 |
} |
| 3090 |
return INVALID_POSITION; |
| 3091 |
} |
| 3092 |
|
| 3093 |
|
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
private ArrowScrollFocusResult arrowScrollFocused(final int direction) { |
| 3103 |
final View selectedView = getSelectedView(); |
| 3104 |
View newFocus; |
| 3105 |
if (selectedView != null && selectedView.hasFocus()) { |
| 3106 |
View oldFocus = selectedView.findFocus(); |
| 3107 |
newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction); |
| 3108 |
} else { |
| 3109 |
if (direction == View.FOCUS_DOWN) { |
| 3110 |
final boolean topFadingEdgeShowing = (mFirstPosition > 0); |
| 3111 |
final int listTop = mListPadding.top + |
| 3112 |
(topFadingEdgeShowing ? getArrowScrollPreviewLength() : 0); |
| 3113 |
final int ySearchPoint = |
| 3114 |
(selectedView != null && selectedView.getTop() > listTop) ? |
| 3115 |
selectedView.getTop() : |
| 3116 |
listTop; |
| 3117 |
mTempRect.set(0, ySearchPoint, 0, ySearchPoint); |
| 3118 |
} else { |
| 3119 |
final boolean bottomFadingEdgeShowing = |
| 3120 |
(mFirstPosition + getChildCount() - 1) < mItemCount; |
| 3121 |
final int listBottom = getHeight() - mListPadding.bottom - |
| 3122 |
(bottomFadingEdgeShowing ? getArrowScrollPreviewLength() : 0); |
| 3123 |
final int ySearchPoint = |
| 3124 |
(selectedView != null && selectedView.getBottom() < listBottom) ? |
| 3125 |
selectedView.getBottom() : |
| 3126 |
listBottom; |
| 3127 |
mTempRect.set(0, ySearchPoint, 0, ySearchPoint); |
| 3128 |
} |
| 3129 |
newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction); |
| 3130 |
} |
| 3131 |
|
| 3132 |
if (newFocus != null) { |
| 3133 |
final int positionOfNewFocus = positionOfNewFocus(newFocus); |
| 3134 |
|
| 3135 |
|
| 3136 |
|
| 3137 |
if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) { |
| 3138 |
final int selectablePosition = lookForSelectablePositionOnScreen(direction); |
| 3139 |
if (selectablePosition != INVALID_POSITION && |
| 3140 |
((direction == View.FOCUS_DOWN && selectablePosition < positionOfNewFocus) || |
| 3141 |
(direction == View.FOCUS_UP && selectablePosition > positionOfNewFocus))) { |
| 3142 |
return null; |
| 3143 |
} |
| 3144 |
} |
| 3145 |
|
| 3146 |
int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus); |
| 3147 |
|
| 3148 |
final int maxScrollAmount = getMaxScrollAmount(); |
| 3149 |
if (focusScroll < maxScrollAmount) { |
| 3150 |
|
| 3151 |
newFocus.requestFocus(direction); |
| 3152 |
mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll); |
| 3153 |
return mArrowScrollFocusResult; |
| 3154 |
} else if (distanceToView(newFocus) < maxScrollAmount){ |
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
newFocus.requestFocus(direction); |
| 3160 |
mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount); |
| 3161 |
return mArrowScrollFocusResult; |
| 3162 |
} |
| 3163 |
} |
| 3164 |
return null; |
| 3165 |
} |
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
private int positionOfNewFocus(View newFocus) { |
| 3172 |
final int numChildren = getChildCount(); |
| 3173 |
for (int i = 0; i < numChildren; i++) { |
| 3174 |
final View child = getChildAt(i); |
| 3175 |
if (isViewAncestorOf(newFocus, child)) { |
| 3176 |
return mFirstPosition + i; |
| 3177 |
} |
| 3178 |
} |
| 3179 |
throw new IllegalArgumentException("newFocus is not a child of any of the" |
| 3180 |
+ " children of the list!"); |
| 3181 |
} |
| 3182 |
|
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
private boolean isViewAncestorOf(View child, View parent) { |
| 3187 |
if (child == parent) { |
| 3188 |
return true; |
| 3189 |
} |
| 3190 |
|
| 3191 |
final ViewParent theParent = child.getParent(); |
| 3192 |
return (theParent instanceof ViewGroup) && isViewAncestorOf((View) theParent, parent); |
| 3193 |
} |
| 3194 |
|
| 3195 |
|
| 3196 |
|
| 3197 |
|
| 3198 |
|
| 3199 |
|
| 3200 |
|
| 3201 |
|
| 3202 |
|
| 3203 |
|
| 3204 |
private int amountToScrollToNewFocus(int direction, View newFocus, int positionOfNewFocus) { |
| 3205 |
int amountToScroll = 0; |
| 3206 |
newFocus.getDrawingRect(mTempRect); |
| 3207 |
offsetDescendantRectToMyCoords(newFocus, mTempRect); |
| 3208 |
if (direction == View.FOCUS_UP) { |
| 3209 |
if (mTempRect.top < mListPadding.top) { |
| 3210 |
amountToScroll = mListPadding.top - mTempRect.top; |
| 3211 |
if (positionOfNewFocus > 0) { |
| 3212 |
amountToScroll += getArrowScrollPreviewLength(); |
| 3213 |
} |
| 3214 |
} |
| 3215 |
} else { |
| 3216 |
final int listBottom = getHeight() - mListPadding.bottom; |
| 3217 |
if (mTempRect.bottom > listBottom) { |
| 3218 |
amountToScroll = mTempRect.bottom - listBottom; |
| 3219 |
if (positionOfNewFocus < mItemCount - 1) { |
| 3220 |
amountToScroll += getArrowScrollPreviewLength(); |
| 3221 |
} |
| 3222 |
} |
| 3223 |
} |
| 3224 |
return amountToScroll; |
| 3225 |
} |
| 3226 |
|
| 3227 |
|
| 3228 |
|
| 3229 |
|
| 3230 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
private int distanceToView(View descendant) { |
| 3235 |
int distance = 0; |
| 3236 |
descendant.getDrawingRect(mTempRect); |
| 3237 |
offsetDescendantRectToMyCoords(descendant, mTempRect); |
| 3238 |
final int listBottom = mBottom - mTop - mListPadding.bottom; |
| 3239 |
if (mTempRect.bottom < mListPadding.top) { |
| 3240 |
distance = mListPadding.top - mTempRect.bottom; |
| 3241 |
} else if (mTempRect.top > listBottom) { |
| 3242 |
distance = mTempRect.top - listBottom; |
| 3243 |
} |
| 3244 |
return distance; |
| 3245 |
} |
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
@UnsupportedAppUsage |
| 3255 |
private void scrollListItemsBy(int amount) { |
| 3256 |
int oldX = mScrollX; |
| 3257 |
int oldY = mScrollY; |
| 3258 |
|
| 3259 |
offsetChildrenTopAndBottom(amount); |
| 3260 |
|
| 3261 |
final int listBottom = getHeight() - mListPadding.bottom; |
| 3262 |
final int listTop = mListPadding.top; |
| 3263 |
final AbsListView.RecycleBin recycleBin = mRecycler; |
| 3264 |
|
| 3265 |
if (amount < 0) { |
| 3266 |
|
| 3267 |
|
| 3268 |
|
| 3269 |
int numChildren = getChildCount(); |
| 3270 |
View last = getChildAt(numChildren - 1); |
| 3271 |
while (last.getBottom() < listBottom) { |
| 3272 |
final int lastVisiblePosition = mFirstPosition + numChildren - 1; |
| 3273 |
if (lastVisiblePosition < mItemCount - 1) { |
| 3274 |
last = addViewBelow(last, lastVisiblePosition); |
| 3275 |
numChildren++; |
| 3276 |
} else { |
| 3277 |
break; |
| 3278 |
} |
| 3279 |
} |
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
if (last.getBottom() < listBottom) { |
| 3285 |
offsetChildrenTopAndBottom(listBottom - last.getBottom()); |
| 3286 |
} |
| 3287 |
|
| 3288 |
|
| 3289 |
View first = getChildAt(0); |
| 3290 |
while (first.getBottom() < listTop) { |
| 3291 |
AbsListView.LayoutParams layoutParams = (LayoutParams) first.getLayoutParams(); |
| 3292 |
if (recycleBin.shouldRecycleViewType(layoutParams.viewType)) { |
| 3293 |
recycleBin.addScrapView(first, mFirstPosition); |
| 3294 |
} |
| 3295 |
detachViewFromParent(first); |
| 3296 |
first = getChildAt(0); |
| 3297 |
mFirstPosition++; |
| 3298 |
} |
| 3299 |
} else { |
| 3300 |
|
| 3301 |
View first = getChildAt(0); |
| 3302 |
|
| 3303 |
|
| 3304 |
while ((first.getTop() > listTop) && (mFirstPosition > 0)) { |
| 3305 |
first = addViewAbove(first, mFirstPosition); |
| 3306 |
mFirstPosition--; |
| 3307 |
} |
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
if (first.getTop() > listTop) { |
| 3312 |
offsetChildrenTopAndBottom(listTop - first.getTop()); |
| 3313 |
} |
| 3314 |
|
| 3315 |
int lastIndex = getChildCount() - 1; |
| 3316 |
View last = getChildAt(lastIndex); |
| 3317 |
|
| 3318 |
|
| 3319 |
while (last.getTop() > listBottom) { |
| 3320 |
AbsListView.LayoutParams layoutParams = (LayoutParams) last.getLayoutParams(); |
| 3321 |
if (recycleBin.shouldRecycleViewType(layoutParams.viewType)) { |
| 3322 |
recycleBin.addScrapView(last, mFirstPosition+lastIndex); |
| 3323 |
} |
| 3324 |
detachViewFromParent(last); |
| 3325 |
last = getChildAt(--lastIndex); |
| 3326 |
} |
| 3327 |
} |
| 3328 |
recycleBin.fullyDetachScrapViews(); |
| 3329 |
removeUnusedFixedViews(mHeaderViewInfos); |
| 3330 |
removeUnusedFixedViews(mFooterViewInfos); |
| 3331 |
onScrollChanged(mScrollX, mScrollY, oldX, oldY); |
| 3332 |
} |
| 3333 |
|
| 3334 |
private View addViewAbove(View theView, int position) { |
| 3335 |
int abovePosition = position - 1; |
| 3336 |
View view = obtainView(abovePosition, mIsScrap); |
| 3337 |
int edgeOfNewChild = theView.getTop() - mDividerHeight; |
| 3338 |
setupChild(view, abovePosition, edgeOfNewChild, false, mListPadding.left, |
| 3339 |
false, mIsScrap[0]); |
| 3340 |
return view; |
| 3341 |
} |
| 3342 |
|
| 3343 |
private View addViewBelow(View theView, int position) { |
| 3344 |
int belowPosition = position + 1; |
| 3345 |
View view = obtainView(belowPosition, mIsScrap); |
| 3346 |
int edgeOfNewChild = theView.getBottom() + mDividerHeight; |
| 3347 |
setupChild(view, belowPosition, edgeOfNewChild, true, mListPadding.left, |
| 3348 |
false, mIsScrap[0]); |
| 3349 |
return view; |
| 3350 |
} |
| 3351 |
|
| 3352 |
|
| 3353 |
|
| 3354 |
|
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
public void setItemsCanFocus(boolean itemsCanFocus) { |
| 3359 |
mItemsCanFocus = itemsCanFocus; |
| 3360 |
if (!itemsCanFocus) { |
| 3361 |
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); |
| 3362 |
} |
| 3363 |
} |
| 3364 |
|
| 3365 |
|
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
public boolean getItemsCanFocus() { |
| 3370 |
return mItemsCanFocus; |
| 3371 |
} |
| 3372 |
|
| 3373 |
@Override |
| 3374 |
public boolean isOpaque() { |
| 3375 |
boolean retValue = (mCachingActive && mIsCacheColorOpaque && mDividerIsOpaque && |
| 3376 |
hasOpaqueScrollbars()) || super.isOpaque(); |
| 3377 |
if (retValue) { |
| 3378 |
|
| 3379 |
final int listTop = mListPadding != null ? mListPadding.top : mPaddingTop; |
| 3380 |
View first = getChildAt(0); |
| 3381 |
if (first == null || first.getTop() > listTop) { |
| 3382 |
return false; |
| 3383 |
} |
| 3384 |
final int listBottom = getHeight() - |
| 3385 |
(mListPadding != null ? mListPadding.bottom : mPaddingBottom); |
| 3386 |
View last = getChildAt(getChildCount() - 1); |
| 3387 |
if (last == null || last.getBottom() < listBottom) { |
| 3388 |
return false; |
| 3389 |
} |
| 3390 |
} |
| 3391 |
return retValue; |
| 3392 |
} |
| 3393 |
|
| 3394 |
@Override |
| 3395 |
public void setCacheColorHint(int color) { |
| 3396 |
final boolean opaque = (color >>> 24) == 0xFF; |
| 3397 |
mIsCacheColorOpaque = opaque; |
| 3398 |
if (opaque) { |
| 3399 |
if (mDividerPaint == null) { |
| 3400 |
mDividerPaint = new Paint(); |
| 3401 |
} |
| 3402 |
mDividerPaint.setColor(color); |
| 3403 |
} |
| 3404 |
super.setCacheColorHint(color); |
| 3405 |
} |
| 3406 |
|
| 3407 |
void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) { |
| 3408 |
final int height = drawable.getMinimumHeight(); |
| 3409 |
|
| 3410 |
canvas.save(); |
| 3411 |
canvas.clipRect(bounds); |
| 3412 |
|
| 3413 |
final int span = bounds.bottom - bounds.top; |
| 3414 |
if (span < height) { |
| 3415 |
bounds.top = bounds.bottom - height; |
| 3416 |
} |
| 3417 |
|
| 3418 |
drawable.setBounds(bounds); |
| 3419 |
drawable.draw(canvas); |
| 3420 |
|
| 3421 |
canvas.restore(); |
| 3422 |
} |
| 3423 |
|
| 3424 |
void drawOverscrollFooter(Canvas canvas, Drawable drawable, Rect bounds) { |
| 3425 |
final int height = drawable.getMinimumHeight(); |
| 3426 |
|
| 3427 |
canvas.save(); |
| 3428 |
canvas.clipRect(bounds); |
| 3429 |
|
| 3430 |
final int span = bounds.bottom - bounds.top; |
| 3431 |
if (span < height) { |
| 3432 |
bounds.bottom = bounds.top + height; |
| 3433 |
} |
| 3434 |
|
| 3435 |
drawable.setBounds(bounds); |
| 3436 |
drawable.draw(canvas); |
| 3437 |
|
| 3438 |
canvas.restore(); |
| 3439 |
} |
| 3440 |
|
| 3441 |
@Override |
| 3442 |
protected void dispatchDraw(Canvas canvas) { |
| 3443 |
if (mCachingStarted) { |
| 3444 |
mCachingActive = true; |
| 3445 |
} |
| 3446 |
|
| 3447 |
|
| 3448 |
final int dividerHeight = mDividerHeight; |
| 3449 |
final Drawable overscrollHeader = mOverScrollHeader; |
| 3450 |
final Drawable overscrollFooter = mOverScrollFooter; |
| 3451 |
final boolean drawOverscrollHeader = overscrollHeader != null; |
| 3452 |
final boolean drawOverscrollFooter = overscrollFooter != null; |
| 3453 |
final boolean drawDividers = dividerHeight > 0 && mDivider != null; |
| 3454 |
|
| 3455 |
if (drawDividers || drawOverscrollHeader || drawOverscrollFooter) { |
| 3456 |
|
| 3457 |
final Rect bounds = mTempRect; |
| 3458 |
bounds.left = mPaddingLeft; |
| 3459 |
bounds.right = mRight - mLeft - mPaddingRight; |
| 3460 |
|
| 3461 |
final int count = getChildCount(); |
| 3462 |
final int headerCount = getHeaderViewsCount(); |
| 3463 |
final int itemCount = mItemCount; |
| 3464 |
final int footerLimit = (itemCount - mFooterViewInfos.size()); |
| 3465 |
final boolean headerDividers = mHeaderDividersEnabled; |
| 3466 |
final boolean footerDividers = mFooterDividersEnabled; |
| 3467 |
final int first = mFirstPosition; |
| 3468 |
final boolean areAllItemsSelectable = mAreAllItemsSelectable; |
| 3469 |
final ListAdapter adapter = mAdapter; |
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
|
| 3474 |
final boolean fillForMissingDividers = isOpaque() && !super.isOpaque(); |
| 3475 |
|
| 3476 |
if (fillForMissingDividers && mDividerPaint == null && mIsCacheColorOpaque) { |
| 3477 |
mDividerPaint = new Paint(); |
| 3478 |
mDividerPaint.setColor(getCacheColorHint()); |
| 3479 |
} |
| 3480 |
final Paint paint = mDividerPaint; |
| 3481 |
|
| 3482 |
int effectivePaddingTop = 0; |
| 3483 |
int effectivePaddingBottom = 0; |
| 3484 |
if ((mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
| 3485 |
effectivePaddingTop = mListPadding.top; |
| 3486 |
effectivePaddingBottom = mListPadding.bottom; |
| 3487 |
} |
| 3488 |
|
| 3489 |
final int listBottom = mBottom - mTop - effectivePaddingBottom + mScrollY; |
| 3490 |
if (!mStackFromBottom) { |
| 3491 |
int bottom = 0; |
| 3492 |
|
| 3493 |
|
| 3494 |
final int scrollY = mScrollY; |
| 3495 |
if (count > 0 && scrollY < 0) { |
| 3496 |
if (drawOverscrollHeader) { |
| 3497 |
bounds.bottom = 0; |
| 3498 |
bounds.top = scrollY; |
| 3499 |
drawOverscrollHeader(canvas, overscrollHeader, bounds); |
| 3500 |
} else if (drawDividers) { |
| 3501 |
bounds.bottom = 0; |
| 3502 |
bounds.top = -dividerHeight; |
| 3503 |
drawDivider(canvas, bounds, -1); |
| 3504 |
} |
| 3505 |
} |
| 3506 |
|
| 3507 |
for (int i = 0; i < count; i++) { |
| 3508 |
final int itemIndex = (first + i); |
| 3509 |
final boolean isHeader = (itemIndex < headerCount); |
| 3510 |
final boolean isFooter = (itemIndex >= footerLimit); |
| 3511 |
if ((headerDividers || !isHeader) && (footerDividers || !isFooter)) { |
| 3512 |
final View child = getChildAt(i); |
| 3513 |
bottom = child.getBottom(); |
| 3514 |
final boolean isLastItem = (i == (count - 1)); |
| 3515 |
|
| 3516 |
if (drawDividers && (bottom < listBottom) |
| 3517 |
&& !(drawOverscrollFooter && isLastItem)) { |
| 3518 |
final int nextIndex = (itemIndex + 1); |
| 3519 |
|
| 3520 |
|
| 3521 |
|
| 3522 |
if (adapter.isEnabled(itemIndex) && (headerDividers || !isHeader |
| 3523 |
&& (nextIndex >= headerCount)) && (isLastItem |
| 3524 |
|| adapter.isEnabled(nextIndex) && (footerDividers || !isFooter |
| 3525 |
&& (nextIndex < footerLimit)))) { |
| 3526 |
bounds.top = bottom; |
| 3527 |
bounds.bottom = bottom + dividerHeight; |
| 3528 |
drawDivider(canvas, bounds, i); |
| 3529 |
} else if (fillForMissingDividers) { |
| 3530 |
bounds.top = bottom; |
| 3531 |
bounds.bottom = bottom + dividerHeight; |
| 3532 |
canvas.drawRect(bounds, paint); |
| 3533 |
} |
| 3534 |
} |
| 3535 |
} |
| 3536 |
} |
| 3537 |
|
| 3538 |
final int overFooterBottom = mBottom + mScrollY; |
| 3539 |
if (drawOverscrollFooter && first + count == itemCount && |
| 3540 |
overFooterBottom > bottom) { |
| 3541 |
bounds.top = bottom; |
| 3542 |
bounds.bottom = overFooterBottom; |
| 3543 |
drawOverscrollFooter(canvas, overscrollFooter, bounds); |
| 3544 |
} |
| 3545 |
} else { |
| 3546 |
int top; |
| 3547 |
|
| 3548 |
final int scrollY = mScrollY; |
| 3549 |
|
| 3550 |
if (count > 0 && drawOverscrollHeader) { |
| 3551 |
bounds.top = scrollY; |
| 3552 |
bounds.bottom = getChildAt(0).getTop(); |
| 3553 |
drawOverscrollHeader(canvas, overscrollHeader, bounds); |
| 3554 |
} |
| 3555 |
|
| 3556 |
final int start = drawOverscrollHeader ? 1 : 0; |
| 3557 |
for (int i = start; i < count; i++) { |
| 3558 |
final int itemIndex = (first + i); |
| 3559 |
final boolean isHeader = (itemIndex < headerCount); |
| 3560 |
final boolean isFooter = (itemIndex >= footerLimit); |
| 3561 |
if ((headerDividers || !isHeader) && (footerDividers || !isFooter)) { |
| 3562 |
final View child = getChildAt(i); |
| 3563 |
top = child.getTop(); |
| 3564 |
if (drawDividers && (top > effectivePaddingTop)) { |
| 3565 |
final boolean isFirstItem = (i == start); |
| 3566 |
final int previousIndex = (itemIndex - 1); |
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
if (adapter.isEnabled(itemIndex) && (headerDividers || !isHeader |
| 3571 |
&& (previousIndex >= headerCount)) && (isFirstItem || |
| 3572 |
adapter.isEnabled(previousIndex) && (footerDividers || !isFooter |
| 3573 |
&& (previousIndex < footerLimit)))) { |
| 3574 |
bounds.top = top - dividerHeight; |
| 3575 |
bounds.bottom = top; |
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
drawDivider(canvas, bounds, i - 1); |
| 3581 |
} else if (fillForMissingDividers) { |
| 3582 |
bounds.top = top - dividerHeight; |
| 3583 |
bounds.bottom = top; |
| 3584 |
canvas.drawRect(bounds, paint); |
| 3585 |
} |
| 3586 |
} |
| 3587 |
} |
| 3588 |
} |
| 3589 |
|
| 3590 |
if (count > 0 && scrollY > 0) { |
| 3591 |
if (drawOverscrollFooter) { |
| 3592 |
final int absListBottom = mBottom; |
| 3593 |
bounds.top = absListBottom; |
| 3594 |
bounds.bottom = absListBottom + scrollY; |
| 3595 |
drawOverscrollFooter(canvas, overscrollFooter, bounds); |
| 3596 |
} else if (drawDividers) { |
| 3597 |
bounds.top = listBottom; |
| 3598 |
bounds.bottom = listBottom + dividerHeight; |
| 3599 |
drawDivider(canvas, bounds, -1); |
| 3600 |
} |
| 3601 |
} |
| 3602 |
} |
| 3603 |
} |
| 3604 |
|
| 3605 |
|
| 3606 |
super.dispatchDraw(canvas); |
| 3607 |
} |
| 3608 |
|
| 3609 |
@Override |
| 3610 |
protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 3611 |
boolean more = super.drawChild(canvas, child, drawingTime); |
| 3612 |
if (mCachingActive && child.mCachingFailed) { |
| 3613 |
mCachingActive = false; |
| 3614 |
} |
| 3615 |
return more; |
| 3616 |
} |
| 3617 |
|
| 3618 |
|
| 3619 |
|
| 3620 |
|
| 3621 |
|
| 3622 |
|
| 3623 |
|
| 3624 |
|
| 3625 |
|
| 3626 |
|
| 3627 |
void drawDivider(Canvas canvas, Rect bounds, int childIndex) { |
| 3628 |
|
| 3629 |
final Drawable divider = mDivider; |
| 3630 |
|
| 3631 |
divider.setBounds(bounds); |
| 3632 |
divider.draw(canvas); |
| 3633 |
} |
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
|
| 3639 |
|
| 3640 |
|
| 3641 |
@InspectableProperty |
| 3642 |
@Nullable |
| 3643 |
public Drawable getDivider() { |
| 3644 |
return mDivider; |
| 3645 |
} |
| 3646 |
|
| 3647 |
|
| 3648 |
|
| 3649 |
|
| 3650 |
|
| 3651 |
|
| 3652 |
|
| 3653 |
|
| 3654 |
|
| 3655 |
|
| 3656 |
public void setDivider(@Nullable Drawable divider) { |
| 3657 |
if (divider != null) { |
| 3658 |
mDividerHeight = divider.getIntrinsicHeight(); |
| 3659 |
} else { |
| 3660 |
mDividerHeight = 0; |
| 3661 |
} |
| 3662 |
mDivider = divider; |
| 3663 |
mDividerIsOpaque = divider == null || divider.getOpacity() == PixelFormat.OPAQUE; |
| 3664 |
requestLayout(); |
| 3665 |
invalidate(); |
| 3666 |
} |
| 3667 |
|
| 3668 |
|
| 3669 |
|
| 3670 |
|
| 3671 |
@InspectableProperty |
| 3672 |
public int getDividerHeight() { |
| 3673 |
return mDividerHeight; |
| 3674 |
} |
| 3675 |
|
| 3676 |
|
| 3677 |
|
| 3678 |
|
| 3679 |
|
| 3680 |
|
| 3681 |
|
| 3682 |
public void setDividerHeight(int height) { |
| 3683 |
mDividerHeight = height; |
| 3684 |
requestLayout(); |
| 3685 |
invalidate(); |
| 3686 |
} |
| 3687 |
|
| 3688 |
|
| 3689 |
|
| 3690 |
|
| 3691 |
|
| 3692 |
|
| 3693 |
|
| 3694 |
|
| 3695 |
|
| 3696 |
|
| 3697 |
public void setHeaderDividersEnabled(boolean headerDividersEnabled) { |
| 3698 |
mHeaderDividersEnabled = headerDividersEnabled; |
| 3699 |
invalidate(); |
| 3700 |
} |
| 3701 |
|
| 3702 |
|
| 3703 |
|
| 3704 |
|
| 3705 |
|
| 3706 |
|
| 3707 |
@InspectableProperty(name = "headerDividersEnabled") |
| 3708 |
public boolean areHeaderDividersEnabled() { |
| 3709 |
return mHeaderDividersEnabled; |
| 3710 |
} |
| 3711 |
|
| 3712 |
|
| 3713 |
|
| 3714 |
|
| 3715 |
|
| 3716 |
|
| 3717 |
|
| 3718 |
|
| 3719 |
|
| 3720 |
|
| 3721 |
public void setFooterDividersEnabled(boolean footerDividersEnabled) { |
| 3722 |
mFooterDividersEnabled = footerDividersEnabled; |
| 3723 |
invalidate(); |
| 3724 |
} |
| 3725 |
|
| 3726 |
|
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
@InspectableProperty(name = "footerDividersEnabled") |
| 3732 |
public boolean areFooterDividersEnabled() { |
| 3733 |
return mFooterDividersEnabled; |
| 3734 |
} |
| 3735 |
|
| 3736 |
|
| 3737 |
|
| 3738 |
|
| 3739 |
|
| 3740 |
|
| 3741 |
|
| 3742 |
public void setOverscrollHeader(Drawable header) { |
| 3743 |
mOverScrollHeader = header; |
| 3744 |
if (mScrollY < 0) { |
| 3745 |
invalidate(); |
| 3746 |
} |
| 3747 |
} |
| 3748 |
|
| 3749 |
|
| 3750 |
|
| 3751 |
|
| 3752 |
public Drawable getOverscrollHeader() { |
| 3753 |
return mOverScrollHeader; |
| 3754 |
} |
| 3755 |
|
| 3756 |
|
| 3757 |
|
| 3758 |
|
| 3759 |
|
| 3760 |
|
| 3761 |
|
| 3762 |
|
| 3763 |
public void setOverscrollFooter(Drawable footer) { |
| 3764 |
mOverScrollFooter = footer; |
| 3765 |
invalidate(); |
| 3766 |
} |
| 3767 |
|
| 3768 |
|
| 3769 |
|
| 3770 |
|
| 3771 |
public Drawable getOverscrollFooter() { |
| 3772 |
return mOverScrollFooter; |
| 3773 |
} |
| 3774 |
|
| 3775 |
@Override |
| 3776 |
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { |
| 3777 |
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
| 3778 |
|
| 3779 |
final ListAdapter adapter = mAdapter; |
| 3780 |
int closetChildIndex = -1; |
| 3781 |
int closestChildTop = 0; |
| 3782 |
if (adapter != null && gainFocus && previouslyFocusedRect != null) { |
| 3783 |
previouslyFocusedRect.offset(mScrollX, mScrollY); |
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
if (adapter.getCount() < getChildCount() + mFirstPosition) { |
| 3788 |
mLayoutMode = LAYOUT_NORMAL; |
| 3789 |
layoutChildren(); |
| 3790 |
} |
| 3791 |
|
| 3792 |
|
| 3793 |
|
| 3794 |
Rect otherRect = mTempRect; |
| 3795 |
int minDistance = Integer.MAX_VALUE; |
| 3796 |
final int childCount = getChildCount(); |
| 3797 |
final int firstPosition = mFirstPosition; |
| 3798 |
|
| 3799 |
for (int i = 0; i < childCount; i++) { |
| 3800 |
|
| 3801 |
if (!adapter.isEnabled(firstPosition + i)) { |
| 3802 |
continue; |
| 3803 |
} |
| 3804 |
|
| 3805 |
View other = getChildAt(i); |
| 3806 |
other.getDrawingRect(otherRect); |
| 3807 |
offsetDescendantRectToMyCoords(other, otherRect); |
| 3808 |
int distance = getDistance(previouslyFocusedRect, otherRect, direction); |
| 3809 |
|
| 3810 |
if (distance < minDistance) { |
| 3811 |
minDistance = distance; |
| 3812 |
closetChildIndex = i; |
| 3813 |
closestChildTop = other.getTop(); |
| 3814 |
} |
| 3815 |
} |
| 3816 |
} |
| 3817 |
|
| 3818 |
if (closetChildIndex >= 0) { |
| 3819 |
setSelectionFromTop(closetChildIndex + mFirstPosition, closestChildTop); |
| 3820 |
} else { |
| 3821 |
requestLayout(); |
| 3822 |
} |
| 3823 |
} |
| 3824 |
|
| 3825 |
|
| 3826 |
|
| 3827 |
|
| 3828 |
|
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
@Override |
| 3833 |
protected void onFinishInflate() { |
| 3834 |
super.onFinishInflate(); |
| 3835 |
|
| 3836 |
int count = getChildCount(); |
| 3837 |
if (count > 0) { |
| 3838 |
for (int i = 0; i < count; ++i) { |
| 3839 |
addHeaderView(getChildAt(i)); |
| 3840 |
} |
| 3841 |
removeAllViews(); |
| 3842 |
} |
| 3843 |
} |
| 3844 |
|
| 3845 |
|
| 3846 |
|
| 3847 |
|
| 3848 |
|
| 3849 |
@Override |
| 3850 |
protected <T extends View> T findViewTraversal(@IdRes int id) { |
| 3851 |
|
| 3852 |
|
| 3853 |
View v = super.findViewTraversal(id); |
| 3854 |
if (v == null) { |
| 3855 |
v = findViewInHeadersOrFooters(mHeaderViewInfos, id); |
| 3856 |
if (v != null) { |
| 3857 |
return (T) v; |
| 3858 |
} |
| 3859 |
v = findViewInHeadersOrFooters(mFooterViewInfos, id); |
| 3860 |
if (v != null) { |
| 3861 |
return (T) v; |
| 3862 |
} |
| 3863 |
} |
| 3864 |
return (T) v; |
| 3865 |
} |
| 3866 |
|
| 3867 |
View findViewInHeadersOrFooters(ArrayList<FixedViewInfo> where, int id) { |
| 3868 |
|
| 3869 |
if (where != null) { |
| 3870 |
int len = where.size(); |
| 3871 |
View v; |
| 3872 |
|
| 3873 |
for (int i = 0; i < len; i++) { |
| 3874 |
v = where.get(i).view; |
| 3875 |
|
| 3876 |
if (!v.isRootNamespace()) { |
| 3877 |
v = v.findViewById(id); |
| 3878 |
|
| 3879 |
if (v != null) { |
| 3880 |
return v; |
| 3881 |
} |
| 3882 |
} |
| 3883 |
} |
| 3884 |
} |
| 3885 |
return null; |
| 3886 |
} |
| 3887 |
|
| 3888 |
|
| 3889 |
|
| 3890 |
|
| 3891 |
|
| 3892 |
@Override |
| 3893 |
protected <T extends View> T findViewWithTagTraversal(Object tag) { |
| 3894 |
|
| 3895 |
|
| 3896 |
View v = super.findViewWithTagTraversal(tag); |
| 3897 |
if (v == null) { |
| 3898 |
v = findViewWithTagInHeadersOrFooters(mHeaderViewInfos, tag); |
| 3899 |
if (v != null) { |
| 3900 |
return (T) v; |
| 3901 |
} |
| 3902 |
|
| 3903 |
v = findViewWithTagInHeadersOrFooters(mFooterViewInfos, tag); |
| 3904 |
if (v != null) { |
| 3905 |
return (T) v; |
| 3906 |
} |
| 3907 |
} |
| 3908 |
return (T) v; |
| 3909 |
} |
| 3910 |
|
| 3911 |
View findViewWithTagInHeadersOrFooters(ArrayList<FixedViewInfo> where, Object tag) { |
| 3912 |
|
| 3913 |
|
| 3914 |
if (where != null) { |
| 3915 |
int len = where.size(); |
| 3916 |
View v; |
| 3917 |
|
| 3918 |
for (int i = 0; i < len; i++) { |
| 3919 |
v = where.get(i).view; |
| 3920 |
|
| 3921 |
if (!v.isRootNamespace()) { |
| 3922 |
v = v.findViewWithTag(tag); |
| 3923 |
|
| 3924 |
if (v != null) { |
| 3925 |
return v; |
| 3926 |
} |
| 3927 |
} |
| 3928 |
} |
| 3929 |
} |
| 3930 |
return null; |
| 3931 |
} |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
|
| 3939 |
|
| 3940 |
@Override |
| 3941 |
protected <T extends View> T findViewByPredicateTraversal( |
| 3942 |
Predicate<View> predicate, View childToSkip) { |
| 3943 |
View v = super.findViewByPredicateTraversal(predicate, childToSkip); |
| 3944 |
if (v == null) { |
| 3945 |
v = findViewByPredicateInHeadersOrFooters(mHeaderViewInfos, predicate, childToSkip); |
| 3946 |
if (v != null) { |
| 3947 |
return (T) v; |
| 3948 |
} |
| 3949 |
|
| 3950 |
v = findViewByPredicateInHeadersOrFooters(mFooterViewInfos, predicate, childToSkip); |
| 3951 |
if (v != null) { |
| 3952 |
return (T) v; |
| 3953 |
} |
| 3954 |
} |
| 3955 |
return (T) v; |
| 3956 |
} |
| 3957 |
|
| 3958 |
|
| 3959 |
|
| 3960 |
|
| 3961 |
|
| 3962 |
View findViewByPredicateInHeadersOrFooters(ArrayList<FixedViewInfo> where, |
| 3963 |
Predicate<View> predicate, View childToSkip) { |
| 3964 |
if (where != null) { |
| 3965 |
int len = where.size(); |
| 3966 |
View v; |
| 3967 |
|
| 3968 |
for (int i = 0; i < len; i++) { |
| 3969 |
v = where.get(i).view; |
| 3970 |
|
| 3971 |
if (v != childToSkip && !v.isRootNamespace()) { |
| 3972 |
v = v.findViewByPredicate(predicate); |
| 3973 |
|
| 3974 |
if (v != null) { |
| 3975 |
return v; |
| 3976 |
} |
| 3977 |
} |
| 3978 |
} |
| 3979 |
} |
| 3980 |
return null; |
| 3981 |
} |
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
|
| 3987 |
|
| 3988 |
|
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
@Deprecated |
| 3993 |
public long[] getCheckItemIds() { |
| 3994 |
|
| 3995 |
if (mAdapter != null && mAdapter.hasStableIds()) { |
| 3996 |
return getCheckedItemIds(); |
| 3997 |
} |
| 3998 |
|
| 3999 |
|
| 4000 |
|
| 4001 |
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) { |
| 4002 |
final SparseBooleanArray states = mCheckStates; |
| 4003 |
final int count = states.size(); |
| 4004 |
final long[] ids = new long[count]; |
| 4005 |
final ListAdapter adapter = mAdapter; |
| 4006 |
|
| 4007 |
int checkedCount = 0; |
| 4008 |
for (int i = 0; i < count; i++) { |
| 4009 |
if (states.valueAt(i)) { |
| 4010 |
ids[checkedCount++] = adapter.getItemId(states.keyAt(i)); |
| 4011 |
} |
| 4012 |
} |
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
if (checkedCount == count) { |
| 4017 |
return ids; |
| 4018 |
} else { |
| 4019 |
final long[] result = new long[checkedCount]; |
| 4020 |
System.arraycopy(ids, 0, result, 0, checkedCount); |
| 4021 |
|
| 4022 |
return result; |
| 4023 |
} |
| 4024 |
} |
| 4025 |
return new long[0]; |
| 4026 |
} |
| 4027 |
|
| 4028 |
@Override |
| 4029 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 4030 |
int getHeightForPosition(int position) { |
| 4031 |
final int height = super.getHeightForPosition(position); |
| 4032 |
if (shouldAdjustHeightForDivider(position)) { |
| 4033 |
return height + mDividerHeight; |
| 4034 |
} |
| 4035 |
return height; |
| 4036 |
} |
| 4037 |
|
| 4038 |
private boolean shouldAdjustHeightForDivider(int itemIndex) { |
| 4039 |
final int dividerHeight = mDividerHeight; |
| 4040 |
final Drawable overscrollHeader = mOverScrollHeader; |
| 4041 |
final Drawable overscrollFooter = mOverScrollFooter; |
| 4042 |
final boolean drawOverscrollHeader = overscrollHeader != null; |
| 4043 |
final boolean drawOverscrollFooter = overscrollFooter != null; |
| 4044 |
final boolean drawDividers = dividerHeight > 0 && mDivider != null; |
| 4045 |
|
| 4046 |
if (drawDividers) { |
| 4047 |
final boolean fillForMissingDividers = isOpaque() && !super.isOpaque(); |
| 4048 |
final int itemCount = mItemCount; |
| 4049 |
final int headerCount = getHeaderViewsCount(); |
| 4050 |
final int footerLimit = (itemCount - mFooterViewInfos.size()); |
| 4051 |
final boolean isHeader = (itemIndex < headerCount); |
| 4052 |
final boolean isFooter = (itemIndex >= footerLimit); |
| 4053 |
final boolean headerDividers = mHeaderDividersEnabled; |
| 4054 |
final boolean footerDividers = mFooterDividersEnabled; |
| 4055 |
if ((headerDividers || !isHeader) && (footerDividers || !isFooter)) { |
| 4056 |
final ListAdapter adapter = mAdapter; |
| 4057 |
if (!mStackFromBottom) { |
| 4058 |
final boolean isLastItem = (itemIndex == (itemCount - 1)); |
| 4059 |
if (!drawOverscrollFooter || !isLastItem) { |
| 4060 |
final int nextIndex = itemIndex + 1; |
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
if (adapter.isEnabled(itemIndex) && (headerDividers || !isHeader |
| 4065 |
&& (nextIndex >= headerCount)) && (isLastItem |
| 4066 |
|| adapter.isEnabled(nextIndex) && (footerDividers || !isFooter |
| 4067 |
&& (nextIndex < footerLimit)))) { |
| 4068 |
return true; |
| 4069 |
} else if (fillForMissingDividers) { |
| 4070 |
return true; |
| 4071 |
} |
| 4072 |
} |
| 4073 |
} else { |
| 4074 |
final int start = drawOverscrollHeader ? 1 : 0; |
| 4075 |
final boolean isFirstItem = (itemIndex == start); |
| 4076 |
if (!isFirstItem) { |
| 4077 |
final int previousIndex = (itemIndex - 1); |
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
if (adapter.isEnabled(itemIndex) && (headerDividers || !isHeader |
| 4082 |
&& (previousIndex >= headerCount)) && (isFirstItem || |
| 4083 |
adapter.isEnabled(previousIndex) && (footerDividers || !isFooter |
| 4084 |
&& (previousIndex < footerLimit)))) { |
| 4085 |
return true; |
| 4086 |
} else if (fillForMissingDividers) { |
| 4087 |
return true; |
| 4088 |
} |
| 4089 |
} |
| 4090 |
} |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
return false; |
| 4095 |
} |
| 4096 |
|
| 4097 |
@Override |
| 4098 |
public CharSequence getAccessibilityClassName() { |
| 4099 |
return ListView.class.getName(); |
| 4100 |
} |
| 4101 |
|
| 4102 |
|
| 4103 |
@Override |
| 4104 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 4105 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 4106 |
|
| 4107 |
final int rowsCount = getCount(); |
| 4108 |
final int selectionMode = getSelectionModeForAccessibility(); |
| 4109 |
final CollectionInfo collectionInfo = CollectionInfo.obtain( |
| 4110 |
-1, -1, false, selectionMode); |
| 4111 |
info.setCollectionInfo(collectionInfo); |
| 4112 |
|
| 4113 |
if (rowsCount > 0) { |
| 4114 |
info.addAction(AccessibilityAction.ACTION_SCROLL_TO_POSITION); |
| 4115 |
} |
| 4116 |
} |
| 4117 |
|
| 4118 |
|
| 4119 |
@Override |
| 4120 |
public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
| 4121 |
if (super.performAccessibilityActionInternal(action, arguments)) { |
| 4122 |
return true; |
| 4123 |
} |
| 4124 |
|
| 4125 |
switch (action) { |
| 4126 |
case R.id.accessibilityActionScrollToPosition: { |
| 4127 |
final int row = arguments.getInt(AccessibilityNodeInfo.ACTION_ARGUMENT_ROW_INT, -1); |
| 4128 |
final int position = Math.min(row, getCount() - 1); |
| 4129 |
if (row >= 0) { |
| 4130 |
|
| 4131 |
|
| 4132 |
smoothScrollToPosition(position); |
| 4133 |
return true; |
| 4134 |
} |
| 4135 |
} break; |
| 4136 |
} |
| 4137 |
|
| 4138 |
return false; |
| 4139 |
} |
| 4140 |
|
| 4141 |
@Override |
| 4142 |
public void onInitializeAccessibilityNodeInfoForItem( |
| 4143 |
View view, int position, AccessibilityNodeInfo info) { |
| 4144 |
super.onInitializeAccessibilityNodeInfoForItem(view, position, info); |
| 4145 |
|
| 4146 |
final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 4147 |
final boolean isHeading = lp != null && lp.viewType == ITEM_VIEW_TYPE_HEADER_OR_FOOTER; |
| 4148 |
final boolean isSelected = isItemChecked(position); |
| 4149 |
final CollectionItemInfo itemInfo = CollectionItemInfo.obtain( |
| 4150 |
position, 1, 0, 1, isHeading, isSelected); |
| 4151 |
info.setCollectionItemInfo(itemInfo); |
| 4152 |
} |
| 4153 |
|
| 4154 |
|
| 4155 |
@Override |
| 4156 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 4157 |
super.encodeProperties(encoder); |
| 4158 |
|
| 4159 |
encoder.addProperty("recycleOnMeasure", recycleOnMeasure()); |
| 4160 |
} |
| 4161 |
|
| 4162 |
|
| 4163 |
protected HeaderViewListAdapter wrapHeaderListAdapterInternal( |
| 4164 |
ArrayList<ListView.FixedViewInfo> headerViewInfos, |
| 4165 |
ArrayList<ListView.FixedViewInfo> footerViewInfos, |
| 4166 |
ListAdapter adapter) { |
| 4167 |
return new HeaderViewListAdapter(headerViewInfos, footerViewInfos, adapter); |
| 4168 |
} |
| 4169 |
|
| 4170 |
|
| 4171 |
protected void wrapHeaderListAdapterInternal() { |
| 4172 |
mAdapter = wrapHeaderListAdapterInternal(mHeaderViewInfos, mFooterViewInfos, mAdapter); |
| 4173 |
} |
| 4174 |
|
| 4175 |
|
| 4176 |
protected void dispatchDataSetObserverOnChangedInternal() { |
| 4177 |
if (mDataSetObserver != null) { |
| 4178 |
mDataSetObserver.onChanged(); |
| 4179 |
} |
| 4180 |
} |
| 4181 |
} |