├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── styles.xml │ │ │ └── strings.xml │ │ ├── drawable │ │ │ ├── toolbar_background_gradient.xml │ │ │ ├── toolbar_shadow.xml │ │ │ └── ic_share_white.xml │ │ └── layout │ │ │ ├── view_holder_item.xml │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── alexjlockwood │ │ └── nestedscrolling │ │ ├── LoremIpsumAdapter.java │ │ ├── MaxHeightRecyclerView.java │ │ ├── CustomNestedScrollView2.java │ │ ├── ViewGroupUtils.java │ │ ├── NestedScrollView2.java │ │ ├── MainActivity.java │ │ └── CustomBehavior.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── misc.xml └── codeStyleSettings.xml ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexjlockwood/adp-nested-scrolling/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #3F51B5 5 | #303F9F 6 | #a00808 7 | 8 | 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Nov 24 17:27:01 PST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/toolbar_background_gradient.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/toolbar_shadow.xml: -------------------------------------------------------------------------------- 1 | 4 | 8 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_share_white.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Nested Scrolling 4 | Lorem ipsum dolor sit amet. 5 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eget interdum ante, at adipiscing ipsum. Phasellus pellentesque commodo libero, id volutpat ipsum pretium at. Duis sit amet egestas nisi. Donec id dui eu lectus volutpat posuere. Nunc et viverra ante, vel malesuada tortor. Pellentesque ut interdum tellus, sit amet hendrerit dui. Sed accumsan, nisl eu tincidunt tempus, neque lorem sodales odio, non tristique orci lorem quis augue. In sodales rutrum faucibus. Morbi egestas massa sit amet ante pellentesque, eu laoreet ante congue. Sed aliquet, dolor ac placerat pellentesque, risus leo fermentum quam, eget eleifend tellus nulla euismod tellus. Donec adipiscing cursus leo et faucibus. Aliquam erat volutpat. 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_holder_item.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 16 | 17 | 24 | 25 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | 23 | -keep class * extends android.support.design.widget.CoordinatorLayout$Behavior { 24 | public (); 25 | public (android.content.Context, android.util.AttributeSet); 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/LoremIpsumAdapter.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.support.v7.widget.RecyclerView.ViewHolder; 6 | import android.view.LayoutInflater; 7 | import android.view.ViewGroup; 8 | 9 | /** 10 | * A dummy {@link RecyclerView.Adapter} that displays a list of placeholder 11 | * list items to the user. 12 | */ 13 | class LoremIpsumAdapter extends RecyclerView.Adapter { 14 | private static final int ITEM_COUNT = 5; 15 | 16 | private final LayoutInflater mInflater; 17 | 18 | LoremIpsumAdapter(Context context) { 19 | mInflater = LayoutInflater.from(context); 20 | } 21 | 22 | @Override 23 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 24 | return new ViewHolder( 25 | mInflater.inflate(R.layout.view_holder_item, parent, false)) {}; 26 | } 27 | 28 | @Override 29 | public void onBindViewHolder(ViewHolder holder, int position) { 30 | } 31 | 32 | @Override 33 | public int getItemCount() { 34 | return ITEM_COUNT; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "alexjlockwood.nestedscrolling" 7 | minSdkVersion 21 8 | targetSdkVersion 27 9 | versionCode 2 10 | versionName "1.0" 11 | } 12 | buildTypes { 13 | release { 14 | minifyEnabled true 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 16 | } 17 | } 18 | } 19 | 20 | ext { 21 | supportLibVersion = '27.0.2' 22 | glideVersion = '4.3.1' 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation "com.android.support:appcompat-v7:${supportLibVersion}" 28 | implementation "com.android.support:design:${supportLibVersion}" 29 | implementation "com.android.support:cardview-v7:${supportLibVersion}" 30 | implementation "com.android.support:recyclerview-v7:${supportLibVersion}" 31 | implementation "com.github.bumptech.glide:glide:${glideVersion}" 32 | annotationProcessor "com.github.bumptech.glide:compiler:${glideVersion}" 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/MaxHeightRecyclerView.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | 8 | /** 9 | * A {@link RecyclerView} with an optional maximum height. 10 | */ 11 | public class MaxHeightRecyclerView extends RecyclerView { 12 | private int mMaxHeight = -1; 13 | 14 | public MaxHeightRecyclerView(Context context, AttributeSet attrs) { 15 | super(context, attrs); 16 | } 17 | 18 | @Override 19 | protected void onMeasure(int widthSpec, int heightSpec) { 20 | final int mode = MeasureSpec.getMode(heightSpec); 21 | final int height = MeasureSpec.getSize(heightSpec); 22 | if (mMaxHeight >= 0 && (mode == MeasureSpec.UNSPECIFIED || height > mMaxHeight)) { 23 | heightSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST); 24 | } 25 | super.onMeasure(widthSpec, heightSpec); 26 | } 27 | 28 | /** 29 | * Sets the maximum height for this recycler view. 30 | */ 31 | public void setMaxHeight(int maxHeight) { 32 | if (mMaxHeight != maxHeight) { 33 | mMaxHeight = maxHeight; 34 | requestLayout(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/CustomNestedScrollView2.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.v4.widget.NestedScrollView; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.util.AttributeSet; 9 | import android.view.View; 10 | 11 | /** 12 | * A custom {@link NestedScrollView} that customizes the sample app's 13 | * nested scrolling behavior. 14 | */ 15 | public class CustomNestedScrollView2 extends NestedScrollView2 { 16 | 17 | public CustomNestedScrollView2(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | } 20 | 21 | @Override 22 | public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) { 23 | final RecyclerView rv = (RecyclerView) target; 24 | if ((dy < 0 && isRvScrolledToTop(rv)) || (dy > 0 && !isNsvScrolledToBottom(this))) { 25 | // The NestedScrollView should steal the scroll event away from the 26 | // RecyclerView if: (1) the user is scrolling their finger down and the 27 | // RecyclerView is scrolled to the top of its content, or (2) the user 28 | // is scrolling their finger up and the NestedScrollView is not scrolled 29 | // to the bottom of its content. 30 | scrollBy(0, dy); 31 | consumed[1] = dy; 32 | return; 33 | } 34 | super.onNestedPreScroll(target, dx, dy, consumed, type); 35 | } 36 | 37 | /** 38 | * Returns true iff the {@link NestedScrollView} is scrolled to the bottom 39 | * of its content (i.e. the card is completely expanded). 40 | */ 41 | private static boolean isNsvScrolledToBottom(NestedScrollView nsv) { 42 | return !nsv.canScrollVertically(1); 43 | } 44 | 45 | /** 46 | * Returns true iff the {@link RecyclerView} is scrolled to the 47 | * top of its content (i.e. its first item is completely visible). 48 | */ 49 | private static boolean isRvScrolledToTop(RecyclerView rv) { 50 | final LinearLayoutManager lm = (LinearLayoutManager) rv.getLayoutManager(); 51 | return lm.findFirstVisibleItemPosition() == 0 52 | && lm.findViewByPosition(0).getTop() == 0; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/ViewGroupUtils.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.graphics.Matrix; 4 | import android.graphics.Rect; 5 | import android.graphics.RectF; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.view.ViewParent; 9 | 10 | /** 11 | * A simple utility class copied from the design support library 12 | * source code (see j.mp/ViewGroupUtils). 13 | */ 14 | final class ViewGroupUtils { 15 | private static final Matrix sMatrix = new Matrix(); 16 | private static final RectF sRectF = new RectF(); 17 | private static final Matrix sIdentity = new Matrix(); 18 | private static final Rect sRect = new Rect(); 19 | 20 | /** 21 | * Check if a given point in the parent's coordinates are within 22 | * the view bounds of the given direct child view. 23 | * 24 | * @param child child view to test 25 | * @param x X coordinate to test, in the parent's coordinate system 26 | * @param y Y coordinate to test, in the parent's coordinate system 27 | * @return true if the point is within the child's bounds, false otherwise 28 | */ 29 | static boolean isPointInChildBounds(ViewGroup parent, View child, int x, int y) { 30 | getDescendantRect(parent, child, sRect); 31 | return sRect.contains(x, y); 32 | } 33 | 34 | /** 35 | * Retrieve the transformed bounding rect of an arbitrary descendant view. 36 | * This does not need to be a direct child. 37 | * 38 | * @param descendant descendant view to reference 39 | * @param out rect to set to the bounds of the descendant view 40 | */ 41 | private static void getDescendantRect(ViewGroup parent, View descendant, Rect out) { 42 | out.set(0, 0, descendant.getWidth(), descendant.getHeight()); 43 | offsetDescendantRect(parent, descendant, out); 44 | } 45 | 46 | /** 47 | * This is a port of the common 48 | * {@link ViewGroup#offsetDescendantRectToMyCoords(View, Rect)} from the 49 | * framework, but adapted to take transformations into account. The result 50 | * will be the bounding rect of the real transformed rect. 51 | * 52 | * @param descendant view defining the original coordinate system of rect 53 | * @param rect the rect to offset from descendant to this view's coordinate system 54 | */ 55 | private static void offsetDescendantRect(ViewGroup parent, View descendant, Rect rect) { 56 | sMatrix.set(sIdentity); 57 | offsetDescendantMatrix(parent, descendant, sMatrix); 58 | sRectF.set(rect); 59 | sMatrix.mapRect(sRectF); 60 | final int left = (int) (sRectF.left + 0.5f); 61 | final int top = (int) (sRectF.top + 0.5f); 62 | final int right = (int) (sRectF.right + 0.5f); 63 | final int bottom = (int) (sRectF.bottom + 0.5f); 64 | rect.set(left, top, right, bottom); 65 | } 66 | 67 | private static void offsetDescendantMatrix(ViewParent target, View view, Matrix m) { 68 | final ViewParent parent = view.getParent(); 69 | if (parent instanceof View && parent != target) { 70 | final View vp = (View) parent; 71 | offsetDescendantMatrix(target, vp, m); 72 | m.preTranslate(-vp.getScrollX(), -vp.getScrollY()); 73 | } 74 | m.preTranslate(view.getLeft(), view.getTop()); 75 | if (!view.getMatrix().isIdentity()) { 76 | m.preConcat(view.getMatrix()); 77 | } 78 | } 79 | 80 | private ViewGroupUtils() { 81 | } 82 | } -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/NestedScrollView2.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.view.NestedScrollingParent2; 7 | import android.support.v4.view.NestedScrollingParentHelper; 8 | import android.support.v4.view.ViewCompat; 9 | import android.support.v4.widget.NestedScrollView; 10 | import android.util.AttributeSet; 11 | import android.view.View; 12 | 13 | /** 14 | * A {@link NestedScrollView} that implements the {@link NestedScrollingParent2} interface. 15 | */ 16 | public class NestedScrollView2 extends NestedScrollView implements NestedScrollingParent2 { 17 | private final NestedScrollingParentHelper parentHelper; 18 | 19 | public NestedScrollView2(Context context, AttributeSet attrs) { 20 | super(context, attrs); 21 | parentHelper = new NestedScrollingParentHelper(this); 22 | } 23 | 24 | // NestedScrollingParent2 methods. 25 | 26 | @Override 27 | public boolean onStartNestedScroll( 28 | @NonNull View child, @NonNull View target, int axes, int type) { 29 | return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0; 30 | } 31 | 32 | @Override 33 | public void onNestedScrollAccepted( 34 | @NonNull View child, @NonNull View target, int axes, int type) { 35 | parentHelper.onNestedScrollAccepted(child, target, axes); 36 | startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, type); 37 | } 38 | 39 | @Override 40 | public void onNestedPreScroll( 41 | @NonNull View target, int dx, int dy, @Nullable int[] consumed, int type) { 42 | dispatchNestedPreScroll(dx, dy, consumed, null, type); 43 | } 44 | 45 | @Override 46 | public void onNestedScroll( 47 | @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) { 48 | final int oldScrollY = getScrollY(); 49 | scrollBy(0, dyUnconsumed); 50 | final int myConsumed = getScrollY() - oldScrollY; 51 | final int myUnconsumed = dyUnconsumed - myConsumed; 52 | dispatchNestedScroll(0, myConsumed, 0, myUnconsumed, null, type); 53 | } 54 | 55 | @Override 56 | public void onStopNestedScroll(@NonNull View target, int type) { 57 | parentHelper.onStopNestedScroll(target, type); 58 | stopNestedScroll(type); 59 | } 60 | 61 | // NestedScrollingParent methods. For the most part these methods delegate 62 | // to the NestedScrollingParent2 methods above, passing TYPE_TOUCH as the 63 | // type to maintain API compatibility. 64 | 65 | @Override 66 | public boolean onStartNestedScroll( 67 | @NonNull View child, @NonNull View target, int axes) { 68 | return onStartNestedScroll(child, target, axes, ViewCompat.TYPE_TOUCH); 69 | } 70 | 71 | @Override 72 | public void onNestedScrollAccepted( 73 | @NonNull View child, @NonNull View target, int axes) { 74 | onNestedScrollAccepted(child, target, axes, ViewCompat.TYPE_TOUCH); 75 | } 76 | 77 | @Override 78 | public void onNestedPreScroll( 79 | @NonNull View target, int dx, int dy, @NonNull int[] consumed) { 80 | onNestedPreScroll(target, dx, dy, consumed, ViewCompat.TYPE_TOUCH); 81 | } 82 | 83 | @Override 84 | public void onNestedScroll( 85 | @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { 86 | onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, ViewCompat.TYPE_TOUCH); 87 | } 88 | 89 | @Override 90 | public void onStopNestedScroll(@NonNull View target) { 91 | onStopNestedScroll(target, ViewCompat.TYPE_TOUCH); 92 | } 93 | 94 | @Override 95 | public int getNestedScrollAxes() { 96 | return parentHelper.getNestedScrollAxes(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/MainActivity.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v4.widget.NestedScrollView; 7 | import android.support.v4.widget.NestedScrollView.OnScrollChangeListener; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.support.v7.widget.RecyclerView.OnScrollListener; 12 | import android.support.v7.widget.Toolbar; 13 | import android.view.MenuItem; 14 | import android.view.View; 15 | import android.view.animation.DecelerateInterpolator; 16 | import android.widget.ImageView; 17 | import android.support.v7.widget.DividerItemDecoration; 18 | 19 | import com.bumptech.glide.Glide; 20 | import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; 21 | import com.bumptech.glide.request.RequestOptions; 22 | 23 | /** 24 | * The sample app's main activity. 25 | */ 26 | public class MainActivity extends AppCompatActivity { 27 | 28 | // True iff the shadow view between the card header and the RecyclerView 29 | // is currently showing. 30 | private boolean mIsShowingCardHeaderShadow; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); 37 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 38 | 39 | Glide.with(this).asDrawable() 40 | .load("http://i.imgur.com/zKYUpWa.jpg") 41 | .apply(RequestOptions.placeholderOf(new ColorDrawable(Color.BLACK))) 42 | .apply(RequestOptions.centerCropTransform()) 43 | .transition(DrawableTransitionOptions.withCrossFade()) 44 | .into((ImageView) findViewById(R.id.background_image)); 45 | 46 | final RecyclerView rv = findViewById(R.id.card_recyclerview); 47 | final LinearLayoutManager lm = new LinearLayoutManager(this); 48 | rv.setLayoutManager(lm); 49 | rv.setAdapter(new LoremIpsumAdapter(this)); 50 | rv.addItemDecoration(new DividerItemDecoration(this, lm.getOrientation())); 51 | 52 | final View cardHeaderShadow = findViewById(R.id.card_header_shadow); 53 | rv.addOnScrollListener(new OnScrollListener() { 54 | @Override 55 | public void onScrolled(RecyclerView rv, int dx, int dy) { 56 | // Animate the shadow view in/out as the user scrolls so that it 57 | // looks like the RecyclerView is scrolling beneath the card header. 58 | final boolean isRecyclerViewScrolledToTop = 59 | lm.findFirstVisibleItemPosition() == 0 60 | && lm.findViewByPosition(0).getTop() == 0; 61 | if (!isRecyclerViewScrolledToTop && !mIsShowingCardHeaderShadow) { 62 | mIsShowingCardHeaderShadow = true; 63 | showOrHideView(cardHeaderShadow, true); 64 | } else if (isRecyclerViewScrolledToTop && mIsShowingCardHeaderShadow) { 65 | mIsShowingCardHeaderShadow = false; 66 | showOrHideView(cardHeaderShadow, false); 67 | } 68 | } 69 | }); 70 | 71 | final NestedScrollView nsv = findViewById(R.id.nestedscrollview); 72 | nsv.setOverScrollMode(View.OVER_SCROLL_NEVER); 73 | nsv.setOnScrollChangeListener(new OnScrollChangeListener() { 74 | @Override 75 | public void onScrollChange( 76 | NestedScrollView nsv, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 77 | if (scrollY == 0 && oldScrollY > 0) { 78 | // Reset the RecyclerView's scroll position each time the card 79 | // returns to its starting position. 80 | rv.scrollToPosition(0); 81 | cardHeaderShadow.setAlpha(0f); 82 | mIsShowingCardHeaderShadow = false; 83 | } 84 | } 85 | }); 86 | } 87 | 88 | private static void showOrHideView(View view, boolean shouldShow) { 89 | view.animate().alpha(shouldShow ? 1f : 0f) 90 | .setDuration(100) 91 | .setInterpolator(new DecelerateInterpolator()); 92 | } 93 | 94 | @Override 95 | public boolean onOptionsItemSelected(MenuItem item) { 96 | if (item.getItemId() == android.R.id.home) { 97 | finish(); 98 | return true; 99 | } 100 | return super.onOptionsItemSelected(item); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 22 | 23 | 30 | 31 | 32 | 37 | 38 | 45 | 46 | 51 | 52 | 56 | 57 | 69 | 70 | 81 | 82 | 85 | 86 | 90 | 91 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /app/src/main/java/alexjlockwood/nestedscrolling/CustomBehavior.java: -------------------------------------------------------------------------------- 1 | package alexjlockwood.nestedscrolling; 2 | 3 | import android.content.Context; 4 | import android.support.design.widget.CoordinatorLayout; 5 | import android.support.v4.view.ViewCompat; 6 | import android.support.v4.widget.NestedScrollView; 7 | import android.util.AttributeSet; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.view.ViewGroup.MarginLayoutParams; 12 | 13 | import static android.support.design.widget.CoordinatorLayout.Behavior; 14 | 15 | /** 16 | * A custom {@link Behavior} used to block touch events that do not originate on 17 | * top of the {@link MainActivity}'s card view or FAB. It also is used to 18 | * adjust the layout so that the UI is displayed properly. 19 | */ 20 | class CustomBehavior extends CoordinatorLayout.Behavior { 21 | 22 | public CustomBehavior(Context context, AttributeSet attrs) { 23 | super(context, attrs); 24 | } 25 | 26 | @Override 27 | public boolean layoutDependsOn( 28 | CoordinatorLayout parent, NestedScrollView child, View dependency) { 29 | // List the toolbar container as a dependency to ensure that it will 30 | // always be laid out before the child (which depends on the toolbar 31 | // container's height in onLayoutChild() below). 32 | return dependency.getId() == R.id.toolbar_container; 33 | } 34 | 35 | @Override 36 | public boolean onLayoutChild( 37 | CoordinatorLayout parent, NestedScrollView child, int layoutDirection) { 38 | // First layout the child as normal. 39 | parent.onLayoutChild(child, layoutDirection); 40 | 41 | // Center the FAB vertically along the top edge of the card. 42 | final int fabHalfHeight = child.findViewById(R.id.fab).getHeight() / 2; 43 | setTopMargin(child.findViewById(R.id.cardview), fabHalfHeight); 44 | 45 | // Give the RecyclerView a maximum height to ensure the card will never 46 | // overlap the toolbar as it scrolls. 47 | final int rvMaxHeight = 48 | child.getHeight() 49 | - fabHalfHeight 50 | - child.findViewById(R.id.card_title).getHeight() 51 | - child.findViewById(R.id.card_subtitle).getHeight(); 52 | final MaxHeightRecyclerView rv = child.findViewById(R.id.card_recyclerview); 53 | rv.setMaxHeight(rvMaxHeight); 54 | 55 | // Give the card container top padding so that only the top edge of the card 56 | // initially appears at the bottom of the screen. The total padding will 57 | // be the distance from the top of the screen to the FAB's top edge. 58 | final View cardContainer = child.findViewById(R.id.card_container); 59 | final int toolbarContainerHeight = 60 | parent.getDependencies(child).get(0).getHeight(); 61 | setPaddingTop(cardContainer, rvMaxHeight - toolbarContainerHeight); 62 | 63 | // Offset the child's height so that its bounds don't overlap the 64 | // toolbar container. 65 | ViewCompat.offsetTopAndBottom(child, toolbarContainerHeight); 66 | 67 | // Add the same amount of bottom padding to the RecyclerView so it doesn't 68 | // display its content underneath the navigation bar. 69 | setPaddingBottom(rv, toolbarContainerHeight); 70 | 71 | // Return true so that the parent doesn't waste time laying out the 72 | // child again (any modifications made above will have triggered a second 73 | // layout pass anyway). 74 | return true; 75 | } 76 | 77 | private static void setTopMargin(View v, int topMargin) { 78 | final MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); 79 | if (lp.topMargin != topMargin) { 80 | lp.topMargin = topMargin; 81 | v.setLayoutParams(lp); 82 | } 83 | } 84 | 85 | private static void setPaddingTop(View v, int top) { 86 | if (v.getPaddingTop() != top) { 87 | v.setPadding(v.getPaddingLeft(), top, v.getPaddingRight(), v.getPaddingBottom()); 88 | } 89 | } 90 | 91 | private static void setPaddingBottom(View v, int bottom) { 92 | if (v.getPaddingBottom() != bottom) { 93 | v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), bottom); 94 | } 95 | } 96 | 97 | @Override 98 | public boolean onInterceptTouchEvent( 99 | CoordinatorLayout parent, NestedScrollView child, MotionEvent ev) { 100 | // Block all touch events that originate within the bounds of our 101 | // NestedScrollView but do *not* originate within the bounds of its 102 | // inner CardView and FloatingActionButton. 103 | return ev.getActionMasked() == MotionEvent.ACTION_DOWN 104 | && isTouchInChildBounds(parent, child, ev) 105 | && !isTouchInChildBounds(parent, child.findViewById(R.id.cardview), ev) 106 | && !isTouchInChildBounds(parent, child.findViewById(R.id.fab), ev); 107 | } 108 | 109 | private static boolean isTouchInChildBounds( 110 | ViewGroup parent, View child, MotionEvent ev) { 111 | return ViewGroupUtils.isPointInChildBounds( 112 | parent, child, (int) ev.getX(), (int) ev.getY()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 236 | 238 | --------------------------------------------------------------------------------