1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 package androidx.core.view; 19 20 import android.view.MotionEvent; 21 import android.view.VelocityTracker; 22 import android.view.View; 23 import android.view.ViewConfiguration; 24 25 import androidx.annotation.NonNull; 26 import androidx.core.view.ViewCompat.ScrollAxis; 27 28 /** 29 * This interface should be implemented by {@link android.view.ViewGroup ViewGroup} subclasses 30 * that wish to support scrolling operations delegated by a nested child view. 31 * 32 * <p>Classes implementing this interface should create a final instance of a 33 * {@link NestedScrollingParentHelper} as a field and delegate any View or ViewGroup methods 34 * to the <code>NestedScrollingParentHelper</code> methods of the same signature.</p> 35 * 36 * <p>Views invoking nested scrolling functionality should always do so from the relevant 37 * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility 38 * shim static methods. This ensures interoperability with nested scrolling views on Android 39 * 5.0 Lollipop and newer.</p> 40 */ 41 public interface NestedScrollingParent { 42 /** 43 * React to a descendant view initiating a nestable scroll operation, claiming the 44 * nested scroll operation if appropriate. 45 * 46 * <p>This method will be called in response to a descendant view invoking 47 * {@link ViewCompat#startNestedScroll(View, int)}. Each parent up the view hierarchy will be 48 * given an opportunity to respond and claim the nested scrolling operation by returning 49 * <code>true</code>.</p> 50 * 51 * <p>This method may be overridden by ViewParent implementations to indicate when the view 52 * is willing to support a nested scrolling operation that is about to begin. If it returns 53 * true, this ViewParent will become the target view's nested scrolling parent for the duration 54 * of the scroll operation in progress. When the nested scroll is finished this ViewParent 55 * will receive a call to {@link #onStopNestedScroll(View)}. 56 * </p> 57 * 58 * @param child Direct child of this ViewParent containing target 59 * @param target View that initiated the nested scroll 60 * @param axes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}, 61 * {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both 62 * @return true if this ViewParent accepts the nested scroll operation 63 */ onStartNestedScroll(@onNull View child, @NonNull View target, @ScrollAxis int axes)64 boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes); 65 66 /** 67 * React to the successful claiming of a nested scroll operation. 68 * 69 * <p>This method will be called after 70 * {@link #onStartNestedScroll(View, View, int) onStartNestedScroll} returns true. It offers 71 * an opportunity for the view and its superclasses to perform initial configuration 72 * for the nested scroll. Implementations of this method should always call their superclass's 73 * implementation of this method if one is present.</p> 74 * 75 * @param child Direct child of this ViewParent containing target 76 * @param target View that initiated the nested scroll 77 * @param axes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}, 78 * {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both 79 * @see #onStartNestedScroll(View, View, int) 80 * @see #onStopNestedScroll(View) 81 */ onNestedScrollAccepted(@onNull View child, @NonNull View target, @ScrollAxis int axes)82 void onNestedScrollAccepted(@NonNull View child, @NonNull View target, @ScrollAxis int axes); 83 84 /** 85 * React to a nested scroll operation ending. 86 * 87 * <p>Perform cleanup after a nested scrolling operation. 88 * This method will be called when a nested scroll stops, for example when a nested touch 89 * scroll ends with a {@link MotionEvent#ACTION_UP} or {@link MotionEvent#ACTION_CANCEL} event. 90 * Implementations of this method should always call their superclass's implementation of this 91 * method if one is present.</p> 92 * 93 * @param target View that initiated the nested scroll 94 */ onStopNestedScroll(@onNull View target)95 void onStopNestedScroll(@NonNull View target); 96 97 /** 98 * React to a nested scroll in progress. 99 * 100 * <p>This method will be called when the ViewParent's current nested scrolling child view 101 * dispatches a nested scroll event. To receive calls to this method the ViewParent must have 102 * previously returned <code>true</code> for a call to 103 * {@link #onStartNestedScroll(View, View, int)}.</p> 104 * 105 * <p>Both the consumed and unconsumed portions of the scroll distance are reported to the 106 * ViewParent. An implementation may choose to use the consumed portion to match or chase scroll 107 * position of multiple child elements, for example. The unconsumed portion may be used to 108 * allow continuous dragging of multiple scrolling or draggable elements, such as scrolling 109 * a list within a vertical drawer where the drawer begins dragging once the edge of inner 110 * scrolling content is reached.</p> 111 * 112 * @param target The descendent view controlling the nested scroll 113 * @param dxConsumed Horizontal scroll distance in pixels already consumed by target 114 * @param dyConsumed Vertical scroll distance in pixels already consumed by target 115 * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by target 116 * @param dyUnconsumed Vertical scroll distance in pixels not consumed by target 117 */ onNestedScroll(@onNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)118 void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, 119 int dxUnconsumed, int dyUnconsumed); 120 121 /** 122 * React to a nested scroll in progress before the target view consumes a portion of the scroll. 123 * 124 * <p>When working with nested scrolling often the parent view may want an opportunity 125 * to consume the scroll before the nested scrolling child does. An example of this is a 126 * drawer that contains a scrollable list. The user will want to be able to scroll the list 127 * fully into view before the list itself begins scrolling.</p> 128 * 129 * <p><code>onNestedPreScroll</code> is called when a nested scrolling child invokes 130 * {@link View#dispatchNestedPreScroll(int, int, int[], int[])}. The implementation should 131 * report how any pixels of the scroll reported by dx, dy were consumed in the 132 * <code>consumed</code> array. Index 0 corresponds to dx and index 1 corresponds to dy. 133 * This parameter will never be null. Initial values for consumed[0] and consumed[1] 134 * will always be 0.</p> 135 * 136 * @param target View that initiated the nested scroll 137 * @param dx Horizontal scroll distance in pixels 138 * @param dy Vertical scroll distance in pixels 139 * @param consumed Output. The horizontal and vertical scroll distance consumed by this parent 140 */ onNestedPreScroll(@onNull View target, int dx, int dy, @NonNull int[] consumed)141 void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed); 142 143 /** 144 * Request a fling from a nested scroll. 145 * 146 * <p>This method signifies that a nested scrolling child has detected suitable conditions 147 * for a fling. Generally this means that a touch scroll has ended with a 148 * {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds 149 * the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity} 150 * along a scrollable axis.</p> 151 * 152 * <p>If a nested scrolling child view would normally fling but it is at the edge of 153 * its own content, it can use this method to delegate the fling to its nested scrolling 154 * parent instead. The parent may optionally consume the fling or observe a child fling.</p> 155 * 156 * @param target View that initiated the nested scroll 157 * @param velocityX Horizontal velocity in pixels per second 158 * @param velocityY Vertical velocity in pixels per second 159 * @param consumed true if the child consumed the fling, false otherwise 160 * @return true if this parent consumed or otherwise reacted to the fling 161 */ onNestedFling(@onNull View target, float velocityX, float velocityY, boolean consumed)162 boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed); 163 164 /** 165 * React to a nested fling before the target view consumes it. 166 * 167 * <p>This method siginfies that a nested scrolling child has detected a fling with the given 168 * velocity along each axis. Generally this means that a touch scroll has ended with a 169 * {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds 170 * the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity} 171 * along a scrollable axis.</p> 172 * 173 * <p>If a nested scrolling parent is consuming motion as part of a 174 * {@link #onNestedPreScroll(View, int, int, int[]) pre-scroll}, it may be appropriate for 175 * it to also consume the pre-fling to complete that same motion. By returning 176 * <code>true</code> from this method, the parent indicates that the child should not 177 * fling its own internal content as well.</p> 178 * 179 * @param target View that initiated the nested scroll 180 * @param velocityX Horizontal velocity in pixels per second 181 * @param velocityY Vertical velocity in pixels per second 182 * @return true if this parent consumed the fling ahead of the target view 183 */ onNestedPreFling(@onNull View target, float velocityX, float velocityY)184 boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY); 185 186 /** 187 * Return the current axes of nested scrolling for this NestedScrollingParent. 188 * 189 * <p>A NestedScrollingParent returning something other than {@link ViewCompat#SCROLL_AXIS_NONE} 190 * is currently acting as a nested scrolling parent for one or more descendant views in 191 * the hierarchy.</p> 192 * 193 * @return Flags indicating the current axes of nested scrolling 194 * @see ViewCompat#SCROLL_AXIS_HORIZONTAL 195 * @see ViewCompat#SCROLL_AXIS_VERTICAL 196 * @see ViewCompat#SCROLL_AXIS_NONE 197 */ 198 @ScrollAxis getNestedScrollAxes()199 int getNestedScrollAxes(); 200 } 201