Fragments

一个Fragment可以表示activity的一个动作或者一些UI集合。你可以在一个activity中集成多个fragment,也可以在多个activities使用同一个fragment。可以将fragment当成是activity的一个模块,这个模块有自己的生命周期,接收属于它的输入事件,可以在activity运行的时候添加或移除一个fragment(类似于sub activity)。

Fragment只能内嵌在activity中,并且fragment的生命周期直接受宿主activity生命周期的影响。例如,当activity被停止时,所有activity里面的fragment都停止,当activity被销毁时,所有里面包含的fragment都被销毁。当activity在运行时(处在resumed状态),你可以独立的操作每个fragment,比如添加或者删除一个fragment,当你执行上述的操作时,上述的操作会被添加到activity维持的一个back stack中,在back stack的每一项表示一个发生的fragment操作。back stack使得用户可以撤销已执行的动作,即返回上一个状态,比如用户按下返回按钮。

当在你的activity的layout中添加一个fragment时,这个fragment就存在于activity的ViewGroup中,这个fragment定义了自己的view layout。可以通过在activity layout中添加<fragment>元素来添加fragment,或者通过代码将fragment添加到ViewGroup中。当然一个fragment可以不是activity layout的一部分,比如你可能用一个没有UI的fragment作为一个activity不可见的worker。

这个文档描述了如何在你的app中创建fragment,包括当fragment被添加到activity的back stack时,fragment如何维持自己的状态,与activity或者其它activity里的fragment共享事件。

Design Philosophy (设计理念)

android在android 3.0后引进了fragment,以便支持在大屏幕中更动态合适的UI设计,比如平板。因为平板的屏幕比手机大得多,有更多的空间放置UI,让用户与UI交互。Fragment使你不用去管理view层次复制的变化。通过将layout分成多个fragment,你可以在activity运行时更改activity的样式,并通过activity管理的back stack来保存相应的变化。

比如,在一个activity中,你可以用一个fragment在屏幕的左边显示文章列表,另外一个fragment在屏幕右边显示一篇文章,每个fragment有自己的生命周期回调函数,并处理自己的用户输入。而不是通过一个activity来选择文章,另外一个activity来显示文章,用户可以在一个activity里面选择文章,并显示这篇文章,如下图:


最好将每个fragment都设计成一个模块,一个可以重复使用的activity组件,这是因为每个fragment都定义了自己的layout和在生命周期回调函数执行的操作,你可以在多个activity中使用一个fragment,所以最好将它设计成可重复使用,而不是在不同的activity都需要修改这个fragment。这使得你可以只改变fragment的尺寸就可以将你的app应用于不同屏幕大小的设备,比如平板和手机,提高了用户体验。比如在手机中,当在一个activity只能显示一个fragment时,就只能让fragment分开显示。

例如,还是上面的例子,在平板中可以同时显示着两个fragment,但是在手机中,屏幕大小不够显示这两个fragment,只能先启动一个activity显示fragment A,当用户选择文章后,再启动一个activity B显示fragment B来显示对应的文章内容。通过以不同的组合重用fragment,使得这个app可以支持平板和手机。

更多关于如何通过不同的fragment组合来适应不同的屏幕大小的app设计请参照Supporting Tablets and Handsets。'

Creating a Fragment (创建一个Fragment)

通过创建fragment的子类来创建自己的fragment。fragment的代码跟activity有点类似,有自己的回调函数,如onCreate(), onStart(), onPause()和onStop()。实际上,当你将一个activity转换成一个fragment时,仅仅只是将对应的回调函数的代码复制到fragment对应的回调函数中。

通常你必须实现fragment中的下列回调函数:

onCreate()

当系统创建fragment时调用这个函数。在这个函数中,你必须初始化一些重要的组件的参数。

onCreateView()

当fragment第一次需要展示自己的UI时,系统会调用这个函数。你必须在这个函数中返回一个包含这个fragment的layout对应的View对象。如果这个fragment没有对应的UI,返回null。

onPause()

当用户离开当前的fragment(并不表示当前fragment被摧毁),系统会调用这个函数。在这个函数中,你必须保存在当前用户session做出的任何修改(因为用户可能不会再回到这个fragment).

