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 资料地址
  2. Android下单元测试
  3. Android之Activity的四种启动模式
  4. android studio 在线更新android sdk,遇到无法Fetching https://d
  5. 【Android】TabHost与RadioGroup结合完成的菜单
  6. Android(安卓)面试题总结之Android(安卓)基础(四)
  7. flutter 与 android 混合开发 以及 Android与flutter之间的通信
  8. Android(安卓)存储选项之 SQLiteDatabase 源码分析
  9. android总结

随机推荐

  1. Android教程之一:Window下搭建Android开发
  2. Android魔术——手把手教你实现水晶球波
  3. Android(安卓)启动流程
  4. Android的BUG - 广为人知的诟病:频繁重启
  5. android游戏开发(三)触屏事件处理_手势识
  6. 三大布局的基本摆放属性总结,以及imageVIe
  7. 为什么 Android(安卓)截屏需要 root 权限
  8. 游戏开发中SurfaceView的重要作用
  9. Android内核和驱动篇-Android内核介绍
  10. Android(安卓)Socket 发送广播包的那些坑