个性化阅读
专注于IT技术分析

android的fragment片段

本文概述

Android Fragment是活动的一部分,也称为子活动。一个活动中可以有多个片段。片段代表一个活动中的多个屏幕。

Android片段生命周期受活动生命周期的影响,因为片段包含在活动中。

每个片段都有自己的生命周期方法,这些方法受活动生命周期的影响,因为片段嵌入在活动中。

FragmentManager类负责在片段对象之间进行交互。

Android片段生命周期

android片段的生命周期就像活动的生命周期。片段有12种生命周期方法。

Android片段生命周期方法

没有。方法描述
1)onAttach(活动)与活动相关联时, 它仅被调用一次。
2)onCreate(捆绑)它用于初始化片段。
3)onCreateView(LayoutInflater, ViewGroup, Bundle)创建并返回视图层次结构。
4)onActivityCreated(捆绑销售)在onCreate()方法完成后调用它。
5)onViewStateRestored(捆绑)它向片段提供有关片段视图层次结构的所有保存状态已还原的信息。
6)onStart()使片段可见。
7)onResume()使片段具有交互性。
8)onPause()当片段不再互动时调用。
9)onStop()当片段不再可见时调用。
10)onDestroyView()允许片段清理资源。
11)onDestroy()允许片段对片段状态进行最终清理。
12)onDetach()在片段不再与其活动相关联之前立即调用它。

Android片段示例

让我们看一下android片段的简单示例。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
    android:layout_height="fill_parent"
    tools:context="example.srcmini02.com.fragmentexample.MainActivity">

    <fragment
        android:id="@+id/fragment1"
        android:name="example.srcmini02.com.fragmentexample.Fragment1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />

    <fragment
        android:id="@+id/fragment2"
        android:name="example.srcmini02.com.fragmentexample.Fragment2"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />

</LinearLayout>
<FrameLayout 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:background="#F5F5DC"
    tools:context="example.srcmini02.com.fragmentexample.Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
<FrameLayout 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:background="#F0FFFF"
    tools:context="example.srcmini02.com.fragmentexample.Fragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

MainActivity类

package example.srcmini02.com.fragmentexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

package example.srcmini02.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment1, container, false);
    }
 }

package example.srcmini02.com.fragmentexample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment2, container, false);
    }
    
}

输出:

赞(0)
未经允许不得转载:srcmini » android的fragment片段

评论 抢沙发

评论前必须登录!