0

How can I hide fab button when scrolling listView?

I'm using this code at the moment, but it hides FAB button whenever I'm touching screen and scrolling, I need it to hide FAB button when scrolling down and when scrolling a bit up it has to be shown again

current code:

mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
                floatingActionButton.hide();
            }else{
                floatingActionButton.show();
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });
LeTadas
  • 3,436
  • 9
  • 33
  • 65

1 Answers1

1

try this you should use onScroll instead of onScrollStateChanged

listview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {


    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        // add here your logic like this
        // int lastItem = firstVisibleItem + visibleItemCount;
        if (firstVisibleItem < 2) {

            floatingActionButton.setVisibility(View.INVISIBLE);
        }else {
            floatingActionButton.setVisibility(View.VISIBLE);
        }
    }
});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163