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