• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.view.ViewParent;
25 
26 import androidx.annotation.Nullable;
27 import androidx.core.view.ViewCompat.ScrollAxis;
28 
29 /**
30  * This interface should be implemented by {@link android.view.View View} subclasses that wish
31  * to support dispatching nested scrolling operations to a cooperating parent
32  * {@link android.view.ViewGroup ViewGroup}.
33  *
34  * <p>Classes implementing this interface should create a final instance of a
35  * {@link NestedScrollingChildHelper} as a field and delegate any View methods to the
36  * <code>NestedScrollingChildHelper</code> methods of the same signature.</p>
37  *
38  * <p>Views invoking nested scrolling functionality should always do so from the relevant
39  * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility
40  * shim static methods. This ensures interoperability with nested scrolling views on Android
41  * 5.0 Lollipop and newer.</p>
42  */
43 public interface NestedScrollingChild {
44     /**
45      * Enable or disable nested scrolling for this view.
46      *
47      * <p>If this property is set to true the view will be permitted to initiate nested
48      * scrolling operations with a compatible parent view in the current hierarchy. If this
49      * view does not implement nested scrolling this will have no effect. Disabling nested scrolling
50      * while a nested scroll is in progress has the effect of {@link #stopNestedScroll() stopping}
51      * the nested scroll.</p>
52      *
53      * @param enabled true to enable nested scrolling, false to disable
54      *
55      * @see #isNestedScrollingEnabled()
56      */
setNestedScrollingEnabled(boolean enabled)57     void setNestedScrollingEnabled(boolean enabled);
58 
59     /**
60      * Returns true if nested scrolling is enabled for this view.
61      *
62      * <p>If nested scrolling is enabled and this View class implementation supports it,
63      * this view will act as a nested scrolling child view when applicable, forwarding data
64      * about the scroll operation in progress to a compatible and cooperating nested scrolling
65      * parent.</p>
66      *
67      * @return true if nested scrolling is enabled
68      *
69      * @see #setNestedScrollingEnabled(boolean)
70      */
isNestedScrollingEnabled()71     boolean isNestedScrollingEnabled();
72 
73     /**
74      * Begin a nestable scroll operation along the given axes.
75      *
76      * <p>A view starting a nested scroll promises to abide by the following contract:</p>
77      *
78      * <p>The view will call startNestedScroll upon initiating a scroll operation. In the case
79      * of a touch scroll this corresponds to the initial {@link MotionEvent#ACTION_DOWN}.
80      * In the case of touch scrolling the nested scroll will be terminated automatically in
81      * the same manner as {@link ViewParent#requestDisallowInterceptTouchEvent(boolean)}.
82      * In the event of programmatic scrolling the caller must explicitly call
83      * {@link #stopNestedScroll()} to indicate the end of the nested scroll.</p>
84      *
85      * <p>If <code>startNestedScroll</code> returns true, a cooperative parent was found.
86      * If it returns false the caller may ignore the rest of this contract until the next scroll.
87      * Calling startNestedScroll while a nested scroll is already in progress will return true.</p>
88      *
89      * <p>At each incremental step of the scroll the caller should invoke
90      * {@link #dispatchNestedPreScroll(int, int, int[], int[]) dispatchNestedPreScroll}
91      * once it has calculated the requested scrolling delta. If it returns true the nested scrolling
92      * parent at least partially consumed the scroll and the caller should adjust the amount it
93      * scrolls by.</p>
94      *
95      * <p>After applying the remainder of the scroll delta the caller should invoke
96      * {@link #dispatchNestedScroll(int, int, int, int, int[]) dispatchNestedScroll}, passing
97      * both the delta consumed and the delta unconsumed. A nested scrolling parent may treat
98      * these values differently. See
99      * {@link NestedScrollingParent#onNestedScroll(View, int, int, int, int)}.
100      * </p>
101      *
102      * @param axes Flags consisting of a combination of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}
103      *             and/or {@link ViewCompat#SCROLL_AXIS_VERTICAL}.
104      * @return true if a cooperative parent was found and nested scrolling has been enabled for
105      *         the current gesture.
106      *
107      * @see #stopNestedScroll()
108      * @see #dispatchNestedPreScroll(int, int, int[], int[])
109      * @see #dispatchNestedScroll(int, int, int, int, int[])
110      */
startNestedScroll(@crollAxis int axes)111     boolean startNestedScroll(@ScrollAxis int axes);
112 
113     /**
114      * Stop a nested scroll in progress.
115      *
116      * <p>Calling this method when a nested scroll is not currently in progress is harmless.</p>
117      *
118      * @see #startNestedScroll(int)
119      */
stopNestedScroll()120     void stopNestedScroll();
121 
122     /**
123      * Returns true if this view has a nested scrolling parent.
124      *
125      * <p>The presence of a nested scrolling parent indicates that this view has initiated
126      * a nested scroll and it was accepted by an ancestor view further up the view hierarchy.</p>
127      *
128      * @return whether this view has a nested scrolling parent
129      */
hasNestedScrollingParent()130     boolean hasNestedScrollingParent();
131 
132     /**
133      * Dispatch one step of a nested scroll in progress.
134      *
135      * <p>Implementations of views that support nested scrolling should call this to report
136      * info about a scroll in progress to the current nested scrolling parent. If a nested scroll
137      * is not currently in progress or nested scrolling is not
138      * {@link #isNestedScrollingEnabled() enabled} for this view this method does nothing.</p>
139      *
140      * <p>Compatible View implementations should also call
141      * {@link #dispatchNestedPreScroll(int, int, int[], int[]) dispatchNestedPreScroll} before
142      * consuming a component of the scroll event themselves.</p>
143      *
144      * @param dxConsumed Horizontal distance in pixels consumed by this view during this scroll step
145      * @param dyConsumed Vertical distance in pixels consumed by this view during this scroll step
146      * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by this view
147      * @param dyUnconsumed Horizontal scroll distance in pixels not consumed by this view
148      * @param offsetInWindow Optional. If not null, on return this will contain the offset
149      *                       in local view coordinates of this view from before this operation
150      *                       to after it completes. View implementations may use this to adjust
151      *                       expected input coordinate tracking.
152      * @return true if the event was dispatched, false if it could not be dispatched.
153      * @see #dispatchNestedPreScroll(int, int, int[], int[])
154      */
dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow)155     boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
156             int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow);
157 
158     /**
159      * Dispatch one step of a nested scroll in progress before this view consumes any portion of it.
160      *
161      * <p>Nested pre-scroll events are to nested scroll events what touch intercept is to touch.
162      * <code>dispatchNestedPreScroll</code> offers an opportunity for the parent view in a nested
163      * scrolling operation to consume some or all of the scroll operation before the child view
164      * consumes it.</p>
165      *
166      * @param dx Horizontal scroll distance in pixels
167      * @param dy Vertical scroll distance in pixels
168      * @param consumed Output. If not null, consumed[0] will contain the consumed component of dx
169      *                 and consumed[1] the consumed dy.
170      * @param offsetInWindow Optional. If not null, on return this will contain the offset
171      *                       in local view coordinates of this view from before this operation
172      *                       to after it completes. View implementations may use this to adjust
173      *                       expected input coordinate tracking.
174      * @return true if the parent consumed some or all of the scroll delta
175      * @see #dispatchNestedScroll(int, int, int, int, int[])
176      */
dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow)177     boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed,
178             @Nullable int[] offsetInWindow);
179 
180     /**
181      * Dispatch a fling to a nested scrolling parent.
182      *
183      * <p>This method should be used to indicate that a nested scrolling child has detected
184      * suitable conditions for a fling. Generally this means that a touch scroll has ended with a
185      * {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds
186      * the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity}
187      * along a scrollable axis.</p>
188      *
189      * <p>If a nested scrolling child view would normally fling but it is at the edge of
190      * its own content, it can use this method to delegate the fling to its nested scrolling
191      * parent instead. The parent may optionally consume the fling or observe a child fling.</p>
192      *
193      * @param velocityX Horizontal fling velocity in pixels per second
194      * @param velocityY Vertical fling velocity in pixels per second
195      * @param consumed true if the child consumed the fling, false otherwise
196      * @return true if the nested scrolling parent consumed or otherwise reacted to the fling
197      */
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)198     boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);
199 
200     /**
201      * Dispatch a fling to a nested scrolling parent before it is processed by this view.
202      *
203      * <p>Nested pre-fling events are to nested fling events what touch intercept is to touch
204      * and what nested pre-scroll is to nested scroll. <code>dispatchNestedPreFling</code>
205      * offsets an opportunity for the parent view in a nested fling to fully consume the fling
206      * before the child view consumes it. If this method returns <code>true</code>, a nested
207      * parent view consumed the fling and this view should not scroll as a result.</p>
208      *
209      * <p>For a better user experience, only one view in a nested scrolling chain should consume
210      * the fling at a time. If a parent view consumed the fling this method will return false.
211      * Custom view implementations should account for this in two ways:</p>
212      *
213      * <ul>
214      *     <li>If a custom view is paged and needs to settle to a fixed page-point, do not
215      *     call <code>dispatchNestedPreFling</code>; consume the fling and settle to a valid
216      *     position regardless.</li>
217      *     <li>If a nested parent does consume the fling, this view should not scroll at all,
218      *     even to settle back to a valid idle position.</li>
219      * </ul>
220      *
221      * <p>Views should also not offer fling velocities to nested parent views along an axis
222      * where scrolling is not currently supported; a {@link android.widget.ScrollView ScrollView}
223      * should not offer a horizontal fling velocity to its parents since scrolling along that
224      * axis is not permitted and carrying velocity along that motion does not make sense.</p>
225      *
226      * @param velocityX Horizontal fling velocity in pixels per second
227      * @param velocityY Vertical fling velocity in pixels per second
228      * @return true if a nested scrolling parent consumed the fling
229      */
dispatchNestedPreFling(float velocityX, float velocityY)230     boolean dispatchNestedPreFling(float velocityX, float velocityY);
231 }
232