大多数的app至少需要实现这三个函数,还有一些其他的回调函数,详细请参照Handing the Fragment Lifecycle。


除了fragment这个基类,还有其它fragment的子类你可以继承:

DialogFragment

显示一个 浮动对话框,使用这种方法比使用activity中的dialog helper函数更好,因为可以将fragment dialog保存到activity维持的fragment的back stack中,这样以便用户能够返回到一个已经消失的fragment中。

ListFragment:

通过adapter(比如SimpleCursorAdapter)显示一系列项目,跟ListActivity很类似。这个类提供了一些操作列表的函数,比如onListItemClick()来响应某一项被单击。

PreferenceFragment:

显示一个优先项列表,类似于PreferenceActivity。适合用于创建”设置“列表的activity。

Adding a user interface (添加UI)

通常fragment都是作为activity UI的一部分。

可以通过实现onCreateView()来为fragment创建一个layout,android系统会调用这个函数来绘制fragment的UI。在这个函数里返回一个包含所有UI的view对象。

注意:如果你的fragment继承于ListFragment,在这个类里面默认会返回一个ListView,所以你不需要返回ListView。

可以通过inflate(加载)定义在XML的layout文件获得相应的view并返回。

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);
    }
}
在onCreateView()函数中传入的参数container是fragment layout将会被添加的父ViewGroup。savedInstanceState参数是提供之前fragment相关的参数(这个fragment是被复原的,savadInstanceState保存之前的状态值)。

inflate()传入三个参数:

1. 需要加载的layout的ID。

2. fragment layout处在哪个ViewGroup下,传入参数container对于系统将定义在父View中的参数应用到加载的layout是很重要的。

3. 一个布尔值,标示加载的layout是否在加载中会被添加到父ViewGroup(第二个参数)中。(在上述情况下为false,引文系统已经将这个layout添加到container的viewgroup中,若为true将会添加两个重复的fragment layout)。

现在你已经知道如何为fragment创建一个layout,接下来需要将这个fragment添加到你的activity中。

Adding a fragment to an activity (将fragment添加到activity中)

fragment为主activity提供了一部分UI,有两种将fragment添加到activity layout的方式。

Declare the fragment inside the activity's layout file (在activity的layout文件中声明fragment)

在这种情况下,你可以在<fragment>里面声明一些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的类名。

当系统创建这个activity的layout时,它会实例化每个定义在layout中的fragment,并对每个fragment调用对应的onCreateView()函数来获取每个fragment的layout,系统将onCreateView()返回的view插入到<fragment>中。

注意:每个fragment都需要一个唯一的标示符以便当activity被重新启动时系统可以用这个标示符来恢复fragment(或者你可以通过这个标示符来获取对应的fragment,执行其他功能,比如删除这个fragment等)。有三种为fragment设置ID的方式:

赋值个android:id一个唯一的ID

赋值给android:tag一个唯一的字符串

如果你都没有设置前面两个参数,系统会使用父layout的id作为fragment的ID。

Programmatically add the fragment to an existing Viewgroup (通过代码将fragment添加到已存在的Viewgroup中)

只要你的activity在运行,你都可以随时在你的activity layout中添加fragment。你只需要指定一个放置fragment的ViewGroup。

为了操作你的fragment(比如添加,删除,替换),你必须使用FragmentTransaction的API。可以通过下面代码获取FragmentTransaction的一个实例:

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
然后就可以通过函数add()来添加fragment,在这个函数中传入你想添加的fragment(第二个参数)和这个fragment所在的父ViewGroup(第一个参数)。


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

一旦你修改了FragmentTransaction,你必须调用commit()是这个修改开始起作用。

Adding a fragment without a UI (添加一个没有UI的fragment)

上面的例子显示如何将fragment添加到activity中以提供相应的UI。另一方面,你也可以添加一个没有对应UI的fragment,用这个fragment来执行后台任务。

