简体   繁体   中英

How to hide bottom nav bar in fragment

I have bottom nav bar define in the Main activity. I have three fragments linked with BottomNavigation bar in fragments I have recycler view so I want to hide BottomNavigation bar when RecyclerView scrolls down and shows when RecyclerView scrolls up. My problem is how can I access BottomNavigation bar in fragments because it is defined in MainActivity.

This is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp"
    android:background="@color/colorPrimary"
    android:paddingBottom="7dp"
    android:fitsSystemWindows="true">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <Spinner
            android:layout_width="110dp"
            android:layout_height="50dp"
            android:id="@+id/chooseLocation"
            app:backgroundTint="@android:color/white"/>

    </android.support.v7.widget.Toolbar>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:id="@+id/search"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:paddingRight="6dp"
        android:paddingLeft="12dp"
        android:hint="Search here"
        android:textColorHint="#9e9e9e"
        android:textColor="#000"
        tools:ignore="HardcodedText"
        android:background="@drawable/search_edit_text"
        android:paddingEnd="6dp"
        android:paddingStart="12dp"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottomBar"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_menu"
    android:background="#fff"
    app:itemIconTint="@drawable/nav_check"
    app:itemTextColor="@drawable/nav_check"/>

</android.support.design.widget.CoordinatorLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab1Fragment"
android:background="#fff">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/purchasedBook"/>


</RelativeLayout>

This is how my fragments are defined as there is no bottom nav bar in any fragments so how can I access bottom nav bar in fragments.

Someone, please let me know any help would be appreciated.

THANKS

要从片段中访问您的 BottomNavigationView,请使用以下代码:

BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);
navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.full_screen_destination) {
       
       bottomNavigationView.visibility = View.GONE
   } else {
       
       bottomNavigationView.visibility = View.VISIBLE
   }
}

Do this in the main activity. Here R.id.full_screen_destination is id of the fragment in navigation fragment.

As the fragment is always inside an activity and you can call getActivity() in fragment to access objects that already exist in the activity. So you can do this:

Activity

public class MainActivity extends Activity {
//...
   Toolbar toolbar;
//...
   public Toolbar getNav() {
      return toolbar;
   }
//...
}

Fragment

//...
if(getActivity() != null && getActivity instanceOf MainActivity)
    ((MainActivity)getActivity()).getNav().setVisiblity(View.GONE);
//...

Try this,

Add this line in the BottomNavigationView in Xml

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

And Implement this BottomNavigation behavior using CoOrdinator Layout and you can hide or show the view using the scroll listeners.

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

private int height;

@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
    height = child.getHeight();
    return super.onLayoutChild(parent, child, layoutDirection);
}

@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                               BottomNavigationView child, @NonNull 
                               View directTargetChild, @NonNull View target,
                               int axes, int type)
{
    return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}

@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
           @NonNull View target, int dxConsumed, int dyConsumed,
           int dxUnconsumed, int dyUnconsumed, 
            @ViewCompat.NestedScrollType int type)
{
   if (dyConsumed > 0) {
       slideDown(child);
   } else if (dyConsumed < 0) {
       slideUp(child);
   }
}

private void slideUp(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(0).setDuration(200);
}

private void slideDown(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(height).setDuration(200);
}

}

Add this line code to your Activity where it contains bottom navigation

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) 
bottomNavigationView .getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

Try this and let me know Digvijay.Happy Coding.

Fragment has onAttach() method which give you context. So you have to create instance of activity using,

MainActivity mainActivity;
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mainActivity = (MainActivity)context;
}

Now make method with boolean param which hide and show bottom bar.

public void visibilityOfBottom(boolean isScroll){
  if(isScroll){

  // hide bottom bar

  } else{
   // show bottom bar
  }

}

Now access above method in fragment using MainActivity context by,

mainActivity.visibilityOfBottom(false);

I have bottom nav bar define in the Main activity. I have three fragments linked with BottomNavigation bar in fragments I have recycler view so I want to hide BottomNavigation bar when RecyclerView scrolls down and shows when RecyclerView scrolls up. My problem is how can I access BottomNavigation bar in fragments because it is defined in MainActivity.

This is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp"
    android:background="@color/colorPrimary"
    android:paddingBottom="7dp"
    android:fitsSystemWindows="true">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <Spinner
            android:layout_width="110dp"
            android:layout_height="50dp"
            android:id="@+id/chooseLocation"
            app:backgroundTint="@android:color/white"/>

    </android.support.v7.widget.Toolbar>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:id="@+id/search"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:paddingRight="6dp"
        android:paddingLeft="12dp"
        android:hint="Search here"
        android:textColorHint="#9e9e9e"
        android:textColor="#000"
        tools:ignore="HardcodedText"
        android:background="@drawable/search_edit_text"
        android:paddingEnd="6dp"
        android:paddingStart="12dp"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottomBar"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_menu"
    android:background="#fff"
    app:itemIconTint="@drawable/nav_check"
    app:itemTextColor="@drawable/nav_check"/>

</android.support.design.widget.CoordinatorLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab1Fragment"
android:background="#fff">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/purchasedBook"/>


</RelativeLayout>

This is how my fragments are defined as there is no bottom nav bar in any fragments so how can I access bottom nav bar in fragments.

Someone, please let me know any help would be appreciated.

THANKS

Kotlin

Access the navigation view from a fragment

val navBar: BottomNavigationView = activity!!.findViewById(R.id.bottomBar)

The only way I found for hiding bottom navigator in V6 was making structure like this -

Stack Nav {
   Bottom Nav
   Screen 1
   Screen 2
   Screen 3
}

Add this code to your fragment. will hide the Bottom nav.

   AppCompatActivity activity = (AppCompatActivity) view.getContext();
    chipNavigationBar = activity.findViewById(R.id.chipNavigation);
    chipNavigationBar.animate().translationY(chipNavigationBar.getHeight()).setDuration(1000);

Use this code : when Scrolling down the Recyclerview to your fragment will hide the bottom navigation. then when Scrolled Up it will show the Bottom nav.

private View view;
private AppCompatActivity activity;
private ChipNavigationBar chipNavigationBar;
//...............................................

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

//...........................................................

private void hide_NavigationBar_adwhen_Scrolling() {
        activity = (AppCompatActivity) view.getContext();
        chipNavigationBar = activity.findViewById(R.id.chipNavigation);

        RecyclerView recyclerView = view.findViewById(R.id.recylerView);
       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) {//on_Scrolled_down
                //  chipNavigationBar.animate().translationY(200).setDuration(500);
               chipNavigationBar.animate().translationY(banner_ad_card_1.getHeight()).setDuration(1000);

            } else {//on_Scrolled_up
                 chipNavigationBar.setVisibility(View.VISIBLE);
                chipNavigationBar.animate().translationY(0).setDuration(1000);
                //  chipNavigationBar.setItemSelected(R.id.home, true);
            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }
    });
    }

Add

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

to

BottomNavigationView

app:layout_behavior="@string/appbar_scrolling_view_behavior"

to

RecyclerView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM