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