From 743bc557200974f375efb988cd20b90440d8d082 Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Thu, 13 Oct 2022 14:15:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E5=AF=BC=E8=88=AA?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 无法切换页面 --- .../project/devcontrol/MainActivity.java | 53 ++++++++++++++++++- .../ui/nav/main/device/NavDeviceFragment.java | 45 ++++++++++++++++ .../nav/main/device/NavDeviceViewModel.java | 18 +++++++ .../ui/nav/main/log/NavLogFragment.java | 41 ++++++++++++++ .../ui/nav/main/log/NavLogViewModel.java | 18 +++++++ .../ui/nav/main/setup/NavSetUpFragment.java | 40 ++++++++++++++ .../ui/nav/main/setup/NavSetUpViewModel.java | 18 +++++++ app/src/main/res/layout/activity_main.xml | 29 +++++++--- .../res/layout/main_nav_fragment_device.xml | 20 +++++++ .../main/res/layout/main_nav_fragment_log.xml | 20 +++++++ .../res/layout/main_nav_fragment_set_up.xml | 20 +++++++ app/src/main/res/menu/main_nav_menu.xml | 19 +++++++ .../main/res/navigation/main_navigation.xml | 23 ++++++++ app/src/main/res/values/ids.xml | 9 ++++ app/src/main/res/values/strings.xml | 7 +++ 15 files changed, 373 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceViewModel.java create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogFragment.java create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogViewModel.java create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpFragment.java create mode 100644 app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpViewModel.java create mode 100644 app/src/main/res/layout/main_nav_fragment_device.xml create mode 100644 app/src/main/res/layout/main_nav_fragment_log.xml create mode 100644 app/src/main/res/layout/main_nav_fragment_set_up.xml create mode 100644 app/src/main/res/menu/main_nav_menu.xml create mode 100644 app/src/main/res/navigation/main_navigation.xml create mode 100644 app/src/main/res/values/ids.xml diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java index 09a1e20..24af5fb 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java @@ -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); } } \ No newline at end of file diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java new file mode 100644 index 0000000..fe99fad --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceFragment.java @@ -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; + } +} \ No newline at end of file diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceViewModel.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceViewModel.java new file mode 100644 index 0000000..5d77a31 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/device/NavDeviceViewModel.java @@ -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 vText; + + public NavDeviceViewModel() { + this.vText = new MutableLiveData<>(); + this.vText.setValue("this is device view"); + } + + public MutableLiveData getText() { + return this.vText; + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogFragment.java new file mode 100644 index 0000000..bc424ce --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogFragment.java @@ -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; + } + +} \ No newline at end of file diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogViewModel.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogViewModel.java new file mode 100644 index 0000000..ec3b543 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/log/NavLogViewModel.java @@ -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 vText; + + public NavLogViewModel() { + this.vText = new MutableLiveData<>(); + this.vText.setValue("this is log view"); + } + + public MutableLiveData getText() { + return this.vText; + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpFragment.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpFragment.java new file mode 100644 index 0000000..a1a4596 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpFragment.java @@ -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; + } +} \ No newline at end of file diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpViewModel.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpViewModel.java new file mode 100644 index 0000000..d4701b8 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/nav/main/setup/NavSetUpViewModel.java @@ -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 vText; + + public NavSetUpViewModel() { + this.vText = new MutableLiveData<>(); + this.vText.setValue("this is setup view"); + } + + public MutableLiveData getText() { + return this.vText; + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..6b6968d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,35 @@ + android:paddingTop="?attr/actionBarSize"> - + app:menu="@menu/main_nav_menu" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/main_nav_fragment_device.xml b/app/src/main/res/layout/main_nav_fragment_device.xml new file mode 100644 index 0000000..1f43f07 --- /dev/null +++ b/app/src/main/res/layout/main_nav_fragment_device.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main_nav_fragment_log.xml b/app/src/main/res/layout/main_nav_fragment_log.xml new file mode 100644 index 0000000..aaec827 --- /dev/null +++ b/app/src/main/res/layout/main_nav_fragment_log.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main_nav_fragment_set_up.xml b/app/src/main/res/layout/main_nav_fragment_set_up.xml new file mode 100644 index 0000000..04fedde --- /dev/null +++ b/app/src/main/res/layout/main_nav_fragment_set_up.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_nav_menu.xml b/app/src/main/res/menu/main_nav_menu.xml new file mode 100644 index 0000000..11feae3 --- /dev/null +++ b/app/src/main/res/menu/main_nav_menu.xml @@ -0,0 +1,19 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/main_navigation.xml b/app/src/main/res/navigation/main_navigation.xml new file mode 100644 index 0000000..0fe7bcc --- /dev/null +++ b/app/src/main/res/navigation/main_navigation.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml new file mode 100644 index 0000000..0a28330 --- /dev/null +++ b/app/src/main/res/values/ids.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee1740b..da68338 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,10 @@ DevControl + + 设备 + 日志 + 设置 + + Hello blank fragment + \ No newline at end of file