zoukankan      html  css  js  c++  java
  • Android: Fragment编程指南

    本文来自于www.lanttor.org


    Fragment代表了Activity里的一个行为,或者Activity UI的一部分。你可以在一个activity里构造多个Fragment,也可以在多个activities里复用一个Fragment。你可以认为Fragment是activity里的一个模块片段。Fragment有自己的lifecycle,接收自己的input事件。你可以在activity运行的时候添加或者删除一个Fragment。

    一个fragment必须嵌在一个activity里,它的lifecycle直接受到主activity的lifecycle的影响。例如,当一个activity 处于paused状态时,所有的fragment也处于paused状态;当一个activity处于destroyed状态时,所有的fragment也是destroyed状态。然而,当一个activity处于运行状态时(it is in the resumed lifecycle state),你可以独立的操作每个fragment,例如添加或删除它。当你执行一个fragment事务处理时,你可以将该操作加入到activity管理的back stack里---每一个back stack项是记录发生的一个fragment事务。back stack允许你通过回退按钮来回退一个fragment事务处理。

    当你在你的activity layout里添加一个fragment时,它驻扎于activity 的一个viewgroup里。

    Fragment定义自己的视图布局。你可以在你的activity layout里使用<fragment>元素插入一个fragment,也可以在代码里添加一个fragment到已存在的ViewGroup里。然而,fragment的视图布局不是必须的,你也可以使用没有UI的fragement,作为你的activity的一个不可见的worker。

    1. 设计指南

    Android的fragment机制,是在Android 3.0 (API level 11)上引进的,主要是支持大尺寸屏幕上的动态和灵活的UI设计,例如平板电脑。由于平板的尺寸远远大于手机设备,它有足够的空间来进行UI界面的组合和交互。Fragment机制可以让你避免复杂的视图结构的管理,来实现UI的动态交互。将activity里的布局,分解成多个fragment,你能够在activity运行时动态改变activity的外观,在back stack里维持这些改变。

    例如,一个应用可以使用一个fragment在左侧显示文章的标题,使用另一个fragment在右侧显示文章的内容---两个fragment都在同一个activity里,每个fragment有自己独立的一套lifecycle,可以处理自己的用户输入事件。这样,可以规避用一个activity来实现选择文章,另一个activity来实现阅读文章的旧方法;利用fragment,可以在一个activity里实现选择文章和阅读文章。如下图所示:

    Image1

    你应当设计每一个fragment,作为activity里可复用的组件模块。因为每个fragment定义自己的layout,自己的行为,自己的lifecycle callback,你可以在多个activity里使用一个fragment。一个模块化的fragment,允许在不同尺寸的设备上改变fragment的排列。当你打算涉及你的应用既支持平板又支持手机时,你可以在不同的布局配置上复用你的fragment,根据相应的显示空间优化用户的体验。例如,在手机上,fragment可能只能同时显示一屏UI界面。

    2. 创建一个Fragment

    创建一个fragment,你必须实现一个类继承fragment类。Fragment类包含了与activity类相似的回调函数,例如onCreate(),onStart(),onPause(),onStop()。实际上,你可以简单的将activity里实现的回调函数拷贝到你的fragment类里,进行修改。

    通常,你至少需要考虑实现下面的回调函数:

    onCreate()

    当创建fragment时系统会调用此函数。你应当在这里初始化fragment必须的组件。

    onCreateView()

    当fragment绘画UI界面时系统调用此函数。这个方法必须返回一个View,作为fragment的根布局。

    onPause()

    当用户离开此fragment界面时,系统调用此函数。你应当在这里保存你需要维持的数据。

    基于fragment类,系统已实现了下面的一些扩展子类:

    • DialogFragment
    • ListFragment
    • PreferenceFragment

    Image2

    3. 添加一个fragment UI界面

    要为一个fragment提供布局,必须实现onCreateView()回调函数。该函数返回的View就是你的fragment的根布局。

    说明:如果你的fragment是由ListFragment扩展而来,默认的onCreateView()实现会返回一个ListView,所以你的代码里不必实现它。

    示例如下:

    public static class ExampleFragment extends Fragment {
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              return inflater.inflate(R.layout.example_fragment, container, false);
          }
      }

    4. 在activity里添加一个fragment

    有两种方法实现:

    • 在activity的layout文件里声明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">
          <fragment android:name="com.example.news.ArticleListFragment"
                  android:id="@+id/list"
                  android:layout_weight="1"
                  android:layout_width="0dp"
                  android:layout_height="match_parent" />
          <fragment android:name="com.example.news.ArticleReaderFragment"
                  android:id="@+id/viewer"
                  android:layout_weight="2"
                  android:layout_width="0dp"
                  android:layout_height="match_parent" />
      </LinearLayout>

    <fragment>元素里的android:name属性定义了实例化此布局的Fragment类。当系统创建这个layout,它会初始化layout定义的每个fragment,调用onCreatView()来获取每个fragment的view。

    • 在代码里,添加一个fragment到一个已经存在的ViewGroup里。

    在activity运行时,你随时可以在你的activity布局里添加fragment。你仅仅需要为放置的fragment指定一个ViewGroup。为了在你的activity里进行fragment事务处理,你必须使用FragmentTransaction的API函数。用如下代码获得FragmentTransaction:

    FragmentManager fragmentManager = getFragmentManager()
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    然后调用add()来添加一个fragment:

    ExampleFragment fragment = new ExampleFragment();
      fragmentTransaction.add(R.id.fragment_container, fragment);
      fragmentTransaction.commit();

    一旦调用FragmentTransaction的事务处理函数后,必须在最后调用commit()函数来使其生效。

    5. Fragment与Activity通信

    fragment可以通过getActivity()里访问Activity的实例。例如查找Activity里的一个view:

    View listView = getActivity().findViewById(R.id.list);

    同样的,activity可以通过FragmentManager的findFragmentbyId()或者findFragmentbyTag()来获取到一个需要访问的fragment:

    ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

    6. 创建事件回调函数

    在一些case里,你也许需要fragment和activity之间共享事件。一个很好的做法是,在fragment里定义回调函数接口,在主activity里去实现它。

    例如,当一个应用需要在activity里实现两个fragment。fragment A用于显示文章标题,fragment B用于显示文章内容。当用户选中一个标题时,fragment A必须告之Activity,让其通知Fragment B来显示文章内容。这个case里,fragment A定义了如下的接口:

    public static class FragmentA extends ListFragment {
          ...
          // Container Activity must implement this interface
          public interface OnArticleSelectedListener {
              public void onArticleSelected(Uri articleUri);
          }
          ...
      }

    主activity会实现这个接口,覆写onArticleSelected()来通知Fragment B,来自于Fragment A的消息。Fragment A会在onAttach()里获取到这个接口的实现:

    public static class FragmentA extends ListFragment {
          OnArticleSelectedListener mListener;
          ...
          @Override
          public void onAttach(Activity activity) {
              super.onAttach(activity);
              try {
                  mListener = (OnArticleSelectedListener) activity;
              } catch (ClassCastException e) {
                  throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
              }
          }
          ...
      }

    当用户点击一个litst item,系统会调用fragment里的onListItemClick(),在这个函数里,会调用onArticleSelected()来通知activity:

    public static class FragmentA extends ListFragment {
          OnArticleSelectedListener mListener;
          ...
          @Override
          public void onListItemClick(ListView l, View v, int position, long id) {
              // Append the clicked item's row ID with the content provider Uri
              Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
              // Send the event and Uri to the host activity
              mListener.onArticleSelected(noteUri);
          }
          ...
      }

    7. 处理fragment lifecycle

    与activity类似,fragment存在如下三种状态:

    • Resumed

    fragment在运行的activity里是可见的。

    • Paused

    其他的activity在切面,部分遮住了当前的主activity,或者是背景半透明的全部遮住了当前的activity。导致当前的主activty里的fragment处于Paused状态。

    • Stopped

    fragment不可见。要么是主activity已经停止,要么是fragment被移除,但是被添加到了back stack里。

    Activity停止时,默认情况下会被添加到back stack;Fragment停止时,必须显示的调用addToBackStack(),才能够被添加到back stack。

    主activity的life cycle会直接影响到fragment的lifecycle。如下图所示:

    Image3

     本文档完整的pdf版本,请参考“Android Fragment指南.pdf

    (如需转发,请标明出处)

    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    linux基础知识-17
    linux基础知识-16
    linux基础知识-15
    linux基础知识-14
    linux基础知识-13
    phone 调试三种工具
    ANT_HOME is set incorrectly or ant could not be located .Please set ANT_HOME.
    如何解决google ping不通的问题。
    weinre targets none 的问题
    phonegap3.5了结
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834763.html
Copyright © 2011-2022 走看看