• Navigation 组件(三) ViewModel,LiveData,DataBinding 组合使用


    1. Module 中 build.gradle 配置文件添加引用库

    1. android {
    2. //AS 4.1 之后写法
    3. buildFeatures {
    4. dataBinding = true
    5. }
    6. }
    7. dependencies {
    8. implementation 'androidx.navigation:navigation-fragment:2.5.1'
    9. implementation 'androidx.navigation:navigation-ui:2.5.1'
    10. }

    2. MasterFragment页面

      2.1 MasterFragment.java

    1. public class MasterFragment extends Fragment {
    2. MyViewModel myViewModel;
    3. FragmentMasterBinding binding;
    4. @Override
    5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    6. myViewModel = new ViewModelProvider(getActivity()).get(MyViewModel.class);
    7. binding = DataBindingUtil.inflate(inflater, R.layout.fragment_master, container, false);
    8. binding.setData(myViewModel);
    9. binding.setLifecycleOwner(getActivity());
    10. binding.button.setOnClickListener(new View.OnClickListener() {
    11. @Override
    12. public void onClick(View view) {
    13. NavController controller = Navigation.findNavController(view);
    14. controller.navigate(R.id.action_masterFragment_to_detailFragment);
    15. }
    16. });
    17. binding.seekBar.setProgress(myViewModel.getNumber().getValue());
    18. binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    19. @Override
    20. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    21. myViewModel.getNumber().setValue(progress);
    22. }
    23. @Override
    24. public void onStartTrackingTouch(SeekBar seekBar) {
    25. }
    26. @Override
    27. public void onStopTrackingTouch(SeekBar seekBar) {
    28. }
    29. });
    30. return binding.getRoot();
    31. }
    32. }

      2.2 fragment_master.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools">
    5. <data>
    6. <variable
    7. name="data"
    8. type="com.example.navviewmodel.MyViewModel" />
    9. </data>
    10. <androidx.constraintlayout.widget.ConstraintLayout
    11. android:layout_width="match_parent"
    12. android:layout_height="match_parent"
    13. tools:context=".MasterFragment">
    14. <TextView
    15. android:id="@+id/textView"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:text="@{String.valueOf(data.getNumber())}"
    19. android:textSize="24sp"
    20. app:layout_constraintBottom_toBottomOf="parent"
    21. app:layout_constraintEnd_toEndOf="parent"
    22. app:layout_constraintStart_toStartOf="parent"
    23. app:layout_constraintTop_toTopOf="parent"
    24. app:layout_constraintVertical_bias="0.224" />
    25. <SeekBar
    26. android:id="@+id/seekBar"
    27. android:layout_width="0dp"
    28. android:layout_height="wrap_content"
    29. android:max="10"
    30. app:layout_constraintBottom_toBottomOf="parent"
    31. app:layout_constraintEnd_toEndOf="parent"
    32. app:layout_constraintStart_toStartOf="parent"
    33. app:layout_constraintTop_toTopOf="parent"
    34. app:layout_constraintVertical_bias="0.39" />
    35. <Button
    36. android:id="@+id/button"
    37. android:layout_width="wrap_content"
    38. android:layout_height="wrap_content"
    39. android:text="进入"
    40. android:textSize="20sp"
    41. app:layout_constraintBottom_toBottomOf="parent"
    42. app:layout_constraintEnd_toEndOf="parent"
    43. app:layout_constraintStart_toStartOf="parent"
    44. app:layout_constraintTop_toTopOf="parent" />
    45. </androidx.constraintlayout.widget.ConstraintLayout>
    46. </layout>

    3. DetailFragment 页面

      3.1 DetailFragment.java

    1. public class DetailFragment extends Fragment {
    2. MyViewModel myViewModel;
    3. FragmentDetailBinding binding;
    4. @Override
    5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    6. myViewModel = new ViewModelProvider(getActivity()).get(MyViewModel.class);
    7. binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false);
    8. binding.setData(myViewModel);
    9. binding.setLifecycleOwner(getActivity());
    10. binding.button4.setOnClickListener(new View.OnClickListener() {
    11. @Override
    12. public void onClick(View view) {
    13. NavController controller = Navigation.findNavController(view);
    14. controller.navigate(R.id.action_detailFragment_to_masterFragment);
    15. }
    16. });
    17. return binding.getRoot();
    18. }
    19. }

      3.2 fragment_detail.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools">
    5. <data>
    6. <variable
    7. name="data"
    8. type="com.example.navviewmodel.MyViewModel" />
    9. </data>
    10. <androidx.constraintlayout.widget.ConstraintLayout
    11. android:layout_width="match_parent"
    12. android:layout_height="match_parent"
    13. tools:context=".DetailFragment">
    14. <TextView
    15. android:id="@+id/textView2"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:text="@{String.valueOf(data.number)}"
    19. android:textSize="24sp"
    20. app:layout_constraintBottom_toBottomOf="parent"
    21. app:layout_constraintEnd_toEndOf="parent"
    22. app:layout_constraintHorizontal_bias="0.498"
    23. app:layout_constraintStart_toStartOf="parent"
    24. app:layout_constraintTop_toTopOf="parent"
    25. app:layout_constraintVertical_bias="0.316" />
    26. <Button
    27. android:id="@+id/button2"
    28. android:layout_width="wrap_content"
    29. android:layout_height="wrap_content"
    30. android:onClick="@{()->data.add(-1)}"
    31. android:text="—"
    32. android:textSize="24sp"
    33. app:layout_constraintBottom_toBottomOf="parent"
    34. app:layout_constraintEnd_toStartOf="@+id/button3"
    35. app:layout_constraintHorizontal_bias="0.5"
    36. app:layout_constraintStart_toStartOf="parent"
    37. app:layout_constraintTop_toTopOf="parent" />
    38. <Button
    39. android:id="@+id/button3"
    40. android:layout_width="wrap_content"
    41. android:layout_height="wrap_content"
    42. android:onClick="@{()->data.add(1)}"
    43. android:text="+"
    44. android:textSize="24sp"
    45. app:layout_constraintBottom_toBottomOf="@+id/button2"
    46. app:layout_constraintEnd_toEndOf="parent"
    47. app:layout_constraintHorizontal_bias="0.5"
    48. app:layout_constraintStart_toEndOf="@+id/button2"
    49. app:layout_constraintTop_toTopOf="@+id/button2" />
    50. <Button
    51. android:id="@+id/button4"
    52. android:layout_width="wrap_content"
    53. android:layout_height="wrap_content"
    54. android:text="返回"
    55. app:layout_constraintBottom_toBottomOf="parent"
    56. app:layout_constraintEnd_toEndOf="parent"
    57. app:layout_constraintStart_toStartOf="parent"
    58. app:layout_constraintTop_toTopOf="parent"
    59. app:layout_constraintVertical_bias="0.65" />
    60. </androidx.constraintlayout.widget.ConstraintLayout>
    61. </layout>

    4. 创建 my_navigation.xml 导航控制器

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:id="@+id/my_navigation"
    6. app:startDestination="@id/masterFragment">
    7. <fragment
    8. android:id="@+id/masterFragment"
    9. android:name="com.example.navviewmodel.MasterFragment"
    10. android:label="fragment_master"
    11. tools:layout="@layout/fragment_master" >
    12. <action
    13. android:id="@+id/action_masterFragment_to_detailFragment"
    14. app:destination="@id/detailFragment" />
    15. </fragment>
    16. <fragment
    17. android:id="@+id/detailFragment"
    18. android:name="com.example.navviewmodel.DetailFragment"
    19. android:label="fragment_detail"
    20. tools:layout="@layout/fragment_detail" >
    21. <action
    22. android:id="@+id/action_detailFragment_to_masterFragment"
    23. app:destination="@id/masterFragment" />
    24. </fragment>
    25. </navigation>

    5. MainActivity 中调用

      5.1 MainActivty.java

    1. public class MainActivity extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.activity_main);
    6. NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
    7. NavController controller = navHostFragment.getNavController();
    8. NavigationUI.setupActionBarWithNavController(this, controller);
    9. }
    10. @Override
    11. public boolean onSupportNavigateUp() {
    12. NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
    13. NavController controller = navHostFragment.getNavController();
    14. return controller.navigateUp();
    15. }
    16. }

      5.2 activity_main.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity">
    8. <androidx.fragment.app.FragmentContainerView
    9. android:id="@+id/fragmentContainerView"
    10. android:name="androidx.navigation.fragment.NavHostFragment"
    11. android:layout_width="match_parent"
    12. android:layout_height="match_parent"
    13. app:defaultNavHost="true"
    14. app:navGraph="@navigation/my_navigation" />
    15. </androidx.constraintlayout.widget.ConstraintLayout>

    6. 效果图

       

  • 相关阅读:
    CompletableFuture 异步编排、案例及应用小案例
    小程序自定义组件(附“我的“页面item组件代码)
    数字IC/FPGA——锁存器/触发器/寄存器
    【Java面向对象】封装的认识与实现
    Pytest_fixture装饰器、调用fixture的三种方法、usefixtures与传fixture区别、fixture自动使用autouse=True
    idea关闭异常服务器还在运行解决方法
    分析少年派2中的Crypto
    MapReduce概述
    第三方软件测试服务有哪些形式?选择时如何避雷?
    24 Network Requests and Remote Resources
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126602673