├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-v21 │ │ │ │ └── dimen.xml │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_favorite_outline_white_24dp.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_favorite_outline_white_24dp.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_favorite_outline_white_24dp.png │ │ │ ├── drawable-xxhdpi │ │ │ │ └── ic_favorite_outline_white_24dp.png │ │ │ ├── values │ │ │ │ ├── dimen.xml │ │ │ │ ├── strings.xml │ │ │ │ ├── color.xml │ │ │ │ └── styles.xml │ │ │ ├── layout │ │ │ │ ├── recycler_header.xml │ │ │ │ ├── fragment_part_three.xml │ │ │ │ ├── recycler_item.xml │ │ │ │ ├── activity_part_two.xml │ │ │ │ ├── activity_part_one.xml │ │ │ │ ├── tabs.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── activity_part_three.xml │ │ │ └── drawable │ │ │ │ └── fab_bcg.xml │ │ ├── java │ │ │ └── pl │ │ │ │ └── michalz │ │ │ │ └── hideonscrollexample │ │ │ │ ├── adapter │ │ │ │ ├── partone │ │ │ │ │ ├── viewholder │ │ │ │ │ │ ├── RecyclerHeaderViewHolder.java │ │ │ │ │ │ └── RecyclerItemViewHolder.java │ │ │ │ │ └── RecyclerAdapter.java │ │ │ │ └── parttwo │ │ │ │ │ ├── viewholder │ │ │ │ │ └── RecyclerItemViewHolder.java │ │ │ │ │ └── RecyclerAdapter.java │ │ │ │ ├── Utils.java │ │ │ │ ├── ScrollingFABBehavior.java │ │ │ │ ├── listener │ │ │ │ ├── partone │ │ │ │ │ └── HidingScrollListener.java │ │ │ │ └── parttwo │ │ │ │ │ └── HidingScrollListener.java │ │ │ │ ├── activity │ │ │ │ ├── MainActivity.java │ │ │ │ ├── partthree │ │ │ │ │ └── PartThreeActivity.java │ │ │ │ ├── partone │ │ │ │ │ └── PartOneActivity.java │ │ │ │ └── parttwo │ │ │ │ │ └── PartTwoActivity.java │ │ │ │ └── fragment │ │ │ │ └── PartThreeFragment.java │ │ └── AndroidManifest.xml │ └── androidTest │ │ └── java │ │ └── pl │ │ └── michalz │ │ └── hideonscrollexample │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── 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/mzgreen/HideOnScrollExample/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/dimen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_favorite_outline_white_24dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/drawable-hdpi/ic_favorite_outline_white_24dp.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_favorite_outline_white_24dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/drawable-mdpi/ic_favorite_outline_white_24dp.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_favorite_outline_white_24dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/drawable-xhdpi/ic_favorite_outline_white_24dp.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_favorite_outline_white_24dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mzgreen/HideOnScrollExample/HEAD/app/src/main/res/drawable-xxhdpi/ic_favorite_outline_white_24dp.png -------------------------------------------------------------------------------- /app/src/main/res/values/dimen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 48dp 4 | 0dp 5 | -------------------------------------------------------------------------------- /app/src/main/res/layout/recycler_header.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Feb 07 15:56:39 CET 2015 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-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_part_three.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | HideOnScrollExample 3 | FAB button description 4 | Part One 5 | Part Two 6 | Part Three 7 | TAB 2 8 | TAB 1 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/adapter/partone/viewholder/RecyclerHeaderViewHolder.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample.adapter.partone.viewholder; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | 6 | public class RecyclerHeaderViewHolder extends RecyclerView.ViewHolder { 7 | public RecyclerHeaderViewHolder(View itemView) { 8 | super(itemView); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /app/src/androidTest/java/pl/michalz/hideonscrollexample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Intellij project files 29 | *.iml 30 | *.ipr 31 | *.iws 32 | .idea/ 33 | -------------------------------------------------------------------------------- /app/src/main/res/values/color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #f44336 4 | #d32f2f 5 | #4caf50 6 | #388e3c 7 | #2196f3 8 | #1976d2 9 | #ff4081 10 | #44000000 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HideOnScrollExample 2 | ============= 3 | 4 | This example shows how to show/hide views (i.e. Toolbar or FAB) when a list is scrolled up/down. 5 | 6 | There is a blog post explaining the code: 7 | 8 | [part 1 - outdated](http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/) 9 | 10 | [part 2 - outdated](http://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling%28part2%29/) 11 | 12 | [part 3](https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/) 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/fab_bcg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android SDK\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/Utils.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | 6 | public class Utils { 7 | 8 | public static int getToolbarHeight(Context context) { 9 | final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes( 10 | new int[]{R.attr.actionBarSize}); 11 | int toolbarHeight = (int) styledAttributes.getDimension(0, 0); 12 | styledAttributes.recycle(); 13 | 14 | return toolbarHeight; 15 | } 16 | 17 | public static int getTabsHeight(Context context) { 18 | return (int) context.getResources().getDimension(R.dimen.tabsHeight); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/recycler_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 16 | 17 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "pl.michalz.hideonscrollexample" 9 | minSdkVersion 14 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:22.2.0' 25 | compile 'com.android.support:recyclerview-v7:22.2.0' 26 | compile 'com.android.support:cardview-v7:22.2.0' 27 | compile 'com.android.support:design:22.2.0' 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 10 | 11 | 15 | 16 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/adapter/partone/viewholder/RecyclerItemViewHolder.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample.adapter.partone.viewholder; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | import pl.michalz.hideonscrollexample.R; 7 | 8 | public class RecyclerItemViewHolder extends RecyclerView.ViewHolder { 9 | 10 | private final TextView mItemTextView; 11 | 12 | public RecyclerItemViewHolder(final View parent, TextView itemTextView) { 13 | super(parent); 14 | mItemTextView = itemTextView; 15 | } 16 | 17 | public static RecyclerItemViewHolder newInstance(View parent) { 18 | TextView itemTextView = (TextView) parent.findViewById(R.id.itemTextView); 19 | return new RecyclerItemViewHolder(parent, itemTextView); 20 | } 21 | 22 | public void setItemText(CharSequence text) { 23 | mItemTextView.setText(text); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/adapter/parttwo/viewholder/RecyclerItemViewHolder.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample.adapter.parttwo.viewholder; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | import pl.michalz.hideonscrollexample.R; 7 | 8 | public class RecyclerItemViewHolder extends RecyclerView.ViewHolder { 9 | 10 | private final TextView mItemTextView; 11 | 12 | public RecyclerItemViewHolder(final View parent, TextView itemTextView) { 13 | super(parent); 14 | mItemTextView = itemTextView; 15 | } 16 | 17 | public static RecyclerItemViewHolder newInstance(View parent) { 18 | TextView itemTextView = (TextView) parent.findViewById(R.id.itemTextView); 19 | return new RecyclerItemViewHolder(parent, itemTextView); 20 | } 21 | 22 | public void setItemText(CharSequence text) { 23 | mItemTextView.setText(text); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 23 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_part_two.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 18 | 19 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_part_one.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 9 | 10 | 15 | 16 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/adapter/parttwo/RecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample.adapter.parttwo; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import pl.michalz.hideonscrollexample.R; 9 | import pl.michalz.hideonscrollexample.adapter.parttwo.viewholder.RecyclerItemViewHolder; 10 | 11 | import java.util.List; 12 | 13 | public class RecyclerAdapter extends RecyclerView.Adapter { 14 | 15 | private List mItemList; 16 | 17 | public RecyclerAdapter(List itemList) { 18 | mItemList = itemList; 19 | } 20 | 21 | @Override 22 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 23 | Context context = parent.getContext(); 24 | View view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false); 25 | return RecyclerItemViewHolder.newInstance(view); 26 | } 27 | 28 | @Override 29 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { 30 | RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder; 31 | String itemText = mItemList.get(position); 32 | holder.setItemText(itemText); 33 | } 34 | 35 | @Override 36 | public int getItemCount() { 37 | return mItemList == null ? 0 : mItemList.size(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/res/layout/tabs.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 9 | 17 | 22 | 23 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/pl/michalz/hideonscrollexample/ScrollingFABBehavior.java: -------------------------------------------------------------------------------- 1 | package pl.michalz.hideonscrollexample; 2 | 3 | import android.content.Context; 4 | import android.support.design.widget.AppBarLayout; 5 | import android.support.design.widget.CoordinatorLayout; 6 | import android.support.design.widget.FloatingActionButton; 7 | import android.util.AttributeSet; 8 | import android.view.View; 9 | 10 | public class ScrollingFABBehavior extends FloatingActionButton.Behavior { 11 | private int toolbarHeight; 12 | 13 | public ScrollingFABBehavior(Context context, AttributeSet attrs) { 14 | super(); 15 | this.toolbarHeight = Utils.getToolbarHeight(context); 16 | } 17 | 18 | @Override 19 | public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 20 | return super.layoutDependsOn(parent, fab, dependency) || (dependency instanceof AppBarLayout); 21 | } 22 | 23 | @Override 24 | public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 25 | boolean returnValue = super.onDependentViewChanged(parent, fab, dependency); 26 | if (dependency instanceof AppBarLayout) { 27 | CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); 28 | int fabBottomMargin = lp.bottomMargin; 29 | int distanceToScroll = fab.getHeight() + fabBottomMargin; 30 | float ratio = (float)dependency.getY()/(float)toolbarHeight; 31 | fab.setTranslationY(-distanceToScroll * ratio); 32 | } 33 | return returnValue; 34 | } 35 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 |