Fragment 对比Activity - Android 碎片介绍

作者: Android 开发网原创 时间: 2011-02-01

Fragment Android honeycomb 3.0 新增的概念,Fragment 名为碎片不过却和Activity 十分相似,下面Android123 介绍下Android Fragment 的作用和用法。Fragment 用来描述一些行为或一部分用户界面在一个Activity 中,你可以合并多个fragment 在一个单独 的activity 中建立多个UI 面板,同时重用fragment 在多个activity. 你可以认为fragment 作为一个activity 中的一 节模块 ,fragment 有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.

一个fragment 必须总是嵌入在一个activity 中,同时fragment 的生命周期受activity 而影响,举个例子吧,当activity 暂停,那么所有在这个activityfragments 将被destroy 释放。然而当一个activity 在运行比如resume 时,你可以单独的 操控每个fragment ,比如添加或删除。

我们可以看到两个Activity 通过两个Fragment 合并到一个Activity 的布局方式,对于平板等大屏幕设备来说有着不错的展示面板。不过因为FragmentActivity 的生命周期都比较复杂,我们分别对比下:

创建一个fragment 你必须创建一个Fragment 的子类或存在的子类,比如类似下面的代码

public static class Android123Fragment extends Fragment {
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate (R.layout.android123_fragment, container, false);
}
}

Fragment 类的一些代码看起来有些像Activity 为了让大家了解清楚,Android 开发网给大家整理下Fragment 的生命周期如上图所示,部分类似Activity 的,我们详细解释

onCreate()
fragment 创建时被调用,你应该初始化一些实用的组件,比如在fragment 暂停或停止时需要恢复的

onCreateView()
当系统调用fragment 在首次绘制用户界面时,如果画一个UI 在你的fragment 你必须返回一个View 当然了你可以返回null 代表这个fragment 没有UI.

那么如何添加一个FragmentActivity 中呢? Activity 的布局可以这样写

<?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.android123.cwj.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.android123.cwj.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

一、 管理Fragment

管理Fragment 在你的Activity 你需要使用一个名为FragmentManager 的类,通过调用getFragmentManager() 方法来实例化该管理类在你的Activity 种。FragmentManager 类一些主要的方法有通过findFragmentById() 来获取一个Activity 中有关Fragment 布局。当然还有类似 findFragmentByTag() 方法,以及唐Fragment 中出栈的popBackStack() 同时可以注册 addOnBackStackChangedListener() 管理. 具体的可以在android.app.FragmentManager 类中了解

二、 优化Fragment 事物处理

一个很好的特性在添加,删除,替换fragmentActivity 时可以使用FragmentTransaction 类来提高批量处理的效率,这点和SQLite 的数据库更新原理类似。

FragmentManager fragmentManager = getFragmentManager(); // 实例化fragmentmanager
FragmentTransaction transaction = fragmentManager.beginTransaction (); //
通过begintransaction 方法获取一个事物处理实例。

在这期间可以使用 add(), remove(), 以及 replace(). 最终需要改变时执行 commit() 即可,接下来我们写代码

transaction.replace (R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();

三、FragmentActivity 互相通讯

通常Fragment 中我们放入平时标准的控件或自定义的控件,基本上和Activity 一样,但是如何Fragment 中的View 布局也是放到Activity 中的,这里Android 开发网提示大家有两种方法来实现

View listView = getActivity().findViewById(R.id.cwj); // 通过getActivity 方法可以获取一个Activity 中的fragment ,这里的cwj 是一个fragment ,在activity 中的布 局如下:

<?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.android123.cwj.ArticleListFragment"
android:id="@+id/cwj "
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.android123.cwj.ArticleReaderFragment"
android:id="@+id/smart"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

当然还有一种通过getFragmentManager 方法获取实例,ExampleFragment fragment = (ExampleFragment) getFragmentManager ().findFragmentById (R.id.cwj);

更多相关文章

  1. Android Studio 布局属性笔记
  2. 相对布局相关属性
  3. 关于android LinearLayout的比例布局
  4. Android 之 五大布局案例
  5. android xml布局文件属性说明
  6. ANDROID 绝对布局 相对布局 Linear…
  7. Android 界面布局之RelativeLayout
  8. Android Studio App设置线性布局LinerLayout控件占屏幕长宽比例

随机推荐

  1. Android中的Intent Filter匹配规则介绍
  2. 定义Window进入和退出效果(及Window,Activ
  3. android activity启动过程分析
  4. Android(安卓)五大存储方式详解
  5. android studio配置Terminal命令窗口
  6. PhotoView的使用
  7. 【Android(安卓)Developers Training】 9
  8. Android中的UID、GID与应用安全
  9. Android闪闪发光字体效果
  10. Android(安卓)Activity及其生命周期