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