mirror of
				https://github.com/niushuai233/DevControl.git
				synced 2024-10-27 22:43:20 +08:00 
			
		
		
		
	feat: 首页导航图
无法切换页面
This commit is contained in:
		| @@ -1,14 +1,65 @@ | ||||
| package cc.niushuai.project.devcontrol; | ||||
|  | ||||
| import androidx.appcompat.app.AppCompatActivity; | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.navigation.NavController; | ||||
| import androidx.navigation.Navigation; | ||||
| import androidx.navigation.fragment.NavHostFragment; | ||||
| import androidx.navigation.ui.AppBarConfiguration; | ||||
| import androidx.navigation.ui.NavigationUI; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.view.Menu; | ||||
|  | ||||
| import com.google.android.material.bottomnavigation.BottomNavigationView; | ||||
|  | ||||
| import cc.niushuai.project.devcontrol.databinding.ActivityMainBinding; | ||||
|  | ||||
| public class MainActivity extends AppCompatActivity { | ||||
|  | ||||
|     private ActivityMainBinding activityMainBinding; | ||||
|  | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
|         super.onCreate(savedInstanceState); | ||||
|         setContentView(R.layout.activity_main); | ||||
|  | ||||
|         activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater()); | ||||
|         setContentView(activityMainBinding.getRoot()); | ||||
|  | ||||
|         // 设置底部导航栏 | ||||
|         setBottomNavigationView(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 设置底部导航栏 | ||||
|      * | ||||
|      * @author niushuai | ||||
|      * @date: 2022/10/13 10:42 | ||||
|      */ | ||||
|     private void setBottomNavigationView() { | ||||
|  | ||||
|         // 导航栏控制器 获取navController的方式不同 | ||||
|         // 方式一 | ||||
|         NavController navController = Navigation.findNavController(this, R.id.main_nav_host_fragment_activity); | ||||
|         // 方式二 | ||||
| //        NavHostFragment fragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.main_nav_host_fragment_activity); | ||||
| //        NavController navController = fragment.getNavController(); | ||||
|  | ||||
|         // 顶部页签  右上角展开设置项 | ||||
| //        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( | ||||
| //                // 设备页签 | ||||
| //                R.id.id_bottom_nav_menu_device, | ||||
| //                // 日志页签 | ||||
| //                R.id.id_bottom_nav_menu_log, | ||||
| //                // 设置页签 | ||||
| //                R.id.id_bottom_nav_menu_set | ||||
| //        ).build(); | ||||
| //        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); | ||||
|  | ||||
|         // 底部导航栏 | ||||
|         BottomNavigationView bottomNavView = this.findViewById(R.id.bottom_nav_view); | ||||
|  | ||||
|         // 底部栏显示 | ||||
|         NavigationUI.setupWithNavController(activityMainBinding.bottomNavView, navController); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,45 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.device; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.lifecycle.ViewModelProvider; | ||||
|  | ||||
| import cc.niushuai.project.devcontrol.databinding.MainNavFragmentDeviceBinding; | ||||
|  | ||||
| /** | ||||
|  * 设备页面ui | ||||
|  * | ||||
|  * @author niushuai | ||||
|  * @date: 2022/10/13 10:58 | ||||
|  */ | ||||
| public class NavDeviceFragment extends Fragment { | ||||
|  | ||||
|     private MainNavFragmentDeviceBinding navFragmentDeviceBinding; | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
|                              Bundle savedInstanceState) { | ||||
|  | ||||
|         navFragmentDeviceBinding = MainNavFragmentDeviceBinding.inflate(inflater, container, false); | ||||
|  | ||||
|         NavDeviceViewModel navDeviceViewModel = new ViewModelProvider(this).get(NavDeviceViewModel.class); | ||||
|         View rootView = navFragmentDeviceBinding.getRoot(); | ||||
|  | ||||
|         TextView textView = navFragmentDeviceBinding.navDeviceFragmentTextview; | ||||
|  | ||||
|         navDeviceViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | ||||
|  | ||||
|         return rootView; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onDestroyView() { | ||||
|         super.onDestroyView(); | ||||
|         navFragmentDeviceBinding = null; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,18 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.device; | ||||
|  | ||||
| import androidx.lifecycle.MutableLiveData; | ||||
| import androidx.lifecycle.ViewModel; | ||||
|  | ||||
| public class NavDeviceViewModel extends ViewModel { | ||||
|  | ||||
|     private final MutableLiveData<String> vText; | ||||
|  | ||||
|     public NavDeviceViewModel() { | ||||
|         this.vText = new MutableLiveData<>(); | ||||
|         this.vText.setValue("this is device view"); | ||||
|     } | ||||
|  | ||||
|     public MutableLiveData<String> getText() { | ||||
|         return this.vText; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,41 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.log; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.lifecycle.ViewModelProvider; | ||||
|  | ||||
| import cc.niushuai.project.devcontrol.databinding.MainNavFragmentLogBinding; | ||||
|  | ||||
|  | ||||
| public class NavLogFragment extends Fragment { | ||||
|  | ||||
|     private MainNavFragmentLogBinding navFragmentLogBinding; | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
|                              Bundle savedInstanceState) { | ||||
|  | ||||
|         navFragmentLogBinding = MainNavFragmentLogBinding.inflate(inflater, container, false); | ||||
|  | ||||
|         NavLogViewModel navLogViewModel = new ViewModelProvider(this).get(NavLogViewModel.class); | ||||
|         View rootView = navFragmentLogBinding.getRoot(); | ||||
|  | ||||
|         TextView textView = navFragmentLogBinding.navLogFragmentTextview; | ||||
|  | ||||
|         navLogViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | ||||
|  | ||||
|         return rootView; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onDestroyView() { | ||||
|         super.onDestroyView(); | ||||
|         navFragmentLogBinding = null; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,18 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.log; | ||||
|  | ||||
| import androidx.lifecycle.MutableLiveData; | ||||
| import androidx.lifecycle.ViewModel; | ||||
|  | ||||
| public class NavLogViewModel extends ViewModel { | ||||
|  | ||||
|     private final MutableLiveData<String> vText; | ||||
|  | ||||
|     public NavLogViewModel() { | ||||
|         this.vText = new MutableLiveData<>(); | ||||
|         this.vText.setValue("this is log view"); | ||||
|     } | ||||
|  | ||||
|     public MutableLiveData<String> getText() { | ||||
|         return this.vText; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,40 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.setup; | ||||
|  | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.lifecycle.ViewModelProvider; | ||||
|  | ||||
| import cc.niushuai.project.devcontrol.databinding.MainNavFragmentSetUpBinding; | ||||
|  | ||||
|  | ||||
| public class NavSetUpFragment extends Fragment { | ||||
|  | ||||
|     private MainNavFragmentSetUpBinding navFragmentSetUpBinding; | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
|                              Bundle savedInstanceState) { | ||||
|  | ||||
|         navFragmentSetUpBinding = MainNavFragmentSetUpBinding.inflate(inflater, container, false); | ||||
|  | ||||
|         NavSetUpViewModel navSetUpViewModel = new ViewModelProvider(this).get(NavSetUpViewModel.class); | ||||
|         View rootView = navFragmentSetUpBinding.getRoot(); | ||||
|  | ||||
|         TextView textView = navFragmentSetUpBinding.navSetupFragmentTextview; | ||||
|  | ||||
|         navSetUpViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | ||||
|  | ||||
|         return rootView; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onDestroyView() { | ||||
|         super.onDestroyView(); | ||||
|         navFragmentSetUpBinding = null; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,18 @@ | ||||
| package cc.niushuai.project.devcontrol.ui.nav.main.setup; | ||||
|  | ||||
| import androidx.lifecycle.MutableLiveData; | ||||
| import androidx.lifecycle.ViewModel; | ||||
|  | ||||
| public class NavSetUpViewModel extends ViewModel { | ||||
|     private final MutableLiveData<String> vText; | ||||
|  | ||||
|     public NavSetUpViewModel() { | ||||
|         this.vText = new MutableLiveData<>(); | ||||
|         this.vText.setValue("this is setup view"); | ||||
|     } | ||||
|  | ||||
|     public MutableLiveData<String> getText() { | ||||
|         return this.vText; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,18 +1,35 @@ | ||||
| <?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/id_am_container" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     tools:context=".MainActivity"> | ||||
|     android:paddingTop="?attr/actionBarSize"> | ||||
|  | ||||
|     <TextView | ||||
|         android:layout_width="wrap_content" | ||||
|     <com.google.android.material.bottomnavigation.BottomNavigationView | ||||
|         android:id="@+id/bottom_nav_view" | ||||
|         android:layout_width="0dp" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:text="Hello World!" | ||||
|         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:layout_constraintTop_toTopOf="parent" /> | ||||
|         app:menu="@menu/main_nav_menu" /> | ||||
|  | ||||
|     <fragment | ||||
|         android:id="@+id/main_nav_host_fragment_activity" | ||||
|         android:name="androidx.navigation.fragment.NavHostFragment" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         app:defaultNavHost="true" | ||||
|         app:layout_constraintBottom_toTopOf="@id/bottom_nav_view" | ||||
|         app:layout_constraintHorizontal_bias="0.0" | ||||
|         app:layout_constraintLeft_toLeftOf="parent" | ||||
|         app:layout_constraintRight_toRightOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         app:layout_constraintVertical_bias="1.0" | ||||
|         app:navGraph="@navigation/main_navigation" /> | ||||
|  | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
							
								
								
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_device.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_device.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <androidx.constraintlayout.widget.ConstraintLayout 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" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     tools:context=".ui.nav.main.device.NavDeviceFragment"> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/nav_device_fragment_textview" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:textAlignment="center" | ||||
|         android:text="@string/hello_blank_fragment" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent"/> | ||||
|  | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
							
								
								
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_log.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_log.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <androidx.constraintlayout.widget.ConstraintLayout 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" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     tools:context=".ui.nav.main.log.NavLogFragment"> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/nav_log_fragment_textview" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:textAlignment="center" | ||||
|         android:text="@string/hello_blank_fragment" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent"/> | ||||
|  | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
							
								
								
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_set_up.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/src/main/res/layout/main_nav_fragment_set_up.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| <?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:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     tools:context=".ui.nav.main.setup.NavSetUpFragment"> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/nav_setup_fragment_textview" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:text="@string/hello_blank_fragment" | ||||
|         android:textAlignment="center" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" /> | ||||
|  | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
							
								
								
									
										19
									
								
								app/src/main/res/menu/main_nav_menu.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								app/src/main/res/menu/main_nav_menu.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/id_bottom_nav_menu_device" | ||||
|         android:icon="@drawable/ic_home" | ||||
|         android:title="@string/title_bottom_nav_menu_device" /> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/id_bottom_nav_menu_log" | ||||
|         android:icon="@drawable/ic_log" | ||||
|         android:title="@string/title_bottom_nav_menu_log" /> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/id_bottom_nav_menu_set" | ||||
|         android:icon="@drawable/ic_dashboard" | ||||
|         android:title="@string/title_bottom_nav_menu_set" /> | ||||
|  | ||||
| </menu> | ||||
							
								
								
									
										23
									
								
								app/src/main/res/navigation/main_navigation.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								app/src/main/res/navigation/main_navigation.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| <?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/main_navigation" | ||||
|     app:startDestination="@id/navDeviceFragment"> | ||||
|  | ||||
|     <fragment | ||||
|         android:id="@+id/navDeviceFragment" | ||||
|         android:name="cc.niushuai.project.devcontrol.ui.nav.main.device.NavDeviceFragment" | ||||
|         android:label="@string/title_bottom_nav_menu_device" | ||||
|         tools:layout="@layout/main_nav_fragment_device" /> | ||||
|     <fragment | ||||
|         android:id="@+id/navLogFragment" | ||||
|         android:name="cc.niushuai.project.devcontrol.ui.nav.main.log.NavLogFragment" | ||||
|         android:label="@string/title_bottom_nav_menu_log" | ||||
|         tools:layout="@layout/main_nav_fragment_log" /> | ||||
|     <fragment | ||||
|         android:id="@+id/navSetUpFragment" | ||||
|         android:name="cc.niushuai.project.devcontrol.ui.nav.main.setup.NavSetUpFragment" | ||||
|         android:label="@string/title_bottom_nav_menu_set" | ||||
|         tools:layout="@layout/main_nav_fragment_set_up" /> | ||||
| </navigation> | ||||
							
								
								
									
										9
									
								
								app/src/main/res/values/ids.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								app/src/main/res/values/ids.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <resources> | ||||
|     <!-- 首页 底部导航栏 start--> | ||||
|     <item name="id_bottom_nav_menu_device" type="id"/> | ||||
|     <item name="id_bottom_nav_menu_log" type="id"/> | ||||
|     <item name="id_bottom_nav_menu_set" type="id"/> | ||||
|     <!-- 首页 底部导航栏 end--> | ||||
|  | ||||
| </resources> | ||||
| @@ -1,3 +1,10 @@ | ||||
| <resources> | ||||
|     <string name="app_name">DevControl</string> | ||||
|     <!-- 首页 底部导航栏 start--> | ||||
|     <string name="title_bottom_nav_menu_device">设备</string> | ||||
|     <string name="title_bottom_nav_menu_log">日志</string> | ||||
|     <string name="title_bottom_nav_menu_set">设置</string> | ||||
|     <!-- TODO: Remove or change this placeholder text --> | ||||
|     <string name="hello_blank_fragment">Hello blank fragment</string> | ||||
|     <!-- 首页 底部导航栏 end--> | ||||
| </resources> | ||||
		Reference in New Issue
	
	Block a user