总体思想:
可以先跳转到Fragment所在的MainActivity,再通过NavController控制跳转到指定的Fragment,就可以显示出来了
// 其他的Activity界面 (即你当前所在的Activity,你需要跳转到底部导航栏的任意一项(Fragment))
Intent intent = new Intent(Details.this, MainActivity.class);
// 设置需要跳转到底部导航栏项的标志(名称随意,数字也随意,对应即可)
intent.putExtra("fragment_flag",1);
startActivity(intent);
MainActivity代码:
public class MainActivity extends AppCompatActivity {
NavController navController = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,R.id.navigation_search)
.build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
@Override
protected void onResume() {
// 获取从其他Activity发送过来跳转Fragment的标志fragment_flag(名称随意)
int fragmentFlag = this.getIntent().getIntExtra("fragment_flag", 0);
switch (fragmentFlag){
case 1:
// 控制跳转到底部导航项(navigation_home为该Fragment的对应控件的id值)
navController.navigate(R.id.navigation_home);
break;
case 2:
navController.navigate(R.id.navigation_dashboard);
break;
case 3:
navController.navigate(R.id.navigation_search);
break;
case 4:
navController.navigate(R.id.navigation_cart);
break;
case 5:
navController.navigate(R.id.navigation_notifications);
break;
}
super.onResume();
}
}
NavController用法参看
对应的fragment
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.gzpy.onlineretailerstest.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.gzpy.onlineretailerstest.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_search"
android:name="com.gzpy.onlineretailerstest.ui.search.SearchFragment"
android:label="@string/title_search"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_cart"
android:name="com.gzpy.onlineretailerstest.ui.cart.CartFragment"
android:label="@string/title_cart"
tools:layout="@layout/fragment_cart" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.gzpy.onlineretailerstest.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:paddingTop="?attr/actionBarSize" 设置此属性顶部会出现空白-->
<!--app:labelVisibilityMode="labeled"可以把文字显示出来-->
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="labeled"
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
item:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_search"
android:icon="@drawable/ic_search"
android:title="@string/title_search" />
<item
android:id="@+id/navigation_cart"
android:icon="@drawable/ic_cart"
android:title="@string/title_cart" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_me"
android:title="@string/title_notifications" />
</menu>
fragment:
public class HomeFragment extends Fragment {
// private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
// View root = inflater.inflate(R.layout.fragment_home, container, false);
// final TextView textView = root.findViewById(R.id.text_home);
// homeViewModel.getText().observe(this, new Observer() {
// @Override
// public void onChanged(@Nullable String s) {
// textView.setText(s);
// }
// });
View root = inflater.inflate(R.layout.fragment_home, container, false);
listView = root.findViewById(R.id.listView);
// 设置显示菜单
setHasOptionsMenu(true);
return root;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}