-1

hi i have an ViewPager and FloatingButton on my main activity and view pager have some Fragments with ListView i want to know is there any way when my lists on viewpager scroll this FloatingButton Hide and show?

Sadegh
  • 55
  • 7

1 Answers1

0

try this:

We will add a function in main activity that will get called from fragment, the function will hide/show the floating action button.

In MainActivity

//Make FAB accessible on all function, declare it in MainActivity's class
FloatingActionButton fab;

public void fabShowHide(Boolean hide){
     if(hide)
         fab.hide();
     else
         fab.show();
}

In your fragment, attach onScrollListner to your listview

Replace YourActivityClassName with the main activity class name, this is probably MainActivity, check the name of the class in main activity

 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) {

        int lastItem = firstVisibleItem + visibleItemCount;
        if (lastItem == totalItemCount && firstVisibleItem > 0) {
        //Calling the function from main activity (hide)
            ((YourActivityClassName)getActivity()).fabShowHide(true);
        }
        else {
        //Calling the function from main activity (show)
            ((YourActivityClassName)getActivity()).fabShowHide(false);
        }
    }
});

Floating Action Button in Main Activity

(Delete the app behavior, you don't need it, we created it programmly)

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

Good luck :)

Sources:

Call an activity method from a fragment

FloatingActionButton hide on list scroll

Dor Rud
  • 114
  • 6
  • i search a bit is there any way can i use scrollawarefabbehavior on this? or maybe a good animation i do only fab.setVisibilty(GONE) and for show like this – Sadegh Jan 10 '18 at 01:54
  • Hi there, if you use my method it will do a nice hide/show animation - when using FAB's method fab.show() or fab.hide() it will do a nice reveal animation. – Dor Rud Jan 10 '18 at 08:42