首先,在您的activity_main.xml中,我们不需要3个不同的片段,因为我们只能在1个frameLayout中替换或添加任何选定的片段。其次,只要用户从Bottom
NavigationView中选择任何一个,就只需获取相关Fragment类的实例,并将其替换为activity_main的frameLayout即可。
Fragment selectedFragment = null; switch (item.getItemId()) { case R.id.navigation_home: selectedFragment = FunFragment.newInstance(); break;在获得选定片段的实例后,将其替换为activity_main的frameLayout,以显示在屏幕上。
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content, selectedFragment); transaction.commit();
编辑标记 步骤1. 您的activity_main.xml应该如下所示
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="vertical" tools:context="com.exampl.MainActivity"> <frameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:menu="@menu/navigation" /></LinearLayout>
步骤2. 您的fragment_home.xml布局应如下所示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="android"/> </LinearLayout>
为您的三个不同选项制作3种不同的片段布局
步骤3. 您的MainActivity.java类将如下所示
private BottomNavigationView.onNavigationItemSelectedListener monNavigationItemSelectedListener = new BottomNavigationView.onNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { Fragment selectedFragment = null; switch (item.getItemId()) { case R.id.navigation_home: selectedFragment = FunFragment.newInstance(); break; case R.id.navigation_dashboard: selectedFragment = TheoryFragment.newInstance(); break; case R.id.navigation_notifications: selectedFragment = AndroidFragment.newInstance(); break; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content, selectedFragment); transaction.commit(); return true; } };@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); navigation.setonNavigationItemSelectedListener(mOnNavigationItemSelectedListener); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content, FunFragment.newInstance()); transaction.commit(); }在on create上替换为要在启动应用程序后显示的片段,导航监听器将照顾您将选择的任何选项
步骤4. 您将拥有3个不同的Fragment类,如下所示
public class TheoryFragment extends Fragment { public static TheoryFragment newInstance() { TheoryFragment fragment = new TheoryFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_theory, container, false); unbinder = ButterKnife.bind(this, rootView); return rootView; } }希望它能对您有所帮助,如有任何问题,请告诉我。



