前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Fragment最全面使用详解

Fragment最全面使用详解

作者头像
Carson.Ho
发布2019-02-22 10:13:45
2K0
发布2019-02-22 10:13:45
举报
文章被收录于专栏:Android知识分享Android知识分享

前言

Fragment在Android开发中非常常用,今天就来介绍下Fragment及其使用方法

目录

这里写图片描述
这里写图片描述

定义

Fragment是activity的界面中的一部分或一种行为

1.把Fragment认为模块化的一段activity 2.它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除 3.Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的所有的Fragment们都暂停了,当activity销毁时,它拥有的所有Fragment们都被销毁。

作用

主要是为了支持更动态、更灵活的界面设计(从3.0开始引入)

Fragment的生命周期解析

先来看张官方说明图

这里写图片描述
这里写图片描述

详细解读每个方法的调用场景

  • onAttach方法 Fragment和Activity建立关联的时候调用(获得activity的传递的值)
  • onCreateView方法 为Fragment创建视图(加载布局)时调用(给当前的fragment绘制UI布局,可以使用线程更新UI)
  • onActivityCreated方法 当Activity中的onCreate方法执行完后调用(表示activity执行oncreate方法完成了的时候会调用此方法)
  • onDestroyView方法 Fragment中的布局被移除时调用(表示fragment销毁相关联的UI布局)
  • onDetach方法 Fragment和Activity解除关联的时候调用(脱离activity)

fragment生命周期解析

  • 当一个fragment被创建的时候: onAttach() onCreate() onCreateView() onActivityCreated()
  • 当这个fragment对用户可见的时候,它会经历以下状态。 onStart() onResume() 1.2可以理解为从创建到显示(或切换)
  • 当这个fragment进入“后台模式”的时候,它会经历以下状态。 onPause() onStop()
  • 当这个fragment被销毁了(或者持有它的activity被销毁了): onPause() onStop() onDestroyView() onDestroy() onDetach()
  • 就像Activity一样,在以下的状态中,可以使用Bundle对象保存一个fragment的对象。 onCreate() onCreateView() onActivityCreated()

其他场景的调用

  • 屏幕灭掉 onPause() onSaveInstanceState() onStop()
  • 屏幕解锁 onStart() onResume()
  • 切换到其他Fragment onPause() onStop() onDestroyView()
  • 切换回本身的Fragment onCreateView() onActivityCreated() onStart() onResume()
  • 回到桌面 onPause() onSaveInstanceState() onStop()
  • 回到应用 onStart() onResume()
  • 退出应用 onPause() onStop() onDestroyView() onDestroy() onDetach()

Fragment和Activity的生命周期很相似,以下是对比图

这里写图片描述
这里写图片描述

Fragment的使用

由于Fragment作为Activity的一部分,所以Fragment的使用一般是添加到Activity中,一般有两种方法将Fragment添加到Activity中: - 方法1:在activity的layout.xml文件中声明fragment - 方法2:在activity的layout.xml文件中声明包含

方法1:在activity的layout.xml文件中声明fragment

  • layout文件fragment_layout_test.xml
代码语言:javascript
复制
<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:orientation="vertical" >


   <fragment android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

说明 1. 该layout只包含了一个fragment元素。 2. fragment的属性

代码语言:javascript
复制
android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment" 

说明该fragment是定义在包名为”com.skywang.app”中类”FragmentLayoutTest”“的内部类”ExampleFragment”中的。

  • FragmentLayoutTest.java
代码语言:javascript
复制
package com.skywang.app;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.app.Fragment;

public class FragmentLayoutTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // fragment_layout_test只包括ExampleFragment对象。
        setContentView(R.layout.fragment_layout_test);
    }

    // 继承与Fragment
    public static class ExampleFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            return inflater.inflate(R.layout.example_fragment, container, false);
        }
    }
}

说明: 1. FragmentLayoutTest通过

代码语言:javascript
复制
setContentView(R.layout.fragment_layout_test)

来调用fragment_layout_test.xml来作为布局文件。 2. 上面的fragment_layout_test.xml布局文件仅仅值包括1个Fragment,该Fragment是通过FragmentLayoutTest的内部类ExampleFragment实现的 3. 内部类ExampleFragment继承于Fragment,在onCreateView()方法中通过

代码语言:javascript
复制
return inflater.inflate(R.layout.example_fragment, container, false)

来将example_fragment.xml作为其布局文件。 所以,相当于FragmentLayoutTest直接调用example_fragment.xml来显示。

那为什么要费这么大劲,非要用到Fragment呢?原因是因为Fragment的可扩展性,它能作为一个独立的显示单元添加到activity中。本文仅仅只是为了说明fragment的layout实现方法,实际应用中,可能比这复杂很多。

  • example_fragment.xml
代码语言:javascript
复制
<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:orientation="vertical" >

    <TextView
        android:text="@string/example_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

方法2:在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment

该Fragment采用动态加载的方式。

  • FragmentTransactionTest
代码语言:javascript
复制
package com.skywang.app;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class FragmentTransactionTest extends Activity {

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

        // 获取FragmentManager
        FragmentManager fragmentManager = getFragmentManager();
        // 获取FragmentTransaction        
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // 获取ExampleFragment
        ExampleFragment fragment = new ExampleFragment();
        // 将fragment添加到容器about_fragment_container中
        fragmentTransaction.add(R.id.about_fragment_container, fragment);
        fragmentTransaction.commit();
    }

    // 继承与Fragment
    public static class ExampleFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            return inflater.inflate(R.layout.example_fragment, container, false);
        }
    }
}

说明: 1. 在onCreate()中该activit调用fragment_transaction_test作为其布局文件 2. 在onCreate()中,通过获取FragmentManager和FragmentTransaction,来将ExampleFragment对象添加到R.id.about_fragment_container中,about_fragment_container是一个Fragment。 3. ExampleFragment的onCreateView()中,将example_fragment作为其布局文件。

  • fragment_transaction_test.xml
代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/about_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
  • example_fragment.xml
代码语言:javascript
复制
<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:orientation="vertical" >


    <TextView
        android:text="Hello Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

总结

本文对Fragment进行了全面介绍和分析,有不懂的可以直接在下面留言给我哦! 接下来我会介绍继续介绍Android开发中的相关知识,有兴趣可以继续关注Carson_Ho的安卓开发笔记

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年06月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 目录
  • 定义
  • 作用
  • Fragment的生命周期解析
    • 先来看张官方说明图
      • 详细解读每个方法的调用场景
      • fragment生命周期解析
      • 其他场景的调用
      • Fragment和Activity的生命周期很相似,以下是对比图
      • 方法1:在activity的layout.xml文件中声明fragment
      • 总结
  • Fragment的使用
  • 方法2:在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档