通过在activity中的函数add(Fragment, String) (为fragment提供一个唯一的”tag")。由于这个fragment没有和其它view想关联,因此不需要调用onCreateView()函数,所以不用实现这个函数。

为一个fragment设置一个唯一的字符串tap对于有UI和没有UI的fragment都可以,但是对于没有UI的fragment,设置字符串tag是唯一标识它的方式。通过findFragmentByTag()来获取想要的fragment。

关于activity使用fragment执行后台工作可以参照FragmentRetainInstance.java。

Managing Fragments (管理Fragment)

可以通过FragmentManager来管理activity中的fragment,通过getFragmenManager()函数来获得FragmentManager对象。通过FragmentManager可以:

1. 通过findFragmentById()(对于包好UI的fragment)或者findFragmentByTag()(对于没有UI的fragment)来获取activity的fragment。

2. 通过popBackStac()将fragments从back stack 抛出,可以用来模拟用户按下返回键。

3. 通过addOnBackStackChangedListener()注册一个响应back stack 状态变化的函数。

关于FragmentManager的函数,请参照FragmentManager。

上面提到可以通过FragmentManager来打开一个FragmentTransaction,通过FragmentTransaction对fragment进行操作(如添加删除fragment).

Performing Fragment Transactions (操作fragment)

在activity里可以响应用户的动作来添加,删除,替换fragment。对fragment的每个操作称为transaction。你可以将每个transaction保存到activity管理的back stack中,这样允许用户能够返回原先的状态。

可以从FragmentManager获得一个FragmentTransaction实例:

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

可以通过函数add(),remove()和replace()来执行一系列的transaction。然后调用commit()函数让这些transaction起作用。

在调用commit()函数之前,你可以调用addToBackStack()函数将transaction添加到fragment transaction的back stack中。back stack是由activity管理的,这样允许用户通过返回按钮返回到之前的fragment状态。

下面例子显示如何替换一个fragment,将之前的状态保存到back stack中。

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

在这个例子中,newFragment取代了再layout container的存在的fragment(由于使用R.id.fragment_container,指的是layout container里面的fragment),通过调用addToBackStack()来将这个替换操作保存到back stack中,以便用户可以通过返回按钮返回到之前的fragment。

如果你在commit()之前执行多个操作(调用多次add()或者remove()函数),调用commit()后,这些操作会被认定为一个transaction保存到back stack中,通过返回按钮将恢复到这些操作之前的状态。

通过FragmentTransaction执行的操作与执行的顺序无关,但是:

必须最后才调用commit()函数,

如果你通过FragmentTransaction添加多个fragment到同一个container(容器),那么添加的顺序决定了在view中显示的层次。

如果你在移除fragment中没有调用addToBackStack()函数,那么在调用commit函数后,对应的fragment将会被销毁,用户无法通过返回按钮返回到之前的fragment。如果你调用了addToBackStack()函数,那么移除的fragment将会被停止,如果用户按下返回按钮,这个fragment将会被复原(resume)。

建议:对于每个fragment transacion,你可以通过调用函数setTransition()在这之间使用过渡动画,必须在commit()之前。

调用commit()函数不表示transaction马上被执行,而是当activity的UI thread有空时将这个transaction安排传递给这个thread,当然你可以直接调用executePendingTransactions()立即运行已经commit()的transaction。

注意:你必须在activity保存好自己的状态(当用户离开这个activity)后才commit一个transaction。如果你试图在这之前commit,那么将会保存一个exception,只是因为已经commit的状态在activity需要恢复是丢失。可以使用commitAllowingStateLoss()允许commit的状态丢失。

Communicating with the Activity (与activity进行通信)

虽然fragment是独立于activity的对象,可以在多个activity中使用,但是一个给定的fragment实例是跟包含它的activity绑定在一块的。

fragment可以通过getActivity()获取包含它的activity实例,可以简单的得到一个activity layout的一个view:

View listView = getActivity().findViewById(R.id.list);
类似的,你的 activity可以通过FragmentManager的findFragmentById()或者findFragmentByTag()获取Fragment实例。

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

Creating event callbacks to the activity (在activity中实现fragment定义事件响应函数)

在一些情况下,你可能希望activity和fragment能共享事件。一个方法是在fragment定义响应函数接口,要求包含这个fragment的activity实现这个接口。当activity从这个回调函数中接收到相关信息时,可以与activity中其它的fragment共享这个信息。

例如,在一个activity中有两个fragment,一个用来显示文章列表(fragment A),一个用来显示文章内容(fragment)。当一篇文章被选定后,Fragment A必须告诉activity那篇文章被选中,然后activity告诉fragment B该显示哪篇文章。可以在fragment A定义响应函数如下:

public static class FragmentA extends ListFragment {
    ...
    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
}
那么包含这个fragment的activity必须覆盖这个函数,并在这个函数中通知fragment B来自fragment A的事件(哪篇文章被选中),为了保证宿主activity实现这个接口,在fragment A的onAttach()回调函数中(当系统将fragment添加到activity中会调用这个函数)实例化一个OnArticleSelectedListener与宿主activity中。

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");
        }
    }
    ...
}
如果宿主activity没有实现这个接口,那么这个fragment将会抛出一个ClassCastException。如果实现了这个接口,那么么mListener变量保存着宿主activity实现OnAritcleSelectedListener的引用,所以fragment A可以通过定义在OnArticleSelectedListener的函数与宿主activity共享事件。例如,如果fragment是从ListFragment继承而来,每当用户单击一个选项,系统就会调用fragmetn里面的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);
    }
    ...
}
传入onListItemClick()的id是被单击的选项行ID,可以让其他activity(或者其他fragment)从应用的ContentProvider中获取对应的文章。

