1 Fragment简介

1.1 Fragment的设计初衷

  Android3.0引入Fragment的初衷是为大屏幕的设备提供更加灵活的动态UI设计,由于大屏设备可以容纳更多的UI组件,且这些UI组件之间存在交互关系。Fragment简化了大屏UI的设计,它不需要开发者guan'li管理组件包含关系的复杂变化,开发者使用Fragment对UI组件进行分组、模块化管理,可以更方便的动态更新Activity的用户界面。

1.2 Fragment特征

  Fragment必须被“嵌入”Activity中使用,因此虽然Fragment也拥有自己的生命周期,但是Fragment的生命周期会受到它所在的Activity的生命周期控制:当Activity暂停时,其中所有的Fragment也暂停;当Activity被销毁时,其中所有的Fragment都会被销毁;只有当该Activity处于活动状态时,程序员可以通过方法独立操作Fragment。Fragment

具有如下特征:

  • Fragment总是作为Activity界面的重要组成部分。Fragment可调用getActivity()方法获得它所在的Activity;Activity可以调用FragmentManager的findFragmentById()或者findFragmentByTag()的方法来获取Fragment
  • 在Activity运行过程中,可以调用FragmentManager的add()、remove()、replace()方法动态地添加、删除或替换Fragment
  • 一个Activity可以同时组合多个Fragment,反过来,一个Fragment也可以为多个Activity复用;一个Fragment也可以为同一个Activity复用
  • Fragment可以响应自己的输入事件,并拥有自己的生命周期,但是他的生命周期为Activity所控制

1.3 Fragment的类继承关系

2 创建Fragment

  开发Fragment与开发Activity非常相似,区别只是开发两者继承的类分别为Activity及其子类/Fragment及其子类。需要将原来写在Activity中的回调方法也需要在Fragment中实现。创建Fragment通常需要实现如下三个方法:创建时的回调方法onCreate()、绘制界面时的回调方法onCreateView()、用户离开Fragment时的回调方法onPause()

2.1 静态Fragment

  使用Fragment最简单的一种方式,把Fragment当成普通的控件,直接写在Activity的布局文件中。步骤:

  • 继承Fragment,重写onCreateView决定Fragemnt的布局
  • 在Activity中声明此Fragment,就当和普通的View一样

2.2 动态Fragment

  Activity中的Fragment可以动态的添加、删除、更新。

  1. <include
  2. android:id="@+id/id_ly_bottombar"
  3. android:layout_width="fill_parent"
  4. android:layout_height="55dp"
  5. android:layout_alignParentBottom="true"
  6. layout="@layout/bottombar" />

注意:layout布局文件中包含子布局文件,用<include>元素

3 Fragment与Activity之间数据传输

3.1 对象的获取

  将Fragment添加到Activity之后,Fragment必须与Activity交互信息,这就需要两者能在各自的空间中获取对方的对象。

  • Fragment获取它所在的Activity:调用Fragment的getActivity()方法 ,即可获得它所在的Activity
  • Activity获取它包含的Fragment:调用Activity关联的FragmentManager的findFragmentById(int id)或findFragmentByTag(String tag)方法即可获取指定的Fragment

3.2 数据的传递

  • Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将Bundle的数据包传给Fragment
  • Fragment向Activity传递数据,或者是Fragment在运行中要与Activity实时通信:在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口,这样Fragment可以调用该回调接口将数据传给Activity

示例:

Activity文件:

import android.app.Activity;
import android.os.Bundle; public class SelectBookActivity extends Activity implements
BookListFragment.Callbacks
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 加载/res/layout目录下的activity_book_twopane.xml布局文件
setContentView(R.layout.activity_book_twopane);
}
//实现Callbacks接口必须实现的方法,实现此方法从Fragment取数据
@Override
public void onItemSelected(Integer id) {
///Activity向BookDetailFragment发送数据 Bundle arguments = new Bundle();
arguments.putInt(BookDetailFragment.ITEM_ID, id);
BookDetailFragment fragment = new BookDetailFragment();
// 向Fragment传入参数
fragment.setArguments(arguments);
// 使用fragment替换book_detail_container容器当前显示的Fragment
getFragmentManager().beginTransaction()
.replace(R.id.book_detail_container, fragment)
.commit();
}
}

向Activity发送数据的BookListFragment文件

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class BookListFragment extends ListFragment {
private Callbacks mCallbacks; // 定义一个回调接口,该Fragment所在Activity需要实现该接口
// 该Fragment将通过该接口与它所在的Activity交互
public interface Callbacks {
void onItemSelected(Integer id);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 为该ListFragment设置Adapter
setListAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, BookContent.itemList));
} // 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach() {
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
} // 当用户点击某列表项时激发该回调方法
@Override
public void onListItemClick(ListView listView
, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
// 获取Activity对象,以调用Callbacks回调方法
mCallbacks = (Callbacks) getActivity();
mCallbacks.onItemSelected((int)id);
}
}

