Alexiz Hernandez Alexiz Hernandez - 6 months ago 1371
Android Question

How to hide and show FloatingActionButton with Fragments in a ViewPager?

I am trying to only show my FloatingActionButton on one fragment and hide in the rest. I have tried multiple answers and nothing is working. I tried this solution:


Hide a Floating Action Button of another Layout


But it isn't working. I tried adding buttons on two of the fragments, one that shows and one that hides the FAB and that works, but once I remove the button, it won't show automatically. Here is my code:

hidden_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>


shown_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>


HiddenFragment.java:

public class HiddenFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.hidden_fragment, container, false);

final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();

if (fab != null) {
((MainActivity) getActivity()).hideFloatingActionButton();
}

return view;
}

}


ShownFragment.java:

public class ShownFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.hidden_fragment, container, false);

final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton();

if (fab != null) {
((MainActivity) getActivity()).showFloatingActionButton();
}

return view;
}

}


In my MainActivity.java I have this:

public FloatingActionButton getFloatingActionButton() {
return fab;
}

public void showFloatingActionButton() {
fab.show();
}

public void hideFloatingActionButton() {
fab.hide();
}


The way I currently have it doesn't work. When I launch the app, the first fragment launched is the HiddenFragment. But when I go to the ShownFragment, the FAB doesn't appear. I tried a different approach, I added a button to each fragment and added the showFloatingActionButton() and hideFloatingActionButton() to the buttons and that works. Does anyone know what I am doing wrong?

Answer

For showing/hiding a FloatingActionButton with Fragments in a ViewPager, just use a ViewPager.OnPageChangeListener and show the FloatingActionButton for the positions you want, and hide it for the positions you want.

This would go in the Activity:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                case 0:
                    fab.hide();
                    break;
                case 1:
                    fab.show();
                    break;
                case 3:
                    fab.hide();
                    break;
                default:
                    fab.hide();
                    break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });