如何在片段中隐藏底部导航栏

时间:2019-05-20 06:31:43

标签: android android-fragments

我在主活动中定义了底部导航栏。我在Recycler视图中有三个与BottomNavigation栏链接的片段,因此我想在RecyclerView向下滚动时隐藏BottomNavigation栏,并在RecyclerView向上滚动时显示。 我的问题是如何在片段中访问BottomNavigation栏,因为它是在MainActivity中定义的。

这是我的代码:

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>

这是我的片段的定义方式,因为任何片段中都没有底部导航栏,所以我如何访问片段中的底部导航栏。

有人,请让我知道任何帮助。

谢谢

7 个答案:

答案 0 :(得分:1)

尝试一下

在Xml的BottomNavigationView中添加此行

  

app:layout_behavior =“ @ string / hide_bottom_view_on_scroll_behavior”

并使用CoOrdinator布局实现此BottomNavigation行为,您可以使用滚动侦听器隐藏或显示视图。

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);
}

}

将此行代码添加到您的活动中,其中包含底部导航

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

尝试一下,让我知道Digvijay.Happy编码。

答案 1 :(得分:1)

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

BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);

答案 2 :(得分:0)

由于片段始终位于活动中,因此您可以在片段中调用getActivity()来访问活动中已存在的对象。因此您可以执行以下操作:

活动

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

片段

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

答案 3 :(得分:0)

Fragment具有onAttach()方法,可为您提供上下文。因此,您必须使用创建活动实例,

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

现在使用boolean参数的make方法可以隐藏和显示底部栏。

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

  // hide bottom bar

  } else{
   // show bottom bar
  }

}

现在使用MainActivity上下文通过片段访问上述方法,

mainActivity.visibilityOfBottom(false);

答案 4 :(得分:0)

添加

curl -F 'image=@foo.jpg' 'http://1.2.3.4/upload?bar=baz'

  

BottomNavigationView

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

  

RecyclerView

答案 5 :(得分:0)

最简单的解决方案是在public static中使用MainActivity方法,并从要隐藏底部导航栏的片段中引用它。

不要忘记将navView初始化为public static

在您的MainActivity

    public static void hideBottomNav(){
        navView.setVisibility(View.GONE);
    }

    public static void showBottomNav(){
        navView.setVisibility(View.GONE);
    }

在您的MyFragment

    @Override
    public void onResume() {
        super.onResume();
        MainActivity.hideBottomNav();
    }

    @Override
    public void onStop() {
        super.onStop();
        MainActivity.showBottomNav();
    }

答案 6 :(得分:0)

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.full_screen_destination) {
       
       bottomNavigationView.visibility = View.GONE
   } else {
       
       bottomNavigationView.visibility = View.VISIBLE
   }
}

在主要活动中执行此操作

相关问题