fix: 解决底部导航栏无法切换问题

This commit is contained in:
niushuai233 2022-10-13 14:44:59 +08:00
parent fa29a7cd74
commit c3346234fe
4 changed files with 22 additions and 10 deletions

View File

@ -1,8 +1,11 @@
package cc.niushuai.project.devcontrol; package cc.niushuai.project.devcontrol;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.navigation.NavController; import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation; import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.AppBarConfiguration;
@ -10,6 +13,7 @@ import androidx.navigation.ui.NavigationUI;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
@ -57,6 +61,14 @@ public class MainActivity extends AppCompatActivity {
// 返回键 左上角以及右上角 // 返回键 左上角以及右上角
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); // NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
// 页签点击监听
// navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
// @Override
// public void onDestinationChanged(@NonNull NavController navController, @NonNull NavDestination navDestination, @Nullable Bundle bundle) {
// Toast.makeText(MainActivity.this, "onDestinationChanged Called: " + navDestination.getDisplayName(), Toast.LENGTH_SHORT).show();
// }
// });
// 底部导航栏 // 底部导航栏
BottomNavigationView bottomNavView = this.findViewById(R.id.bottom_nav_view); BottomNavigationView bottomNavView = this.findViewById(R.id.bottom_nav_view);

View File

@ -2,17 +2,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item <item
android:id="@+id/id_bottom_nav_menu_device" android:id="@id/bottom_nav_device"
android:icon="@drawable/ic_home" android:icon="@drawable/ic_home"
android:title="@string/title_bottom_nav_menu_device" /> android:title="@string/title_bottom_nav_menu_device" />
<item <item
android:id="@+id/id_bottom_nav_menu_log" android:id="@id/bottom_nav_log"
android:icon="@drawable/ic_log" android:icon="@drawable/ic_log"
android:title="@string/title_bottom_nav_menu_log" /> android:title="@string/title_bottom_nav_menu_log" />
<item <item
android:id="@+id/id_bottom_nav_menu_set" android:id="@id/bottom_nav_set"
android:icon="@drawable/ic_dashboard" android:icon="@drawable/ic_dashboard"
android:title="@string/title_bottom_nav_menu_set" /> android:title="@string/title_bottom_nav_menu_set" />

View File

@ -3,20 +3,20 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_navigation" android:id="@+id/main_navigation"
app:startDestination="@id/navDeviceFragment"> app:startDestination="@id/bottom_nav_device">
<fragment <fragment
android:id="@+id/navDeviceFragment" android:id="@id/bottom_nav_device"
android:name="cc.niushuai.project.devcontrol.ui.nav.main.device.NavDeviceFragment" android:name="cc.niushuai.project.devcontrol.ui.nav.main.device.NavDeviceFragment"
android:label="@string/title_bottom_nav_menu_device" android:label="@string/title_bottom_nav_menu_device"
tools:layout="@layout/main_nav_fragment_device" /> tools:layout="@layout/main_nav_fragment_device" />
<fragment <fragment
android:id="@+id/navLogFragment" android:id="@id/bottom_nav_log"
android:name="cc.niushuai.project.devcontrol.ui.nav.main.log.NavLogFragment" android:name="cc.niushuai.project.devcontrol.ui.nav.main.log.NavLogFragment"
android:label="@string/title_bottom_nav_menu_log" android:label="@string/title_bottom_nav_menu_log"
tools:layout="@layout/main_nav_fragment_log" /> tools:layout="@layout/main_nav_fragment_log" />
<fragment <fragment
android:id="@+id/navSetUpFragment" android:id="@id/bottom_nav_set"
android:name="cc.niushuai.project.devcontrol.ui.nav.main.setup.NavSetUpFragment" android:name="cc.niushuai.project.devcontrol.ui.nav.main.setup.NavSetUpFragment"
android:label="@string/title_bottom_nav_menu_set" android:label="@string/title_bottom_nav_menu_set"
tools:layout="@layout/main_nav_fragment_set_up" /> tools:layout="@layout/main_nav_fragment_set_up" />

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<!-- 首页 底部导航栏 start--> <!-- 首页 底部导航栏 start-->
<item name="id_bottom_nav_menu_device" type="id"/> <item name="bottom_nav_device" type="id"/>
<item name="id_bottom_nav_menu_log" type="id"/> <item name="bottom_nav_log" type="id"/>
<item name="id_bottom_nav_menu_set" type="id"/> <item name="bottom_nav_set" type="id"/>
<!-- 首页 底部导航栏 end--> <!-- 首页 底部导航栏 end-->
</resources> </resources>