Blog of CodeJELLOW’S ENGINEERING NOTES
← 返回文章

Fragment的使用

fragment是一种控制器对象,activity委派它执行管理用户界面的任务。这样在视图切换时无需销毁activity。 activity托管fragment activity在视图层级里提供一处位置,用来放置fragment 使用支持库版Fragment …

本文目录 ⌄

    fragment是一种控制器对象,activity委派它执行管理用户界面的任务。这样在视图切换时无需销毁activity。

    activity托管fragment

    activity在视图层级里提供一处位置,用来放置fragment

    图片暂不可用。

    使用支持库版Fragment

    TEXT
    implementation 'androidx.appcompat:appcompat:1.1.0'

    activity需要继承AppCompatActivity

    fragment的生命周期

    fragment的生命周期与Activity一致,都需要经过onCreat,onStart,onResume,onPause,onStop,onDestory。

    定义容器视图

    想要使用fragment,首先要在activity中添加容器视图,这里新建一个通用的FrameLayout。替换原有Activity.xml布局文件。

    TEXT
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".CrimeActivity">
    
    </FrameLayout>

    创建UI fragment

    下面写一个想展示出的页面布局

    TEXT
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
                  android:layout_margin="16dp"
                  android:orientation="vertical"
                 tools:context=".CrimeFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:text="@string/crime_title_label"/>
        <EditText android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:id="@+id/crime_title"
                  android:hint="@string/crime_title_hint" />
        <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
                  style="?android:listSeparatorTextViewStyle"
                  android:text="@string/crime_details_label"/>
        <Button android:layout_width="match_parent" android:layout_height="wrap_content"
                android:id="@+id/crime_date"/>
        <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:id="@+id/crime_solved"
                  android:text="@string/crime_solved_label"/>
    
    </LinearLayout>

    创建Fragment.java类

    继承fragment类

    重写onCreate方法,此方法不进行创建fragment视图。

    重写onCreatView方法,该方法实例化fragment布局,然后将实例化的View返回给托管的activity。

    向FragmentManger添加UI fragment

    注意两个方法的区别为:

    • onCreate是指创建该fragment,类似于Activity.onCreate,你可以在其中初始化除了view之外的东西;
    • onCreateView是创建该fragment对应的视图,你必须在这里创建自己的视图并返回给调用者。

    FragmentManger负责管理fragment,并添加到activity中

    这里重写了一个类,当activity初始化时继承这个类,里面创建了FragmentManger,当没有现成的fragment时,就创建新的。

    建议写每个activity都调用fragment,防止后期出现问题。

    通过 RSS 订阅更新 →