android fragment思想理解

    科技2025-12-18  11

    一个页面 可以有多个 fragment

    那么 怎么理解这个 fragment 呢? 可以把这个东西 看成 一个 HTML 网页, 然后 Activity 要 使用到 fragment , 就要用 一个 fragment Containe 去引入 frangment 相当于 vue 里面 的 router-view

    如何 在代码里面 将 Activity 和 fragment 绑定呢?

    使用一个 container 标签 引入 fragment

    <?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"> <!-- <TableRow--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- android:id="@+id/tableRow"--> <!-- android:layout_marginTop="1dp"--> <!-- android:layout_marginBottom="1dp"--> <!-- app:layout_constraintEnd_toEndOf="parent"--> <!-- app:layout_constraintBottom_toTopOf="@+id/app_content_list"--> <!-- app:layout_constraintTop_toTopOf="parent"--> <!-- app:layout_constraintStart_toStartOf="parent"--> <!-- android:layout_marginLeft="1dp"--> <!-- android:layout_marginStart="1dp"--> <!-- android:layout_marginEnd="1dp"--> <!-- android:layout_marginRight="1dp">--> <!-- <Button--> <!-- android:text="append"--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:id="@+id/btn_add"--> <!-- />--> <!-- </TableRow>--> <!-- <ListView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="0dp"--> <!-- android:id="@+id/app_content_list"--> <!-- app:layout_constraintTop_toBottomOf="@+id/tableRow"--> <!-- android:layout_marginBottom="1dp"--> <!-- app:layout_constraintEnd_toEndOf="parent"--> <!-- app:layout_constraintStart_toStartOf="parent"--> <!-- android:layout_marginLeft="1dp"--> <!-- app:layout_constraintBottom_toBottomOf="parent"--> <!-- android:layout_marginStart="1dp"--> <!-- android:layout_marginEnd="1dp"--> <!-- android:layout_marginRight="1dp"/>--> <fragment android:id="@+id/fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/my_navgation" /> </androidx.constraintlayout.widget.ConstraintLayout>

    这个 标签 的一个属性引用 navigation 目录下的一个 xml,用来定义多个页面的关系【路由】,页面如何跳转

    navigation 定义了 fragment 如何跳转

    java 代码实现绑定 navigation 和Activity

    package com.example.myapplication; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.NavigationUI; import com.example.myapplication.core.BaseAnnotationActivity; import com.example.myapplication.core.annon.BindView; /** * @Author lyr * @create 2020/9/24 14:02 */ @BindView(view = R.layout.app_content) public class ContentActivity extends BaseAnnotationActivity { // // @BindView(view = R.id.tabLayout) // // TabLayout tabLayout; // @BindView(view = R.id.app_content_list) // ListView listView; // @BindView(view = R.id.btn_add) // Button btnAdd; // LinkedList<String> data = new LinkedList<>(); /** * 自定义回调 */ @Override protected void onCreateCustom() { // btnBack.setOnClickListener(this); // // data.add("666"); // data.add("abc"); // data.add("456"); // ArrayAdapter<String> adapter = new ArrayAdapter<>( // this, // R.layout.content_item, // data); // // TabLayout tabLayout = findViewById(R.id.tabLayout); // listView.setAdapter(adapter); // btnAdd.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // data.add("66,你点击了一下"); // adapter.notifyDataSetChanged(); // } // }); NavController controller = Navigation .findNavController(this,R.id.fragment); NavigationUI.setupActionBarWithNavController(this,controller); } @Override public boolean onSupportNavigateUp() { NavController controller = Navigation.findNavController(this,R.id.fragment); return controller.navigateUp(); } }
    Processed: 0.025, SQL: 10