从Activity接收数据的BookDetailFragment文件

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class BookDetailFragment extends Fragment {
public static final String ITEM_ID = "item_id";
BookContent.Book book;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_book_detail, container, false);
//Fragment接收Activity传输过来的数据
Bundle bd = getArguments();
book = BookContent.itemList.get(bd.getInt(ITEM_ID));
((TextView) rootView.findViewById(R.id.book_title)).setText(book.title);
((TextView) rootView.findViewById(R.id.book_desc)).setText(book.desc);
return rootView;
}
}

Activity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<!-- 添加一个Fragment -->
<fragment
android:name="com.example.penghuster.fragmenttest.BookListFragment"
android:id="@+id/book_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />`
<!-- 添加一个FrameLayout容器 -->
<FrameLayout
android:id="@+id/book_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>

DetailFragment布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个TextView来显示图书标题 -->
<TextView
android:id="@+id/book_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="哈地方"
android:padding="16dp" />
<!-- 定义一个TextView来显示图书描述 -->
<TextView
android:id="@+id/book_desc"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" />
</LinearLayout>

4 Fragment管理与Fragment事物

4.1 FragmentManager

  • 使用Activity关联的FragmentManager的findFragmentById(int id)或findFragmentByTag(String tag)方法即可获取指定的Fragment
  • 调用popBackStack()方法将Fragment从后台中弹出(模拟用户按下Back按钮)
  • 调用addOnBackStackChangeListener()注册一个监听器,用于监听后台的变化

4.2FragmentTransaction

  如果需要添加,删除,替换Fragment,则需要通过FragmentTransaction对象

5 Fragment的生命周期

Fragment详解的更多相关文章

  1. Fragment详解之三——管理Fragment(1)

    相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...

  2. android——fragment详解

    在android开发过程中,如果使用到了导航栏.那么不可避免的就需要使用fragment来处理界面.闲着没事,就详解一下Framgent的使用方法吧. 难得写一次.本人 shoneworn shone ...

  3. Android面试收集录4 Fragment详解

    1.什么是Fragment? 你可以简单的理解为,Fragment是显示在Activity中的Activity. 它可以显示在Activity中,然后它也可以显示出一些内容. 因为它拥有自己的生命周期 ...

  4. Android 开发 之 Fragment 详解

    本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...

  5. 5. Fragment详解

    onCreateView是Fragment生命周期方法中最重要的一个.因为在该 方法中会创建在Fragment中显示的View. public View onCreateView(LayoutInfl ...

  6. Android Fragment详解

    一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...

  7. Android Fragment 详解(一)

    Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...

  8. Android Fragment详解(二):Fragment创建及其生命周期

    Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...

  9. Android Fragment详解(一):概述

    Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你 ...

随机推荐

  1. iOS 10 使用相机及相簿闪退的问题修正

    http://www.cnblogs.com/onechen/p/5935579.html

  2. imports,using,和include之间的区别

    Imports, Using基本一样,有两个作用 1.将后面命名空间中所有的名字导入到当前命名空间 2.为后面的名字取一个当前命名空间可以访问的别名. 比如StreamWriter这个类在System ...

  3. android SharedPreferences 使用

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值 对数据,通常用来存储一些简单的配置信息.其存储位置在/dat ...

  4. Redis文档

    http://manual.csser.com/redis/connection/auth.html

  5. bzoj1855

    让我们继续练习dp 首先这道题约束条件很多 但实际上方程还是很好写的,f[i,j]表示第i天时拥有j只股票的最大收益 令p=max(0,i-k-1) 上一次较交易 易得f[i,j]=max(f[i-1 ...

  6. 图片处理 Pillow

    Pillow 在python3下用PIL做图像处理 Python图像处理库:Pillow 初级教程 from PIL import Image im = Image.open('22.gif') pr ...

  7. Js动态传递不定数目的参数

    回调程序中,经常有这样的需求:用户传递一个回调方法,该方法可以有不定的参数. 如果参数数目固定则很容易实现,看代码: //回调函数1 function callback1(a,b,c) { alert ...

  8. Host绑定

    Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...

  9. 多线程程序设计学习(11)Two-phapse-Termination pattern

    Two-phapse-Termination[A终止B线程] 一:Two-phapse-Termination的参与者--->A线程--->B线程 二:Two-phapse-Termina ...

  10. POI 操作(新接口)

    POI 生成XLS实例 转载至:http://www.4ucode.com/Study/Topic/697242 ackage test; import java.io.FileOutputStrea ...