Android hide fab on scroll recycler view

How to hide Floating Action Button when scrolling in Recycler View

Published by Gurleen Sethi on March 14, 2017 March 14, 2017

You might have seen many applications hiding the FAB on scrolling down and showing back when scrolling up. Hiding the FAB leaves nothing in the way of user and the content, making it an overall better UX.

It is very easy to achieve this kind of functionality. The prerequisites of this tutorial is that you have a basic understanding of Recycler View and FloatingActionButton.

First set up a basic Activity with a Coordinator Layout containing a Recycler View and FloatingActionButton, just as below.

Now reference the RecyclerView and FloatingActionButton in the Activity.

Now comes the main part. We will attach a scroll listener to the recycler view, so whenever the user scrolls the list we will get notified and will toggle the visibility of the FAB accordingly. The scroll listener looks like this.

Lets break down the above code and understand each line.

  • hide() and show()are two methods provided by the FAB to hide/show the FAB button with a smooth animation.
  • dyis a value that changes when you scroll vertically, when the user scrolls down the value is positive and when the user scrolls up the value is negative. So we check if the FAB is visible and the value is positive(i.e. user is scrolling down) we will hide it and if the FAB is hidden and the value is negative(i.e. user is scrolling up) we will show the FAB.

Once you run the app it will look something like this. Simple and slick!

Gurleen Sethi

Android Developer and enthusiast, believes in developing products and content to help people. Likes to do deep thinking.

Источник

Hide FAB in NestedScrollView when scrolling

I am having a nestedscrollview with content like some linearlayouts and textviews. I am using a floatingactionbutton library for some reasons, as well. So I can’t use any behavior for it. I don’t know how I should handle the scrollchangelistener from scrollview to hide and show the fab dynamically like a behavior.

Any suggestions how to hide and show the fab while scrolling?

5 Answers 5

Simple add this code below to your NestedScrollView ScrollChangeListener:

Create FabScrollBehavior class

Where AppUtil.getToolbarHeight(context) is —

then in your layout add to FloatingActionButton layout_behavior:

The whole layout looks like

define variable type int in your Activity or fragment to set previous Scroll from ScrollView then use this method to listen change scroll in ScrollView Class

Читайте также:  Momo player android emulator

will work done all version of android

After spending such time i have found the solution for it. It may work in all situations. Though it is a hack not the proper solution but you can apply it to make this thing work.

As we know setOnScrollChangeListener will only work if minimum api 23, so what if my minimum api level is less then 23.

So I found out solution from stack overflow that we can use getViewTreeObserver().addOnScrollChangedListener for that so this will be compatible solution for all devices.

Now let’s move to the final solution of over problem «Hide fab button when nested scroll view scrolling and Show fab button when nested scroll view in ideal state»

So for that we can use Handler with postDelayed to slove this issue.

Define on variable in you context private int previousScrollY = 0;

Then use getViewTreeObserver().addOnScrollChangedListener to your nested scroll view like this.

  1. Now you are ready to go.

Источник

How to hide bottom app bar on scroll in android

I am making an android application which has an activity and other activity implements the main activity.Now i am also implementing one activity many fragment pattern.So each activity has at least 7-8 fragment inside that. Here is layout for my main activity.

You can see that my main activity has frame layout in it in which i transact all the fragments.I used image button in place of floating action button as i want floating action button of oval shape.Now what i want that inside fragment when user scrolls then the image button , bottomappbar and the view which is horizontal line hides? The bottom app bar is being used in many fragments so i need a code which i can write in a activity which hides the bottomapp bar and the image button on while users scroll inside fragment.How can i achieve this? I am sorry for my silly question as i am new to android development .Thanks in advance.

4 Answers 4

You can achieve this by putting the below two lines in xml

So the full xml tag will be

Because you are scrolling inside a Fragment, you need to pass the scrolling values to your activity.

I suggest you use the default InteractionInterface that Android Studio generated in Fragment’s template:

And then, in YourActivity , implement YourFragment.OnFragmentInteractionListener

Override the function

the result will be like this mp4 link

The main point is : Pass your scrolling action from Fragment to Activity, You can achieve this in many ways, this is just the basic one;

A similar approach to Wesely’s is the following.

Let assume we have something like this:

A custom bottom appbar and we have at least one fragment per action. What I mean is that activity main is the parent where all the fragments are going to place in.

Читайте также:  Восстановление заводских настроек андроид xiaomi redmi

You can declare an interface in main activity (in this example I want to hide/show a custom bottom appbar and FAB) to perform show/hide of the bottom appbar and fab (both of them in activity_main.xml), so the interface would look something like this:

Once the interface is defined in main activity, every fragment with a Nested scroll view can implement it.

Creating a reference to the NestedScrollView and attaching the onScrollChangeListener will help us to check when there is a change in the scroll over the y axis

