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 18package android.support.v4.view; 19 20import android.support.annotation.NonNull; 21import android.support.v4.view.ViewCompat.NestedScrollType; 22import android.support.v4.view.ViewCompat.ScrollAxis; 23import android.view.View; 24import android.view.ViewGroup; 25 26/** 27 * Helper class for implementing nested scrolling parent views compatible with Android platform 28 * versions earlier than Android 5.0 Lollipop (API 21). 29 * 30 * <p>{@link android.view.ViewGroup ViewGroup} subclasses should instantiate a final instance 31 * of this class as a field at construction. For each <code>ViewGroup</code> method that has 32 * a matching method signature in this class, delegate the operation to the helper instance 33 * in an overridden method implementation. This implements the standard framework policy 34 * for nested scrolling.</p> 35 * 36 * <p>Views invoking nested scrolling functionality should always do so from the relevant 37 * {@link android.support.v4.view.ViewCompat}, {@link android.support.v4.view.ViewGroupCompat} or 38 * {@link android.support.v4.view.ViewParentCompat} compatibility 39 * shim static methods. This ensures interoperability with nested scrolling views on Android 40 * 5.0 Lollipop and newer.</p> 41 */ 42public class NestedScrollingParentHelper { 43 private final ViewGroup mViewGroup; 44 private int mNestedScrollAxes; 45 46 /** 47 * Construct a new helper for a given ViewGroup 48 */ 49 public NestedScrollingParentHelper(@NonNull ViewGroup viewGroup) { 50 mViewGroup = viewGroup; 51 } 52 53 /** 54 * Called when a nested scrolling operation initiated by a descendant view is accepted 55 * by this ViewGroup. 56 * 57 * <p>This is a delegate method. Call it from your {@link android.view.ViewGroup ViewGroup} 58 * subclass method/{@link android.support.v4.view.NestedScrollingParent} interface method with 59 * the same signature to implement the standard policy.</p> 60 */ 61 public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, 62 @ScrollAxis int axes) { 63 onNestedScrollAccepted(child, target, axes, ViewCompat.TYPE_TOUCH); 64 } 65 66 /** 67 * Called when a nested scrolling operation initiated by a descendant view is accepted 68 * by this ViewGroup. 69 * 70 * <p>This is a delegate method. Call it from your {@link android.view.ViewGroup ViewGroup} 71 * subclass method/{@link android.support.v4.view.NestedScrollingParent2} interface method with 72 * the same signature to implement the standard policy.</p> 73 */ 74 public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, 75 @ScrollAxis int axes, @NestedScrollType int type) { 76 mNestedScrollAxes = axes; 77 } 78 79 /** 80 * Return the current axes of nested scrolling for this ViewGroup. 81 * 82 * <p>This is a delegate method. Call it from your {@link android.view.ViewGroup ViewGroup} 83 * subclass method/{@link android.support.v4.view.NestedScrollingParent} interface method with 84 * the same signature to implement the standard policy.</p> 85 */ 86 @ScrollAxis 87 public int getNestedScrollAxes() { 88 return mNestedScrollAxes; 89 } 90 91 /** 92 * React to a nested scroll operation ending. 93 * 94 * <p>This is a delegate method. Call it from your {@link android.view.ViewGroup ViewGroup} 95 * subclass method/{@link android.support.v4.view.NestedScrollingParent} interface method with 96 * the same signature to implement the standard policy.</p> 97 */ 98 public void onStopNestedScroll(@NonNull View target) { 99 onStopNestedScroll(target, ViewCompat.TYPE_TOUCH); 100 } 101 102 /** 103 * React to a nested scroll operation ending. 104 * 105 * <p>This is a delegate method. Call it from your {@link android.view.ViewGroup ViewGroup} 106 * subclass method/{@link android.support.v4.view.NestedScrollingParent2} interface method with 107 * the same signature to implement the standard policy.</p> 108 */ 109 public void onStopNestedScroll(@NonNull View target, @NestedScrollType int type) { 110 mNestedScrollAxes = 0; 111 } 112} 113