1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.graphics.Matrix; 22 import android.graphics.Rect; 23 import android.graphics.Region; 24 import android.os.Bundle; 25 import android.view.accessibility.AccessibilityEvent; 26 import android.window.OnBackInvokedDispatcher; 27 28 /** 29 * Defines the responsibilities for a class that will be a parent of a View. 30 * This is the API that a view sees when it wants to interact with its parent. 31 * 32 */ 33 public interface ViewParent { 34 /** 35 * Called when something has changed which has invalidated the layout of a 36 * child of this view parent. This will schedule a layout pass of the view 37 * tree. 38 */ requestLayout()39 public void requestLayout(); 40 41 /** 42 * Indicates whether layout was requested on this view parent. 43 * 44 * @return true if layout was requested, false otherwise 45 */ isLayoutRequested()46 public boolean isLayoutRequested(); 47 48 /** 49 * Called when a child wants the view hierarchy to gather and report 50 * transparent regions to the window compositor. Views that "punch" holes in 51 * the view hierarchy, such as SurfaceView can use this API to improve 52 * performance of the system. When no such a view is present in the 53 * hierarchy, this optimization in unnecessary and might slightly reduce the 54 * view hierarchy performance. 55 * 56 * @param child the view requesting the transparent region computation 57 * 58 */ requestTransparentRegion(View child)59 public void requestTransparentRegion(View child); 60 61 62 /** 63 * The target View has been invalidated, or has had a drawing property changed that 64 * requires the hierarchy to re-render. 65 * 66 * This method is called by the View hierarchy to signal ancestors that a View either needs to 67 * re-record its drawing commands, or drawing properties have changed. This is how Views 68 * schedule a drawing traversal. 69 * 70 * This signal is generally only dispatched for attached Views, since only they need to draw. 71 * 72 * @param child Direct child of this ViewParent containing target 73 * @param target The view that needs to redraw 74 */ onDescendantInvalidated(@onNull View child, @NonNull View target)75 default void onDescendantInvalidated(@NonNull View child, @NonNull View target) { 76 if (getParent() != null) { 77 // Note: should pass 'this' as default, but can't since we may not be a View 78 getParent().onDescendantInvalidated(child, target); 79 } 80 } 81 82 /** 83 * All or part of a child is dirty and needs to be redrawn. 84 * 85 * @param child The child which is dirty 86 * @param r The area within the child that is invalid 87 * 88 * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead. 89 */ 90 @Deprecated invalidateChild(View child, Rect r)91 public void invalidateChild(View child, Rect r); 92 93 /** 94 * All or part of a child is dirty and needs to be redrawn. 95 * 96 * <p>The location array is an array of two int values which respectively 97 * define the left and the top position of the dirty child.</p> 98 * 99 * <p>This method must return the parent of this ViewParent if the specified 100 * rectangle must be invalidated in the parent. If the specified rectangle 101 * does not require invalidation in the parent or if the parent does not 102 * exist, this method must return null.</p> 103 * 104 * <p>When this method returns a non-null value, the location array must 105 * have been updated with the left and top coordinates of this ViewParent.</p> 106 * 107 * @param location An array of 2 ints containing the left and top 108 * coordinates of the child to invalidate 109 * @param r The area within the child that is invalid 110 * 111 * @return the parent of this ViewParent or null 112 * 113 * @deprecated Use {@link #onDescendantInvalidated(View, View)} instead. 114 */ 115 @Deprecated invalidateChildInParent(int[] location, Rect r)116 public ViewParent invalidateChildInParent(int[] location, Rect r); 117 118 /** 119 * Returns the parent if it exists, or null. 120 * 121 * @return a ViewParent or null if this ViewParent does not have a parent 122 */ getParent()123 public ViewParent getParent(); 124 125 /** 126 * Called when a child of this parent wants focus 127 * 128 * @param child The child of this ViewParent that wants focus. This view 129 * will contain the focused view. It is not necessarily the view that 130 * actually has focus. 131 * @param focused The view that is a descendant of child that actually has 132 * focus 133 */ requestChildFocus(View child, View focused)134 public void requestChildFocus(View child, View focused); 135 136 /** 137 * Tell view hierarchy that the global view attributes need to be 138 * re-evaluated. 139 * 140 * @param child View whose attributes have changed. 141 */ recomputeViewAttributes(View child)142 public void recomputeViewAttributes(View child); 143 144 /** 145 * Called when a child of this parent is giving up focus 146 * 147 * @param child The view that is giving up focus 148 */ clearChildFocus(View child)149 public void clearChildFocus(View child); 150 151 /** 152 * Compute the visible part of a rectangular region defined in terms of a child view's 153 * coordinates. 154 * 155 * <p>Returns the clipped visible part of the rectangle <code>r</code>, defined in the 156 * <code>child</code>'s local coordinate system. <code>r</code> is modified by this method to 157 * contain the result, expressed in the global (root) coordinate system.</p> 158 * 159 * <p>The resulting rectangle is always axis aligned. If a rotation is applied to a node in the 160 * View hierarchy, the result is the axis-aligned bounding box of the visible rectangle.</p> 161 * 162 * @param child A child View, whose rectangular visible region we want to compute 163 * @param r The input rectangle, defined in the child coordinate system. Will be overwritten to 164 * contain the resulting visible rectangle, expressed in global (root) coordinates 165 * @param offset The input coordinates of a point, defined in the child coordinate system. 166 * As with the <code>r</code> parameter, this will be overwritten to contain the global (root) 167 * coordinates of that point. 168 * A <code>null</code> value is valid (in case you are not interested in this result) 169 * @return true if the resulting rectangle is not empty, false otherwise 170 */ getChildVisibleRect(View child, Rect r, android.graphics.Point offset)171 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset); 172 173 /** 174 * Find the nearest view in the specified direction that wants to take focus 175 * 176 * @param v The view that currently has focus 177 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT 178 */ focusSearch(View v, int direction)179 public View focusSearch(View v, int direction); 180 181 /** 182 * Find the nearest keyboard navigation cluster in the specified direction. 183 * This does not actually give focus to that cluster. 184 * 185 * @param currentCluster The starting point of the search. Null means the current cluster is not 186 * found yet 187 * @param direction Direction to look 188 * 189 * @return The nearest keyboard navigation cluster in the specified direction, or null if none 190 * can be found 191 */ keyboardNavigationClusterSearch(View currentCluster, int direction)192 View keyboardNavigationClusterSearch(View currentCluster, int direction); 193 194 /** 195 * Change the z order of the child so it's on top of all other children. 196 * This ordering change may affect layout, if this container 197 * uses an order-dependent layout scheme (e.g., LinearLayout). Prior 198 * to {@link android.os.Build.VERSION_CODES#KITKAT} this 199 * method should be followed by calls to {@link #requestLayout()} and 200 * {@link View#invalidate()} on this parent to force the parent to redraw 201 * with the new child ordering. 202 * 203 * @param child The child to bring to the top of the z order 204 */ bringChildToFront(View child)205 public void bringChildToFront(View child); 206 207 /** 208 * Tells the parent that a new focusable view has become available. This is 209 * to handle transitions from the case where there are no focusable views to 210 * the case where the first focusable view appears. 211 * 212 * @param v The view that has become newly focusable 213 */ focusableViewAvailable(View v)214 public void focusableViewAvailable(View v); 215 216 /** 217 * Shows the context menu for the specified view or its ancestors. 218 * <p> 219 * In most cases, a subclass does not need to override this. However, if 220 * the subclass is added directly to the window manager (for example, 221 * {@link ViewManager#addView(View, android.view.ViewGroup.LayoutParams)}) 222 * then it should override this and show the context menu. 223 * 224 * @param originalView the source view where the context menu was first 225 * invoked 226 * @return {@code true} if the context menu was shown, {@code false} 227 * otherwise 228 * @see #showContextMenuForChild(View, float, float) 229 */ showContextMenuForChild(View originalView)230 public boolean showContextMenuForChild(View originalView); 231 232 /** 233 * Shows the context menu for the specified view or its ancestors anchored 234 * to the specified view-relative coordinate. 235 * <p> 236 * In most cases, a subclass does not need to override this. However, if 237 * the subclass is added directly to the window manager (for example, 238 * {@link ViewManager#addView(View, android.view.ViewGroup.LayoutParams)}) 239 * then it should override this and show the context menu. 240 * <p> 241 * If a subclass overrides this method it should also override 242 * {@link #showContextMenuForChild(View)}. 243 * 244 * @param originalView the source view where the context menu was first 245 * invoked 246 * @param x the X coordinate in pixels relative to the original view to 247 * which the menu should be anchored, or {@link Float#NaN} to 248 * disable anchoring 249 * @param y the Y coordinate in pixels relative to the original view to 250 * which the menu should be anchored, or {@link Float#NaN} to 251 * disable anchoring 252 * @return {@code true} if the context menu was shown, {@code false} 253 * otherwise 254 */ showContextMenuForChild(View originalView, float x, float y)255 boolean showContextMenuForChild(View originalView, float x, float y); 256 257 /** 258 * Have the parent populate the specified context menu if it has anything to 259 * add (and then recurse on its parent). 260 * 261 * @param menu The menu to populate 262 */ createContextMenu(ContextMenu menu)263 public void createContextMenu(ContextMenu menu); 264 265 /** 266 * Start an action mode for the specified view with the default type 267 * {@link ActionMode#TYPE_PRIMARY}. 268 * 269 * <p>In most cases, a subclass does not need to override this. However, if the 270 * subclass is added directly to the window manager (for example, 271 * {@link ViewManager#addView(View, android.view.ViewGroup.LayoutParams)}) 272 * then it should override this and start the action mode.</p> 273 * 274 * @param originalView The source view where the action mode was first invoked 275 * @param callback The callback that will handle lifecycle events for the action mode 276 * @return The new action mode if it was started, null otherwise 277 * 278 * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) 279 */ startActionModeForChild(View originalView, ActionMode.Callback callback)280 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback); 281 282 /** 283 * Start an action mode of a specific type for the specified view. 284 * 285 * <p>In most cases, a subclass does not need to override this. However, if the 286 * subclass is added directly to the window manager (for example, 287 * {@link ViewManager#addView(View, android.view.ViewGroup.LayoutParams)}) 288 * then it should override this and start the action mode.</p> 289 * 290 * @param originalView The source view where the action mode was first invoked 291 * @param callback The callback that will handle lifecycle events for the action mode 292 * @param type One of {@link ActionMode#TYPE_PRIMARY} or {@link ActionMode#TYPE_FLOATING}. 293 * @return The new action mode if it was started, null otherwise 294 */ startActionModeForChild( View originalView, ActionMode.Callback callback, int type)295 public ActionMode startActionModeForChild( 296 View originalView, ActionMode.Callback callback, int type); 297 298 /** 299 * This method is called on the parent when a child's drawable state 300 * has changed. 301 * 302 * @param child The child whose drawable state has changed. 303 */ childDrawableStateChanged(@onNull View child)304 public void childDrawableStateChanged(@NonNull View child); 305 306 /** 307 * Called when a child does not want this parent and its ancestors to 308 * intercept touch events with 309 * {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}. 310 * 311 * <p>This parent should pass this call onto its parents. This parent must obey 312 * this request for the duration of the touch (that is, only clear the flag 313 * after this parent has received an up or a cancel.</p> 314 * 315 * @param disallowIntercept True if the child does not want the parent to 316 * intercept touch events. 317 */ requestDisallowInterceptTouchEvent(boolean disallowIntercept)318 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept); 319 320 /** 321 * Called when a child of this group wants a particular rectangle to be 322 * positioned onto the screen. {@link ViewGroup}s overriding this can trust 323 * that: 324 * <ul> 325 * <li>child will be a direct child of this group</li> 326 * <li>rectangle will be in the child's content coordinates</li> 327 * </ul> 328 * 329 * <p>{@link ViewGroup}s overriding this should uphold the contract:</p> 330 * <ul> 331 * <li>nothing will change if the rectangle is already visible</li> 332 * <li>the view port will be scrolled only just enough to make the 333 * rectangle visible</li> 334 * <ul> 335 * 336 * @param child The direct child making the request. 337 * @param rectangle The rectangle in the child's coordinates the child 338 * wishes to be on the screen. 339 * @param immediate True to forbid animated or delayed scrolling, 340 * false otherwise 341 * @return Whether the group scrolled to handle the operation 342 */ requestChildRectangleOnScreen(@onNull View child, Rect rectangle, boolean immediate)343 public boolean requestChildRectangleOnScreen(@NonNull View child, Rect rectangle, 344 boolean immediate); 345 346 /** 347 * Called by a child to request from its parent to send an {@link AccessibilityEvent}. 348 * The child has already populated a record for itself in the event and is delegating 349 * to its parent to send the event. The parent can optionally add a record for itself. 350 * <p> 351 * Note: An accessibility event is fired by an individual view which populates the 352 * event with a record for its state and requests from its parent to perform 353 * the sending. The parent can optionally add a record for itself before 354 * dispatching the request to its parent. A parent can also choose not to 355 * respect the request for sending the event. The accessibility event is sent 356 * by the topmost view in the view tree.</p> 357 * 358 * @param child The child which requests sending the event. 359 * @param event The event to be sent. 360 * @return True if the event was sent. 361 */ requestSendAccessibilityEvent(@onNull View child, AccessibilityEvent event)362 public boolean requestSendAccessibilityEvent(@NonNull View child, AccessibilityEvent event); 363 364 /** 365 * Called when a child view now has or no longer is tracking transient state. 366 * 367 * <p>"Transient state" is any state that a View might hold that is not expected to 368 * be reflected in the data model that the View currently presents. This state only 369 * affects the presentation to the user within the View itself, such as the current 370 * state of animations in progress or the state of a text selection operation.</p> 371 * 372 * <p>Transient state is useful for hinting to other components of the View system 373 * that a particular view is tracking something complex but encapsulated. 374 * A <code>ListView</code> for example may acknowledge that list item Views 375 * with transient state should be preserved within their position or stable item ID 376 * instead of treating that view as trivially replaceable by the backing adapter. 377 * This allows adapter implementations to be simpler instead of needing to track 378 * the state of item view animations in progress such that they could be restored 379 * in the event of an unexpected recycling and rebinding of attached item views.</p> 380 * 381 * <p>This method is called on a parent view when a child view or a view within 382 * its subtree begins or ends tracking of internal transient state.</p> 383 * 384 * @param child Child view whose state has changed 385 * @param hasTransientState true if this child has transient state 386 */ childHasTransientStateChanged(@onNull View child, boolean hasTransientState)387 public void childHasTransientStateChanged(@NonNull View child, boolean hasTransientState); 388 389 /** 390 * Ask that a new dispatch of {@link View#fitSystemWindows(Rect) 391 * View.fitSystemWindows(Rect)} be performed. 392 */ requestFitSystemWindows()393 public void requestFitSystemWindows(); 394 395 /** 396 * Gets the parent of a given View for accessibility. Since some Views are not 397 * exposed to the accessibility layer the parent for accessibility is not 398 * necessarily the direct parent of the View, rather it is a predecessor. 399 * 400 * @return The parent or <code>null</code> if no such is found. 401 */ getParentForAccessibility()402 public ViewParent getParentForAccessibility(); 403 404 /** 405 * Notifies a view parent that the accessibility state of one of its 406 * descendants has changed and that the structure of the subtree is 407 * different. 408 * @param child The direct child whose subtree has changed. 409 * @param source The descendant view that changed. May not be {@code null}. 410 * @param changeType A bit mask of the types of changes that occurred. One 411 * or more of: 412 * <ul> 413 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION} 414 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_STATE_DESCRIPTION} 415 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_SUBTREE} 416 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_TEXT} 417 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_UNDEFINED} 418 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_DRAG_STARTED} 419 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_DRAG_CANCELLED} 420 * <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_DRAG_DROPPED} 421 * </ul> 422 */ notifySubtreeAccessibilityStateChanged( @onNull View child, @NonNull View source, int changeType)423 public void notifySubtreeAccessibilityStateChanged( 424 @NonNull View child, @NonNull View source, int changeType); 425 426 /** 427 * Tells if this view parent can resolve the layout direction. 428 * See {@link View#setLayoutDirection(int)} 429 * 430 * @return True if this view parent can resolve the layout direction. 431 */ canResolveLayoutDirection()432 public boolean canResolveLayoutDirection(); 433 434 /** 435 * Tells if this view parent layout direction is resolved. 436 * See {@link View#setLayoutDirection(int)} 437 * 438 * @return True if this view parent layout direction is resolved. 439 */ isLayoutDirectionResolved()440 public boolean isLayoutDirectionResolved(); 441 442 /** 443 * Return this view parent layout direction. See {@link View#getLayoutDirection()} 444 * 445 * @return {@link View#LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns 446 * {@link View#LAYOUT_DIRECTION_LTR} if the layout direction is not RTL. 447 */ getLayoutDirection()448 public int getLayoutDirection(); 449 450 /** 451 * Tells if this view parent can resolve the text direction. 452 * See {@link View#setTextDirection(int)} 453 * 454 * @return True if this view parent can resolve the text direction. 455 */ canResolveTextDirection()456 public boolean canResolveTextDirection(); 457 458 /** 459 * Tells if this view parent text direction is resolved. 460 * See {@link View#setTextDirection(int)} 461 * 462 * @return True if this view parent text direction is resolved. 463 */ isTextDirectionResolved()464 public boolean isTextDirectionResolved(); 465 466 /** 467 * Return this view parent text direction. See {@link View#getTextDirection()} 468 * 469 * @return the resolved text direction. Returns one of: 470 * 471 * {@link View#TEXT_DIRECTION_FIRST_STRONG} 472 * {@link View#TEXT_DIRECTION_ANY_RTL}, 473 * {@link View#TEXT_DIRECTION_LTR}, 474 * {@link View#TEXT_DIRECTION_RTL}, 475 * {@link View#TEXT_DIRECTION_LOCALE} 476 */ getTextDirection()477 public int getTextDirection(); 478 479 /** 480 * Tells if this view parent can resolve the text alignment. 481 * See {@link View#setTextAlignment(int)} 482 * 483 * @return True if this view parent can resolve the text alignment. 484 */ canResolveTextAlignment()485 public boolean canResolveTextAlignment(); 486 487 /** 488 * Tells if this view parent text alignment is resolved. 489 * See {@link View#setTextAlignment(int)} 490 * 491 * @return True if this view parent text alignment is resolved. 492 */ isTextAlignmentResolved()493 public boolean isTextAlignmentResolved(); 494 495 /** 496 * Return this view parent text alignment. See {@link android.view.View#getTextAlignment()} 497 * 498 * @return the resolved text alignment. Returns one of: 499 * 500 * {@link View#TEXT_ALIGNMENT_GRAVITY}, 501 * {@link View#TEXT_ALIGNMENT_CENTER}, 502 * {@link View#TEXT_ALIGNMENT_TEXT_START}, 503 * {@link View#TEXT_ALIGNMENT_TEXT_END}, 504 * {@link View#TEXT_ALIGNMENT_VIEW_START}, 505 * {@link View#TEXT_ALIGNMENT_VIEW_END} 506 */ getTextAlignment()507 public int getTextAlignment(); 508 509 /** 510 * React to a descendant view initiating a nestable scroll operation, claiming the 511 * nested scroll operation if appropriate. 512 * 513 * <p>This method will be called in response to a descendant view invoking 514 * {@link View#startNestedScroll(int)}. Each parent up the view hierarchy will be 515 * given an opportunity to respond and claim the nested scrolling operation by returning 516 * <code>true</code>.</p> 517 * 518 * <p>This method may be overridden by ViewParent implementations to indicate when the view 519 * is willing to support a nested scrolling operation that is about to begin. If it returns 520 * true, this ViewParent will become the target view's nested scrolling parent for the duration 521 * of the scroll operation in progress. When the nested scroll is finished this ViewParent 522 * will receive a call to {@link #onStopNestedScroll(View)}. 523 * </p> 524 * 525 * @param child Direct child of this ViewParent containing target 526 * @param target View that initiated the nested scroll 527 * @param nestedScrollAxes Flags consisting of {@link View#SCROLL_AXIS_HORIZONTAL}, 528 * {@link View#SCROLL_AXIS_VERTICAL} or both 529 * @return true if this ViewParent accepts the nested scroll operation 530 */ onStartNestedScroll(@onNull View child, @NonNull View target, int nestedScrollAxes)531 public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, 532 int nestedScrollAxes); 533 534 /** 535 * React to the successful claiming of a nested scroll operation. 536 * 537 * <p>This method will be called after 538 * {@link #onStartNestedScroll(View, View, int) onStartNestedScroll} returns true. It offers 539 * an opportunity for the view and its superclasses to perform initial configuration 540 * for the nested scroll. Implementations of this method should always call their superclass's 541 * implementation of this method if one is present.</p> 542 * 543 * @param child Direct child of this ViewParent containing target 544 * @param target View that initiated the nested scroll 545 * @param nestedScrollAxes Flags consisting of {@link View#SCROLL_AXIS_HORIZONTAL}, 546 * {@link View#SCROLL_AXIS_VERTICAL} or both 547 * @see #onStartNestedScroll(View, View, int) 548 * @see #onStopNestedScroll(View) 549 */ onNestedScrollAccepted(@onNull View child, @NonNull View target, int nestedScrollAxes)550 public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, 551 int nestedScrollAxes); 552 553 /** 554 * React to a nested scroll operation ending. 555 * 556 * <p>Perform cleanup after a nested scrolling operation. 557 * This method will be called when a nested scroll stops, for example when a nested touch 558 * scroll ends with a {@link MotionEvent#ACTION_UP} or {@link MotionEvent#ACTION_CANCEL} event. 559 * Implementations of this method should always call their superclass's implementation of this 560 * method if one is present.</p> 561 * 562 * @param target View that initiated the nested scroll 563 */ onStopNestedScroll(@onNull View target)564 public void onStopNestedScroll(@NonNull View target); 565 566 /** 567 * React to a nested scroll in progress. 568 * 569 * <p>This method will be called when the ViewParent's current nested scrolling child view 570 * dispatches a nested scroll event. To receive calls to this method the ViewParent must have 571 * previously returned <code>true</code> for a call to 572 * {@link #onStartNestedScroll(View, View, int)}.</p> 573 * 574 * <p>Both the consumed and unconsumed portions of the scroll distance are reported to the 575 * ViewParent. An implementation may choose to use the consumed portion to match or chase scroll 576 * position of multiple child elements, for example. The unconsumed portion may be used to 577 * allow continuous dragging of multiple scrolling or draggable elements, such as scrolling 578 * a list within a vertical drawer where the drawer begins dragging once the edge of inner 579 * scrolling content is reached.</p> 580 * 581 * @param target The descendent view controlling the nested scroll 582 * @param dxConsumed Horizontal scroll distance in pixels already consumed by target 583 * @param dyConsumed Vertical scroll distance in pixels already consumed by target 584 * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by target 585 * @param dyUnconsumed Vertical scroll distance in pixels not consumed by target 586 */ onNestedScroll(@onNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)587 public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, 588 int dxUnconsumed, int dyUnconsumed); 589 590 /** 591 * React to a nested scroll in progress before the target view consumes a portion of the scroll. 592 * 593 * <p>When working with nested scrolling often the parent view may want an opportunity 594 * to consume the scroll before the nested scrolling child does. An example of this is a 595 * drawer that contains a scrollable list. The user will want to be able to scroll the list 596 * fully into view before the list itself begins scrolling.</p> 597 * 598 * <p><code>onNestedPreScroll</code> is called when a nested scrolling child invokes 599 * {@link View#dispatchNestedPreScroll(int, int, int[], int[])}. The implementation should 600 * report how any pixels of the scroll reported by dx, dy were consumed in the 601 * <code>consumed</code> array. Index 0 corresponds to dx and index 1 corresponds to dy. 602 * This parameter will never be null. Initial values for consumed[0] and consumed[1] 603 * will always be 0.</p> 604 * 605 * @param target View that initiated the nested scroll 606 * @param dx Horizontal scroll distance in pixels 607 * @param dy Vertical scroll distance in pixels 608 * @param consumed Output. The horizontal and vertical scroll distance consumed by this parent 609 */ onNestedPreScroll(@onNull View target, int dx, int dy, @NonNull int[] consumed)610 public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed); 611 612 /** 613 * Request a fling from a nested scroll. 614 * 615 * <p>This method signifies that a nested scrolling child has detected suitable conditions 616 * for a fling. Generally this means that a touch scroll has ended with a 617 * {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds 618 * the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity} 619 * along a scrollable axis.</p> 620 * 621 * <p>If a nested scrolling child view would normally fling but it is at the edge of 622 * its own content, it can use this method to delegate the fling to its nested scrolling 623 * parent instead. The parent may optionally consume the fling or observe a child fling.</p> 624 * 625 * @param target View that initiated the nested scroll 626 * @param velocityX Horizontal velocity in pixels per second 627 * @param velocityY Vertical velocity in pixels per second 628 * @param consumed true if the child consumed the fling, false otherwise 629 * @return true if this parent consumed or otherwise reacted to the fling 630 */ onNestedFling(@onNull View target, float velocityX, float velocityY, boolean consumed)631 public boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, 632 boolean consumed); 633 634 /** 635 * React to a nested fling before the target view consumes it. 636 * 637 * <p>This method siginfies that a nested scrolling child has detected a fling with the given 638 * velocity along each axis. Generally this means that a touch scroll has ended with a 639 * {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds 640 * the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity} 641 * along a scrollable axis.</p> 642 * 643 * <p>If a nested scrolling parent is consuming motion as part of a 644 * {@link #onNestedPreScroll(View, int, int, int[]) pre-scroll}, it may be appropriate for 645 * it to also consume the pre-fling to complete that same motion. By returning 646 * <code>true</code> from this method, the parent indicates that the child should not 647 * fling its own internal content as well.</p> 648 * 649 * @param target View that initiated the nested scroll 650 * @param velocityX Horizontal velocity in pixels per second 651 * @param velocityY Vertical velocity in pixels per second 652 * @return true if this parent consumed the fling ahead of the target view 653 */ onNestedPreFling(@onNull View target, float velocityX, float velocityY)654 public boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY); 655 656 /** 657 * React to an accessibility action delegated by a target descendant view before the target 658 * processes it. 659 * 660 * <p>This method may be called by a target descendant view if the target wishes to give 661 * a view in its parent chain a chance to react to the event before normal processing occurs. 662 * Most commonly this will be a scroll event such as 663 * {@link android.view.accessibility.AccessibilityNodeInfo#ACTION_SCROLL_FORWARD}. 664 * A ViewParent that supports acting as a nested scrolling parent should override this 665 * method and act accordingly to implement scrolling via accesibility systems.</p> 666 * 667 * @param target The target view dispatching this action 668 * @param action Action being performed; see 669 * {@link android.view.accessibility.AccessibilityNodeInfo} 670 * @param arguments Optional action arguments 671 * @return true if the action was consumed by this ViewParent 672 */ onNestedPrePerformAccessibilityAction(@onNull View target, int action, @Nullable Bundle arguments)673 public boolean onNestedPrePerformAccessibilityAction(@NonNull View target, int action, 674 @Nullable Bundle arguments); 675 676 /** 677 * Given a touchable region of a child, this method reduces region by the bounds of all views on 678 * top of the child for which {@link View#canReceivePointerEvents} returns {@code true}. This 679 * applies recursively for all views in the view hierarchy on top of this one. 680 * 681 * @param touchableRegion The touchable region we want to modify. 682 * @param view A child view of this ViewGroup which indicates the z-order of the touchable 683 * region. 684 * @hide 685 */ subtractObscuredTouchableRegion(Region touchableRegion, View view)686 default void subtractObscuredTouchableRegion(Region touchableRegion, View view) { 687 } 688 689 /** 690 * Compute the region where the child can receive the {@link MotionEvent}s from the root view. 691 * 692 * <p> Given region where the child will accept {@link MotionEvent}s. 693 * Modify the region to the unblocked region where the child can receive the 694 * {@link MotionEvent}s from the view root. 695 * </p> 696 * 697 * <p> The given region is always clipped by the bounds of the parent views. When there are 698 * on-going {@link MotionEvent}s, this method also makes use of the event dispatching results to 699 * determine whether a sibling view will also block the child's hit region. 700 * </p> 701 * 702 * @param child a child View, whose hit region we want to compute. 703 * @param region the initial hit region where the child view will handle {@link MotionEvent}s, 704 * defined in the child coordinates. Will be overwritten to the result hit region. 705 * @param matrix the matrix that maps the given child view's coordinates to the region 706 * coordinates. It will be modified to a matrix that maps window coordinates to 707 * the result region's coordinates. 708 * @param isHover if true it will return the hover events' hit region, otherwise it will 709 * return the touch events' hit region. 710 * @return true if the returned region is not empty. 711 * @hide 712 */ getChildLocalHitRegion(@onNull View child, @NonNull Region region, @NonNull Matrix matrix, boolean isHover)713 default boolean getChildLocalHitRegion(@NonNull View child, @NonNull Region region, 714 @NonNull Matrix matrix, boolean isHover) { 715 region.setEmpty(); 716 return false; 717 } 718 719 /** 720 * Unbuffered dispatch has been requested by a child of this view parent. 721 * This method is called by the View hierarchy to signal ancestors that a View needs to 722 * request unbuffered dispatch. 723 * 724 * @see View#requestUnbufferedDispatch(int) 725 * @hide 726 */ onDescendantUnbufferedRequested()727 default void onDescendantUnbufferedRequested() { 728 if (getParent() != null) { 729 getParent().onDescendantUnbufferedRequested(); 730 } 731 } 732 733 /** 734 * Walk up the View hierarchy to find the nearest {@link OnBackInvokedDispatcher}. 735 * 736 * @return The {@link OnBackInvokedDispatcher} from this or the nearest 737 * ancestor, or null if the view is both not attached and have no ancestor providing an 738 * {@link OnBackInvokedDispatcher}. 739 * 740 * @param child The direct child of this view for which to find a dispatcher. 741 * @param requester The requester that will use the dispatcher. Can be the same as child. 742 */ 743 @Nullable findOnBackInvokedDispatcherForChild(@onNull View child, @NonNull View requester)744 default OnBackInvokedDispatcher findOnBackInvokedDispatcherForChild(@NonNull View child, 745 @NonNull View requester) { 746 return null; 747 } 748 } 749