更多关于使用content provider请参照Content Providers文档。

Adding items to the Action Bar (往操作栏添加项目)

通过调用onCreateOptionMenu(),你的fragment可以往activity的Options Menu添加选项(当然也可以往action bar添加)。为了保证这个函数能接收fragment的调用,你必须在fragment的onCreate()中调用函数setHasOptionsMenu()来说明这个fragment想在option menu中添加选项(不然这个fragment将不会接收到调用onCreateOptionsMenu())

通过fragment天骄到optionmenu的选项都会添加到现有的menu items。当选项被选中中,fragment也会接收到函数onOptionsItemSelected())。

你也可以通过调用registerForContextMenu()在你的fragment layout中注册一个view来提供context menu。当用户打开这个context menu时,fragment会接收到onCreateContextMenu()调用。当用户选择一个选项是,fragment会接收到onContextItemSelected()调用。

注意:当用户选择一个选项时,系统首先会调用activity响应的回调函数,如果activity的回调函数没有处理被选择的选项,才会调用fragment对应的响应函数。对于Options Menu 和 Context Menus都是这样。

关于这部分更多的信息请参照Menus 和Action Bar。

Handing the Fragment Lifecycle (处理fragment的生命周期)

fragment的生命周期跟activity的生命周期过程类似。fragment有三种状态:

Resumed

fragment在运行的activity是可以看到的。

Paused

