• Navigation 组件(一) Fragment 跳转


    1. App Module 库 build.gradle 文件中添加依赖库或者添加xml文件刷新自动添加库

    1. dependencies {
    2. implementation 'androidx.navigation:navigation-fragment:2.5.1'
    3. implementation 'androidx.navigation:navigation-ui:2.5.1'
    4. }

    2. 创建 HomeFragment,右击包名,New -> Fragment -> Fragment(Blank),填入 Fragment 和 xml 文件名称

      2.1 HomeFragment.java

    1. public class HomeFragment extends Fragment {
    2. @Override
    3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    4. return inflater.inflate(R.layout.fragment_home, container, false);
    5. }
    6. @Override
    7. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    8. super.onViewCreated(view, savedInstanceState);
    9. view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    10. @Override
    11. public void onClick(View view) {
    12. NavController controller = Navigation.findNavController(view);
    13. controller.navigate(R.id.action_homeFragment_to_detailFragment);
    14. }
    15. });
    16. }
    17. }

      2.2 fragment_home.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout 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=".HomeFragment">
    8. <androidx.constraintlayout.widget.ConstraintLayout
    9. android:layout_width="match_parent"
    10. android:layout_height="match_parent">
    11. <androidx.constraintlayout.widget.Guideline
    12. android:id="@+id/guideline2"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:orientation="horizontal"
    16. app:layout_constraintGuide_percent="0.5" />
    17. <TextView
    18. android:id="@+id/textView"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:text="Home"
    22. android:textSize="24sp"
    23. app:layout_constraintBottom_toTopOf="@+id/guideline2"
    24. app:layout_constraintEnd_toEndOf="parent"
    25. app:layout_constraintStart_toStartOf="parent"
    26. app:layout_constraintTop_toTopOf="parent"
    27. app:layout_constraintVertical_bias="0.8" />
    28. <Button
    29. android:id="@+id/button"
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:text="Button"
    33. app:layout_constraintBottom_toBottomOf="parent"
    34. app:layout_constraintEnd_toEndOf="parent"
    35. app:layout_constraintStart_toStartOf="parent"
    36. app:layout_constraintTop_toTopOf="@+id/guideline2"
    37. app:layout_constraintVertical_bias="0.2" />
    38. </androidx.constraintlayout.widget.ConstraintLayout>
    39. </FrameLayout>

    3. 创建 DetailFragment

      3.1 DetailFragment.java

    1. public class DetailFragment extends Fragment {
    2. @Override
    3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    4. return inflater.inflate(R.layout.fragment_detail, container, false);
    5. }
    6. @Override
    7. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    8. super.onViewCreated(view, savedInstanceState);
    9. view.findViewById(R.id.button2).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment));
    10. }
    11. }

      3.2 fragment_detail.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout 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=".DetailFragment">
    8. <androidx.constraintlayout.widget.ConstraintLayout
    9. android:layout_width="match_parent"
    10. android:layout_height="match_parent">
    11. <androidx.constraintlayout.widget.Guideline
    12. android:id="@+id/guideline3"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:orientation="horizontal"
    16. app:layout_constraintGuide_percent="0.5" />
    17. <TextView
    18. android:id="@+id/textView2"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:text="Detail"
    22. android:textSize="24sp"
    23. app:layout_constraintBottom_toTopOf="@+id/guideline3"
    24. app:layout_constraintEnd_toEndOf="parent"
    25. app:layout_constraintStart_toStartOf="parent"
    26. app:layout_constraintTop_toTopOf="parent"
    27. app:layout_constraintVertical_bias="0.8" />
    28. <Button
    29. android:id="@+id/button2"
    30. android:layout_width="wrap_content"
    31. android:layout_height="wrap_content"
    32. android:text="Button"
    33. app:layout_constraintBottom_toBottomOf="parent"
    34. app:layout_constraintEnd_toEndOf="parent"
    35. app:layout_constraintHorizontal_bias="0.5"
    36. app:layout_constraintStart_toStartOf="parent"
    37. app:layout_constraintTop_toTopOf="@+id/guideline3"
    38. app:layout_constraintVertical_bias="0.2" />
    39. </androidx.constraintlayout.widget.ConstraintLayout>
    40. </FrameLayout>

    4. 创建 Navigation, 右击 res 文件,选择 New -> Android Resource File, 文件名为 my_navigation.xml ,资源文件类型为 Navigation, 添加 fragment xml 文件。

      4.1 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/homeFragment">
    7. <fragment
    8. android:id="@+id/homeFragment"
    9. android:name="com.example.navigationdemo.HomeFragment"
    10. android:label="Home"
    11. tools:layout="@layout/fragment_home">
    12. <action
    13. android:id="@+id/action_homeFragment_to_detailFragment"
    14. app:destination="@id/detailFragment"
    15. app:enterAnim="@anim/nav_default_enter_anim"
    16. app:exitAnim="@anim/nav_default_exit_anim" />
    17. </fragment>
    18. <fragment
    19. android:id="@+id/detailFragment"
    20. android:name="com.example.navigationdemo.DetailFragment"
    21. android:label="Detail"
    22. tools:layout="@layout/fragment_detail">
    23. <action
    24. android:id="@+id/action_detailFragment_to_homeFragment"
    25. app:destination="@id/homeFragment"
    26. app:enterAnim="@anim/nav_default_enter_anim"
    27. app:exitAnim="@anim/nav_default_exit_anim" />
    28. </fragment>
    29. </navigation>

    5. Activity调用

      5.1 activity_main.xml 文件,画板中,选择 Containers -> NavHostFragment -> my_navigation

    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>

      5.2 MainActivity.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. }

    6. 效果图

        

  • 相关阅读:
    JavaScript 30 JavaScript 日期格式
    数字藏品交易系统有哪些特点?
    0 基础 Java 自学之路(5)
    java异常处理
    公司一般怎样筛选简历
    在DevTool的源代码栏打开控制台界面
    【附源码】计算机毕业设计SSM网上购物系统
    数据结构-Prim算法构造无向图的最小生成树
    【python】bin/dec/hex/bnr进制转换函数及fp32转十六进制
    Scheduled定时任务
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126581738