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?
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: