ADVERTISEMENT

How to hide android floating action button as one scrolls RecyclerView




You might have across some applications that hides floating action button as user scrolls down the RecyclerView and reveals it back when user starts to scroll up. If you have been following my posts we talked about this concept when we were learning how to implement endless scrolling recycler view in android. Under this tutorial we will use the same concept to show and hide floating action button as user scrolls up and down RecyclerView

Approach

The concept behind this is very simple, you just need to detect when user scrolls down and up the RecyclerView. Thanks God, in android there are some built in methods that can help us detect when user is scrolling either side in Recyclerview. Please have a look at the code below, since it will make you get the full concept:

RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
 
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }
 
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
 
        if (dy < 0) {
            // Recycle view scrolling up...
 
        } else if (dy > 0) {
            // Recycle view scrolling down...
        }
    }
});

How to hide floating action button as user scrolling down RecyclerView



You don’t need to create any new application, you just need to observe this simple concept here. First you will need to declare FloatingActionButton object as field, then perform it’s operation as user scrolls down the RecyclerView:

private FloatingActionButton fab;

public class MainActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fab = (FloatingActionButton) this.findViewById(R.id.fab);
        // starting recycler view

       RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
 
       rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
          }
 
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
 
              if (dy < 0) {
                  fab.show();
 
               } else if (dy > 0) {
                  fab.hide();
               }
         }
       });
        // ending recyler view
    }
}

Conclusion

In case you got any question regarding above tutorial, please leave a reply and I will reply to your comment as soon as possible. You can also contact us and we will reply within a period of 24 hours. Thanks!



AdBlock Detected!
This site depends on revenue from ad impressions to survive. If you find this site valuable, please consider disabling your ad blocker or pausing adblock for this website.