Remember in Kotlin we can use _ when the parameter in the lambda is never used. So basically the change in y is val dy = oldScrollY — scrollY and dy is negative when the scroll is from bottom to top and this condition dy is true, so we use the context of the view to invoke OnScrollListenerMain.fabAndBottomAppBarHide()
and again dy is positive when the scroll is from top to bottom and this condition dy > 0 is true, so we use the context of the view to invoke OnScrollListenerMain.fabAndBottomAppBarShow()

But, what if we just want to hide a BottomAppbar (without hiding FAB)?

The BottomAppbar might be child of Coordinator layout and the fragments need to have a NestedScrollView, RecyclerView or ScrollView as parent in their XML, so the main activity XML should look something like:

The fragments should be placed into lly_main and this line makes the trick: app:hideOnScroll=»true»

Источник

Hiding the ActionBar on RecyclerView/ListView onScroll

In my application I got an activity with some kind of actionbar at the top and the listview below it. What I want to do — is to scroll it UP with the list, so it hides and then, when the list is being scrolled down — it should scroll down with the list, like it was just over the upper screen border. how can i achieve this functionality?

7 Answers 7

Updated 6/3/2015:

Google now supports this using the CoordinatorLayout .

Original Answer:

Example similar to Google Play Music and Umano apps:

Take a look at the code in this repository. As you slide the panel up, the ActionBar slides up as well.

From the Demo:

Download example here:

ListView — without Libraries:

I recently wanted the same functionality and this works perfectly for me:

As the user scrolls upward, the ActionBar will be hidden in order to give the user the entire screen to work work with.

As the user scrolls downward and lets go, the ActionBar will return.

RecyclerView — without libraries

Let me know if you need anymore help!

You experience the flickering since by hiding/showing the ActionBar the available space for your content layout changes, which causes a relayout. With this the index of the first visible item’s position changes as well (you can verify this by logging out mLastFirstVisibleItem and currentFirstVisibleItem.

You can cope with the flickering by letting the ActionBar overlay your content layout. To enable overlay mode for the action bar, you need to create a custom theme that extends an existing action bar theme and set the android:windowActionBarOverlay property to true.

Читайте также:  Когда вышла операционная система android

With this you can eliminate the flickering but the action bar will overlay your listview’s content. An easy solution to this is to set the listview’s (or the root layout’s) top padding to the action bar’s height.

Unfortunately this will result in a constant padding at the top. A refinement of this solution is to add a header view to the list view which has the height of ?android:attr/actionBarSize (and remove the top padding set previously)

Источник

Hide FloatingActionButton on scroll of RecyclerView

I want to hide/show FloatingActionButton on scroll of RecyclerView .

DrawerLayout is the parent layout of this layout.

Used this layout behaviour for FloatingActionButton .

When I see logcat only constructor is getting called. onNestedScroll() doesn’t get called when I scroll the list.

15 Answers 15

This should work for you:

Ok, here is what you need:

First, since your FAB depends on the RecyclerView , add the following to your behavior class:

Next, in order to receive onNestedScroll() calls, you need to override this:

Update

Here is what your ScrollAwareFABBehavior should look like:

Also, it was tested using com.android.support:design:23.0.1

Short and Simple Solution:

If you are using Material Components for Android and your FAB is inside a CoordinatorLayout then you can use the layout_behavior com.google.android.material.behavior.HideBottomViewOnScrollBehavior

This is how I did. It works for me! If you don’t know how to implement, You can see detail in this link https://guides.codepath.com/android/floating-action-buttons

If you are not using coordinator layout and want to hide and show FAB smoothly. And you want to implement your own logic to hide fab on scroll down, and show it on scrolling up.

Then here is the solution in kotlin,

  • Declare a variable,
  • After that create a listener,
  • create anim resource file

I have created a custom RecyclerView which has OnUpDownScrollListener , OnLeftRightScrollListener ready:

MBRecyclerView.java

Usage (UpDownScrollListener):

similarely you can handle the LeftRight scrolling by setting

I hope it can help somebody 🙂

The problem is Android 25.0.x+ sets the view to GONE and thats why the listener is not reporting changes.

All the answers which goes on the Behavior path and onNestedScroll (instead of recyclerview listener) don’t comment about the fact that onNestedScroll will be called many times while scrolling. This means that child.show() and child.hide() will be called many times as well. Although show() and hide() are designed to handle that scenario, they still run a lot of code and create some objects which multiplied by the times onNestedScroll is called, result in a lot of objects created unnecessarily.

Considering that and because I wanted to run a different animation instead of the default show() and hide(), I came up with the following Behavior implementation:

The floating action button will hide when there’s scrolling and show when the scrolling stops.

Источник

Оцените статью