如何在底部导航栏保持存在的情况下启动不同的布局

How to Launch different layouts while bottom nav bar stays present

如何在底部导航栏始终保留在屏幕上的同时启动不同的活动。例如,如果我单击底部导航栏中的选项卡,它会启动 google 地图视图,我如何才能使底部导航栏仍然可见。非常像 youtube 应用程序,底部导航栏始终存在。如果我能被指出正确的方向那会很棒我可以自己做研究

如果你想使用相同的底部导航视图,那么你必须使用片段。 activity 你做不到。这是最佳做法。

是的,您也可以将底部导航与 activity 一起使用,但您必须创建所有其他 activity 的导航视图并设置侦听器。前任。就像 youtube,可以说,1st activity 是带导航视图的家。然后单击 "trending",趋势 activity 打开。在趋势 activity 中,您必须再次创建导航视图。

Fragment 是 Activity 的模块化部分,它有自己的生命周期,有自己的布局、视图..,(有点像"sub activity") 使用片段的另一个明显优势,UI 跨不同屏幕的优化,它让您可以在一个 activity 中管理多个页面。 现在按照以下步骤操作:

  1. 让你的主要 Acitivy 扩展 FragmentActivity :

    public class MainActivity extends FragmentActivity {
    ...
    }
    
  2. 将主要 activity 布局更改为如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.yourComp.appName.MainActivity">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_above="@id/footer" />
    
        <LinearLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" />
    
    </RelativeLayout>
    
  3. 创建一个新的 FragmentPagerAdapter 如下所示,我定义了 3 个页面,但您可能想要更改它,记住您必须创建一个 Fragment而不是每个 activity 你需要 :

    public class FragmentAdapter extends FragmentPagerAdapter {
        static final int NUMBER_OF_PAGES = 3;
        Context context;
    
        public FragmentAdapter(android.support.v4.app.FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }
    
        @Override
        public int getCount() {
            return NUMBER_OF_PAGES;
        }
    
        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                case 2:
                    return new Fragment3();
                default:
                    return null;
          }
       }
    }
    
  4. 现在转到 Android Studio,单击 File > New > Fragment > Fragment(Blank),然后创建片段列在FragmentAdapter之前。此 fragments 代替您的活动起作用。

  5. 最后将这行添加到您的main Activity, OnCreate 方法中,目的是将之前声明的片段与viewPager[=51 绑定=] 在主要 activity 布局上:

    ragmentAdapter = new FragmentAdapter(((FragmentActivity) this).getSupportFragmentManager(), this);
    ViewPager pager = (ViewPager) findViewById(R.id.body);
    viewPager.setAdapter(fragmentAdapter);
    

现在设计您自己的自定义页脚视图或使用 BottomNavigationView。用户可以在片段之间滑动,或者您可以使用此方法以编程方式更改页面:

viewPager.setCurrentItem(n);  // n is fragment number that decleared in adapter 0,1,2

此外,您可能希望禁用用户手动滑动,如 here 所述。