NestedScrollingChild2.java revision ac5fe7c617c66850fff75a9fce9979c6e5674b0f
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 18package androidx.core.view; 19 20import androidx.annotation.Nullable; 21import androidx.core.view.ViewCompat.NestedScrollType; 22import androidx.core.view.ViewCompat.ScrollAxis; 23import android.view.MotionEvent; 24import android.view.View; 25import android.view.ViewParent; 26 27/** 28 * This interface should be implemented by {@link View View} subclasses that wish 29 * to support dispatching nested scrolling operations to a cooperating parent 30 * {@link android.view.ViewGroup ViewGroup}. 31 * 32 * <p>Classes implementing this interface should create a final instance of a 33 * {@link NestedScrollingChildHelper} as a field and delegate any View methods to the 34 * <code>NestedScrollingChildHelper</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 all versions 39 * of Android.</p> 40 */ 41public interface NestedScrollingChild2 extends NestedScrollingChild { 42 43 /** 44 * Begin a nestable scroll operation along the given axes, for the given input type. 45 * 46 * <p>A view starting a nested scroll promises to abide by the following contract:</p> 47 * 48 * <p>The view will call startNestedScroll upon initiating a scroll operation. In the case 49 * of a touch scroll type this corresponds to the initial {@link MotionEvent#ACTION_DOWN}. 50 * In the case of touch scrolling the nested scroll will be terminated automatically in 51 * the same manner as {@link ViewParent#requestDisallowInterceptTouchEvent(boolean)}. 52 * In the event of programmatic scrolling the caller must explicitly call 53 * {@link #stopNestedScroll(int)} to indicate the end of the nested scroll.</p> 54 * 55 * <p>If <code>startNestedScroll</code> returns true, a cooperative parent was found. 56 * If it returns false the caller may ignore the rest of this contract until the next scroll. 57 * Calling startNestedScroll while a nested scroll is already in progress will return true.</p> 58 * 59 * <p>At each incremental step of the scroll the caller should invoke 60 * {@link #dispatchNestedPreScroll(int, int, int[], int[], int) dispatchNestedPreScroll} 61 * once it has calculated the requested scrolling delta. If it returns true the nested scrolling 62 * parent at least partially consumed the scroll and the caller should adjust the amount it 63 * scrolls by.</p> 64 * 65 * <p>After applying the remainder of the scroll delta the caller should invoke 66 * {@link #dispatchNestedScroll(int, int, int, int, int[], int) dispatchNestedScroll}, passing 67 * both the delta consumed and the delta unconsumed. A nested scrolling parent may treat 68 * these values differently. See 69 * {@link NestedScrollingParent2#onNestedScroll(View, int, int, int, int, int)}. 70 * </p> 71 * 72 * @param axes Flags consisting of a combination of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL} 73 * and/or {@link ViewCompat#SCROLL_AXIS_VERTICAL}. 74 * @param type the type of input which cause this scroll event 75 * @return true if a cooperative parent was found and nested scrolling has been enabled for 76 * the current gesture. 77 * 78 * @see #stopNestedScroll(int) 79 * @see #dispatchNestedPreScroll(int, int, int[], int[], int) 80 * @see #dispatchNestedScroll(int, int, int, int, int[], int) 81 */ 82 boolean startNestedScroll(@ScrollAxis int axes, @NestedScrollType int type); 83 84 /** 85 * Stop a nested scroll in progress for the given input type. 86 * 87 * <p>Calling this method when a nested scroll is not currently in progress is harmless.</p> 88 * 89 * @param type the type of input which cause this scroll event 90 * @see #startNestedScroll(int, int) 91 */ 92 void stopNestedScroll(@NestedScrollType int type); 93 94 /** 95 * Returns true if this view has a nested scrolling parent for the given input type. 96 * 97 * <p>The presence of a nested scrolling parent indicates that this view has initiated 98 * a nested scroll and it was accepted by an ancestor view further up the view hierarchy.</p> 99 * 100 * @param type the type of input which cause this scroll event 101 * 102 * @return whether this view has a nested scrolling parent 103 */ 104 boolean hasNestedScrollingParent(@NestedScrollType int type); 105 106 /** 107 * Dispatch one step of a nested scroll in progress. 108 * 109 * <p>Implementations of views that support nested scrolling should call this to report 110 * info about a scroll in progress to the current nested scrolling parent. If a nested scroll 111 * is not currently in progress or nested scrolling is not 112 * {@link #isNestedScrollingEnabled() enabled} for this view this method does nothing.</p> 113 * 114 * <p>Compatible View implementations should also call 115 * {@link #dispatchNestedPreScroll(int, int, int[], int[], int) dispatchNestedPreScroll} before 116 * consuming a component of the scroll event themselves.</p> 117 * 118 * @param dxConsumed Horizontal distance in pixels consumed by this view during this scroll step 119 * @param dyConsumed Vertical distance in pixels consumed by this view during this scroll step 120 * @param dxUnconsumed Horizontal scroll distance in pixels not consumed by this view 121 * @param dyUnconsumed Horizontal scroll distance in pixels not consumed by this view 122 * @param offsetInWindow Optional. If not null, on return this will contain the offset 123 * in local view coordinates of this view from before this operation 124 * to after it completes. View implementations may use this to adjust 125 * expected input coordinate tracking. 126 * @param type the type of input which cause this scroll event 127 * @return true if the event was dispatched, false if it could not be dispatched. 128 * @see #dispatchNestedPreScroll(int, int, int[], int[], int) 129 */ 130 boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, 131 int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow, 132 @NestedScrollType int type); 133 134 /** 135 * Dispatch one step of a nested scroll in progress before this view consumes any portion of it. 136 * 137 * <p>Nested pre-scroll events are to nested scroll events what touch intercept is to touch. 138 * <code>dispatchNestedPreScroll</code> offers an opportunity for the parent view in a nested 139 * scrolling operation to consume some or all of the scroll operation before the child view 140 * consumes it.</p> 141 * 142 * @param dx Horizontal scroll distance in pixels 143 * @param dy Vertical scroll distance in pixels 144 * @param consumed Output. If not null, consumed[0] will contain the consumed component of dx 145 * and consumed[1] the consumed dy. 146 * @param offsetInWindow Optional. If not null, on return this will contain the offset 147 * in local view coordinates of this view from before this operation 148 * to after it completes. View implementations may use this to adjust 149 * expected input coordinate tracking. 150 * @param type the type of input which cause this scroll event 151 * @return true if the parent consumed some or all of the scroll delta 152 * @see #dispatchNestedScroll(int, int, int, int, int[], int) 153 */ 154 boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, 155 @Nullable int[] offsetInWindow, @NestedScrollType int type); 156} 157