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