有另外一个activity在屏幕前端并获得用户焦点(focus),但是fragment所在的activity在前端仍然可以看到(有可能是因为另外一个activity是透明的或者只占屏幕一部分。

Stopped

fragment是不可见的。这可能是因为fragment所在的activity被停止或者当前fragment从activity移除到back stack.一个停止的fragment仍然存活的(所有的状态和成员信息都保存在系统中)。但是它已经对用户不可见了,而且当activity被销毁时,这个fragment也会被销毁。

跟activity类似,你可以通过Bundle对象来保存fragment的状态,特别是在当前activity的进程已经被销毁,而在这个activity被重建是你想恢复之前fragment的状态。你可以在函数onSaveInstanceState()保存相应的状态信息,然后在函数onCreate()或者onCreateView()或者onActivityCreated()提取这些信息。过于如何保存状态,可以参照Activity文档。

activity与fragment生命周期不同之处在于当activity被停止时,这个activity被保存在系统管理的back stack中(所以用户可以通过返回按钮返回之前的activity)。而当fragment被移除时,fragment只有在调用addToBackStack()函数或才会被放入由宿主activity管理的back stack中。

除了上面不同外,管理fragment和activity很类似,managing the activity lifecycle提到的内容也对fragment使用。你只需理解activity的生命周期过程是怎么影响fragment的生命周期的。

注意:如果你需要一个Context(上下文)对象时,可以通过调用getActivity()函数获取这个对象,前提是当前的fragment已经添加到activity中,如果没有或者已经从activity中移除,那么getActivity()会返回null。


Coordinating with the activity lifecycle (与activity的生命周期协调)

fragment所在的activity的生命周期过程影响了fragment的生命周期,比如activity的每个生命周期的回调函数都会使得fragment相应的回调函数被调用。比如,当activity的onPause()被调用时,activity的每个fragment的onPause()函数都会被调用。

onAttach()

当fragment与一个activity相关联是,调用这个函数(相应的activity在这个函数的参数列表中)

onCreateView()

用来关联fragment对应的view。

onActivityCreated()

当activity的onCreate()结束时被调用。

onDestroyView()

当与fragment关联的view被移除时被调用。

onDetach()

当与activity解除关联是被调用。

在上面的图中描述了activity的生命周期过程如何影响在其中的fragment的生命周期。可以看到每个连续的activity状态决定fragment的哪个回调函数被调用。当activity的onCreate()被调用后, fragment的onActivityCreated()紧接着被调用。

当activity处于resumed状态时,你可以自由地在activity中添加或者删除fragment。这决定了只有当前的activity处于resumed状态时,fragment的状态变化才是独立的(即不受所在activity影响)。

Example (例子)

下面例子通过在一个activity中创建两个fragment来创建两个面板布局将这节讲的内容都融合到一块。其中一个用来显示莎士比亚的戏剧名字,一个用来显示第一个显示的名字列表中被选中的戏剧的总结。这个例子显示了如何基于屏幕的配置来那个提供不同的fragment配置。

注意:完整的本例子代码详见FragmentLayout.java。

通过下面代码将activity与layout关联。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_layout);
}

在这个layout中创建两个fragment的layout。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent"
            android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

系统在加载activity的layout的同时实例化了TitlesFragment(用来显示戏剧名字),而用来显示戏剧总结的FragmeLayout现在还为空,直到当用户从左边的戏剧名字列表中选中一个戏剧名字。

但是不是所有的屏幕配置都适合同时显示戏剧的名字和总结的。所以上面的配置只适合屏幕处于水平状态时,这个保存在res/layout-land/fragment_layout.xml中。

当屏幕是竖直时,用下面对应的layout,保存在res/layout/fragment_layout.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
这个layout仅仅包含了TitlesFragment,表面当屏幕是竖直时,只显示戏剧的名字列表。所以当用户选中一个戏剧名字是,这个应用会启动一个新的activity来显示对应的戏剧总结,而不是加载第二个fragment。

下面你将会看到如何实现这两个fragment类。第一个第一个TitleFragment继承自ListFragment这个类。

当用户选中一个戏剧名字时,有可能有两种,取决于现在屏幕是水平还是竖直的,如果是水平的,那么将显示一个新的fragment用来显示戏剧的总结,如果是竖直的,那么将启动一个新的activity。

public static class TitlesFragment extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Populate list with our static array of titles.
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));

        // Check to see if we have a frame in which to embed the details
        // fragment directly in the containing UI.
        View detailsFrame = getActivity().findViewById(R.id.details);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane) {
            // In dual-pane mode, the list view highlights the selected item.
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            // Make sure our UI is in the correct state.
            showDetails(mCurCheckPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    /**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed.
     */
    void showDetails(int index) {
        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment)
                    getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if (index == 0) {
                    ft.replace(R.id.details, details);
                } else {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }
}

第二个fragment:DetailsFragment用来显示被选中的戏剧的总结:

public static class DetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}

在TitlesFragment中,如果当前layout文件中不包含R.id.details这个view(即第二个fragment   DetailsFragment所在的view),那么将会启动DetailsActivity这个activity来显示一个戏剧的总结。

下面是DetailActivity的代码:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}
注意在这个activity中,如果当前屏幕是水平的,这个activity将会finish自己,以便主activity能够显示DetailsFragment。这种情况发生在当起初是竖直的,然后突然转换为水平的情况。

更多关于如何使用fragment的例子详见ApiDemos。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值