Charles Galvez
Charles Galvez

Reputation: 1110

Recyclerview AddOnScrollListener

I have implemented addonscrollListener for my recyclerview since I want to
hide my FAB when recyclerview is scrolled as follows :

mRvNearby.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) {
                super.onScrolled(recyclerView, dx, dy);
                Log.e("DY",""+dy);
                if(dy<0){
                    mFloatingActionMenu.hideMenuButton(true);
                }else{
                    mFloatingActionMenu.showMenuButton(true);
                }
            }
        });

But unfortunately, when I use this listener, the "dy" only changes once, and when I scrolled continuously, the value
hasn't changed.
I was expecting that when scrolled down, The value will be less than 0.
and when scrolled up, the value is greater than 0.

Upvotes: 2

Views: 10801

Answers (2)

VishnuSP
VishnuSP

Reputation: 589

You can use FloatingActionButton.Behavior for showing/hiding FAB while Recyclerview scrolls.

Check out this Tutorial

Upvotes: 0

R.R.M
R.R.M

Reputation: 790

Use greater than sign instead of less than. When you scroll down the value is greater than 0. So your code will be :

 mRvNearby.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) {
            super.onScrolled(recyclerView, dx, dy);
            Log.e("DY",""+dy);
            if(dy>0){
                mFloatingActionMenu.hideMenuButton(true);
            }else{
                mFloatingActionMenu.showMenuButton(true);
            }
        }
    });

Upvotes: 4

Related Questions