参考《Professional Android 4 Development》

Fragment简介

Fragment是我们可以将Activity分成不同的组成部分,这些组成部分拥有自己的生命周期和UI。它的最大用途在于适配不同的屏幕。

创建Fragment

Fragment和Activity有很多相似之处,例如可以不带UI,但这样做对两者似乎都没什么意义。他们的创建方式也很相似,例如下面的代码:

package test.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MySkeletonFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
  ViewGroup container,
  Bundle savedInstanceState) {
  // Create, or inflate the Fragment’s UI, and return it.
  // If this Fragment has no UI then return null.
  return inflater.inflate(R.layout.my_fragment, container, false);
  }
}

Fragment生命周期

Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最大的不同在于Activity可以增加或删除Fragment。下图总结了Fragment的生命周期:


Fragment特有的生命周期事件

  1. Attach and detach Fragment from the parent Activity
  2. Creating and destroying Fragment
  3. Creating and Destroying UI

获取Fragment Manager

每个Activity对象都内置了一个FragmentManager对象,使用getFragmentManager()即可获得:

FragmentManager fragmentManager = getFragmentManager();

添加Fragment到Activity中

在Activity中添加Fragment的最简单方法是使用layout配置文件,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <fragment android:name="com.paad.weatherstation.MyListFragment"
  android:id="@+id/my_list_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  />
  <fragment android:name="com.paad.weatherstation.DetailsFragment"
  android:id="@+id/details_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="3"
  />
</LinearLayout>

调用inflate方法生成Fragment的界面后,Fragment实际上是一个类似ViewGroup的角色,在Activity中管理自己的UI。

上面那种将Fragment添加到Activity的方法缺乏灵活性,不能实现动态地添加和删除,更好的方式是使用FragmentTranaction和类似下面这样的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <FrameLayout
  android:id="@+id/ui_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  />
  <FrameLayout
  android:id="@+id/details_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="3"
  />
</LinearLayout>

使用FragmentTransaction

FragmentTransaction可以在运行时添加,删除或替换Fragment,从而实现UI的动态变化。Fragment Transaction由Fragment Manager的beginTransaction()方法创建,然后可以进行Fragment的添加,删除和替换,最后通过commit()方法提交修改。

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Add, remove, and/or replace Fragments.
// Specify animations.
// Add to back stack if required.
fragmentTransaction.commit();

添加,删除和替换Fragment

使用FragmentTransaction的add方法可以添加一个新的Fragment,add()方法的主要参数是Fragment的容器View(或其ID)及Fragment实例,例如:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ui_container, new MyListFragment());
fragmentTransaction.commit();

删除Fragment需要FragmentTransaction的remove()方法,参数为Fragment对象,Fragment对象可以通过FragmentManager的findFragmentById()方法获得。

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();

替换Fragment使用的是FragmentTransaction的replace()方法,参数分别为所要替代Fragment所在容器的ID和新的Fragment:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.details_fragment, new DetailFragment(selected_index));
fragmentTransaction.commit();

获取指定的Fragment

有两种方法可以获取某个特定的Fragment,如果这个Fragment已经被添加到某个layout文件中,则可以使用xml文件中的id作为参数:

MyFragment myFragment = (MyFragment)fragmentManager.findFragmentById(R.id.MyFragment);

也可以通过创建Fragment时添加的tag获取特定的Fragment:

MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag(MY_FRAGMENT_TAG);

删除Fragment容器

在配置文件中将visibility的属性设为"gone",即可删除某个Fragment,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <FrameLayout
  android:id="@+id/ui_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  />
  <FrameLayout
  android:id="@+id/details_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="3"
  android:visibility="gone"
  />
</LinearLayout>

Fragment和Back Stack

Activity拥有Activity Stack,从而在用户按”返回”按钮时,回到前一个Activity。Fragment也可以响应”返回”事件,方法是FragmentTransaction在commit之前调用addToBackStack()方法。这样,在用户按返回键后,Android会首先重现之前的UI布局。

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ui_container, new MyListFragment());
Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);
String tag = null;
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();

原理和Activity类似,调用addToBackStack()后,Fragment会被push到back stack中,而不是销毁。

Fragment Transaction的动画效果

Fragment Transaction有两种方法实现动画效果,分别是:

  • 设置渐进:

transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

  • 设置动画效果:
fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);

Fragment和宿主Activity之间的接口

Fragment可以通过getActivity()方法获得宿主Activity对象:

TextView textView = (TextView)getActivity().findViewById(R.id.textview);

另一种常见的Fragment和Activity之间的交互方式是使用回调函数:

