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 package android.support.v7.widget; 18 19 import android.content.Context; 20 import android.support.v4.view.NestedScrollingParent; 21 import android.support.v4.view.ViewCompat; 22 import android.util.Log; 23 import android.view.View; 24 import android.widget.FrameLayout; 25 26 public class TestedFrameLayout extends FrameLayout implements NestedScrollingParent { 27 28 static final int TEST_NESTED_SCROLL_MODE_IGNORE = 0; 29 static final int TEST_NESTED_SCROLL_MODE_CONSUME = 1; 30 31 private int mNestedScrollMode; 32 private int mNestedFlingMode; 33 private boolean mNestedStopNestedScrollCalled; 34 35 public TestedFrameLayout(Context context) { 36 super(context); 37 } 38 39 @Override 40 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { 41 // Always start nested scroll 42 return mNestedFlingMode == TEST_NESTED_SCROLL_MODE_CONSUME 43 || mNestedScrollMode == TEST_NESTED_SCROLL_MODE_CONSUME; 44 } 45 46 @Override 47 public boolean onNestedPreFling(View target, float velocityX, float velocityY) { 48 Log.d("TestedFrameLayout", "onNestedPreFling: " + mNestedFlingMode); 49 50 return mNestedFlingMode == TEST_NESTED_SCROLL_MODE_CONSUME; 51 } 52 53 @Override 54 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { 55 if (mNestedScrollMode == TEST_NESTED_SCROLL_MODE_CONSUME) { 56 // We consume all scroll deltas 57 consumed[0] = dx; 58 consumed[1] = dy; 59 } 60 } 61 62 @Override 63 public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, 64 int dyUnconsumed) { 65 // ignore 66 } 67 68 @Override 69 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { 70 // ignore 71 return false; 72 } 73 74 @Override 75 public void onNestedScrollAccepted(View child, View target, int axes) { 76 // ignore 77 } 78 79 @Override 80 public int getNestedScrollAxes() { 81 // We can scroll in both direction 82 return ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL; 83 } 84 85 @Override 86 public void onStopNestedScroll(View target) { 87 mNestedStopNestedScrollCalled = true; 88 } 89 90 public boolean stopNestedScrollCalled() { 91 return mNestedStopNestedScrollCalled; 92 } 93 94 public void setNestedScrollMode(int mode) { 95 mNestedScrollMode = mode; 96 } 97 98 public void setNestedFlingMode(int mode) { 99 mNestedFlingMode = mode; 100 } 101 } 102