Skip to content Skip to sidebar Skip to footer

How To Hide Fab On A Particular Fragment

Well I want to remove/hide FAB only on a particular fragment , HISTORY tab in my case . viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { .... (Overr

Solution 1:

I'd think it's as simple as this:

viewPager.addOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
        @OverridepublicvoidonPageSelected(int position) {
            if(position == 0) {
               // hide FAB
            } else {
               // show FAB
            }
        }
}

You can do a lot of cleanup to improve maintenance like assign page numbers to each page like:

privatestaticfinalint PAGE_HISTORY = 0;
privatestaticfinalint PAGE_TRANSACTION = 1;
privatestaticfinalint PAGE_FUTURE = 2;

So instead of if(position == 0) you say if(position == PAGE_HISTORY). That way you don't have to change a lot of code if you ever change your pages around.

Solution 2:

You can try setting the OnTabSelectedListener to your TabLayout.

tabLayout.setOnTabSelectedListener(newTabLayout.OnTabSelectedListener() {
    @OverridepublicvoidonTabSelected(TabLayout.Tab tab) {

        yourFAB.setVisibilty(View.GONE);

        // or

       yourFAB.setVisibilty(View.VISIBLE);

    }

    @OverridepublicvoidonTabUnselected(TabLayout.Tab tab) {

    }

    @OverridepublicvoidonTabReselected(TabLayout.Tab tab) {

    }
});

Solution 3:

Just add this you your OnPageChangeListener:

@OverridepublicvoidonPageSelected(int position) {
    if (adapter.getItem(position) instanceof HistoryFragment) fab.hide();
    //else fab.show();
}    

Post a Comment for "How To Hide Fab On A Particular Fragment"