public interface OnSeasonSelectedListener {
  public void onSeasonSelected(Season season);
}
private OnSeasonSelectedListener onSeasonSelectedListener;
  private Season currentSeason;
  @Override
  public void onAttach(Activity activity) {
  super.onAttach(activity);
  try {
  onSeasonSelectedListener = (OnSeasonSelectedListener)activity;
  } catch (ClassCastException e) {
  throw new ClassCastException(activity.toString() +"must implement OnSeasonSelectedListener");
  }
  }
  private void setSeason(Season season) {
  currentSeason = season;
  onSeasonSelectedListener.onSeasonSelected(season);
}

没有UI的Fragment

尽管不常见,但Fragment的确是可以没有UI的,好处也许是拥有了更灵活的生命周期控制。没有UI的Fragment生命周期事件有这些:

public class NewItemFragment extends Fragment {
  @Override
  public void onAttach(Activity activity) {
   super.onAttach(activity);
  // Get a type-safe reference to the parent Activity.
  }
  @Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // Create background worker threads and tasks.
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  // Initiate worker threads and tasks.
  }
}

常用的Fragment类

  • DiagFragment
  • ListFragment
  • webViewFragment


Android 4学习(8):用户界面 - Fragment的更多相关文章

  1. Android编码学习之Fragment

    1. 什么是Fragment Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似.Fragment是用来描述一些行为或一部 ...

  2. Android学习之探究Fragment

    •碎片是什么 Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间: 出现的初衷是为了适应大屏幕的平板电脑,可以将其看成一个小型Activity,又称作Acti ...

  3. 我的Android 4 学习系列之创建用户基本界面

    目录 使用视图和布局 理解Fragment 优化布局 创建分辨率无关的用户界面 扩展.分组.创建和使用视图 使用适配器将数据绑定到视图 使用视图和布局 1. Android UI 几个基本概念 视图: ...

  4. Android开发学习路线图

    Android开发学习方法: Android是一个比较庞大的体系,从底层的Linux内核到上层的应用层,各部分的内容跨度也比较大.因此,一个好的学习方法对我们学习Android开发很重要. 在此建议, ...

  5. Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例

    ---------------------------------------------------------------------------------------------------- ...

  6. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  7. Android WiFiDirect 学习(二)——Service Discovery

    Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...

  8. Android Sip学习(三)Android Voip实现

    Android Sip学习(三)Android Voip实现   Android Sip学习(准备知识)SIP 协议完整的呼叫流程 Android Sip学习(一)Android 2.3 APIs S ...

  9. Android WebView学习

    Android WebView学习 文章来源:小小懒羊羊个人知识管理库 权限: <uses-permission android:name="android.permission.IN ...

随机推荐

  1. Job for docker.service failed because the control process exited with error code. See "systemctl status do cker.service" and "journalctl -xe" for details.

    问题出现 :入手操作Docker时,安装启动后报了这个错 Job for docker.service failed because the control process exited with e ...

  2. php中生成树形菜单

    废话不多说!上代码 class tree { /** * 生成树型结构所需要的2维数组 * @var array */ var $arr = array(); /** * 生成树型结构所需修饰符号,可 ...

  3. 1 秒杀系统模拟基础实现,使用DB实现

    本文根据动脑学院的一节类似的课程,改编实现.分别使用DB和redis来完成. 隔离的解释 业务隔离:将秒杀业务独立出来,尽量不与其他业务关联,以减少对其他业务的依赖性.譬如秒杀业务只保留用户id,商品 ...

  4. Failed to export application

    打包Android项目,遇到Failed to export application的错误提示.如何处理呢 我当时是 在替换图标时   没有完全替换 只替换了 四张drawable_h图片,没有替换上 ...

  5. LEX下出毛病的问题

    毛病! 1.今日写词法分析,回想起第一次写时候的蓝色警告:不要随便管理员.so,便Win+R,"cmd",回车. 2.在用lex写的时候,注意注释是 /*注释放于此处*/ 而非一般 ...

  6. BZOJ - 3622:已经没有什么好害怕的了 (广义容斥)

    [BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...

  7. HihoCoder1619 “共同富裕”(逆向思维)

    “共同富裕” 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数组A1, A2, ... AN,每次操作可以从中选定一个元素Ai,把除了Ai之外的所有元素都加1. ...

  8. 安卓开发第一记 android stdio 安装后 新建测试项目报错

    Failed to resolve:com.android.support:appcompat-v7:报错处理   你在使用android studio时是否也出现过上图的报错,你还在为它的出现烦恼? ...

  9. HTML服务器控件与Web服务器控件

    asp.net之所以现在开发方便和快捷,关键是它有一组强大的控件库,包括web服务器控件,web用户控件,web自定义控件,html服务器控件和html控件等.这里主要整理一下html控件.html服 ...

  10. 【WCF安全】使用X509证书自定义验证

    接触WCF时间比较短,在项目中要使用X509证书,纠结好几天终于有了结论,因此为了方便日后查阅和园友交流特意单独将部分代码提出,并做以记录. 1.准备工作 制作X509证书,此处用到三个证书名称 导入 ...