Fragment的使用

在activity嵌套activity在谷歌看来是不符合标准的,所以Fragment出现了。

 

从为了解决activity嵌套activity问题的方面来看,1、Fragment跟activity很类似;2、Fragment存在于activity里。

1、Fragment跟activity很类似

这个从生命周期就能看出来,它的生命周期可从官方指南看到:

http://developer.android.com/guide/components/fragments.html

不清楚的可以百度下,有很多这里就不再累述。

 

2、Fragment存在于activity里:

所以怎么管理、使用Fragment就成为了接下来要描述的东西。

首先说说Fragment,Fragment在这里可以看成是activity,利用

public static class FragmentA extends Fragment{}

  

创建一个Fragment,当然接下来你还要重写一些方法,尽管它跟activity相似,但是作用的不同导致某些东西方法还是不同的。

onCreateView方法:

    @Override
    publicView onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState){
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container,false);
    }

To provide a layout for a fragment, you must implement the onCreateView() callback method, which the Android system calls when it's time for the fragment to draw its layout. Your implementation of this method must return a View that is the root of your fragment's layout.

上面都是从官方指南摘取的,也就是说,你必须实现这个接口,这个接口用于返回这个fragment的布局。这个跟activity实现布局有点不太一样,fragment专门使用onCreateView来返回。

 

接下来讲讲,怎么利用activity操作fragment,既然fragment存在于activity里,所以可以想象fragment就跟很多button一样,可以在activity的布局xml文件里体现出来。、

例如:

<?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。

但是,我们也总不能写死了,这样意义就不大了,因此接下来说说怎么动态的操作fragment。

在activity的代码里,写上这些代码:

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

就可以在id名为R.id.fragment_container的标签里放进一个fragment了。FragmentTransaction你可以看成是一个动作,在这里,在FragmentTransaction这个动作在执行之前,我们先对它进行了一些设置,比如这行  fragmentTransaction.add(R.id.fragment_container, fragment);   。然后,再调用commit(),这样,这个动作就执行了,fragment就算是载入了。

fragment是载入了,但是如何替换呢?利用这段代码:

// 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();

注意,这里是replace而不是add。同时addToBackStack()是把它放进一个BackStack,直译过来就是一个栈,这个栈的作用,我举个例子,如果你执行了addToBackStack,当你替换完fragment之后,按下手机的物理键退后键,那么它就跳回到替换之前的那个fragment。但是如果你不执行addToBackStack,那么你按下退后按键之后,就直接结束activity(或者是其他的)了。

 

由于一个小项目的需要,所以还要用到fragment嵌套fragment,而不是activity包含fragment那么简单了。

这块目前还没仔细看,但是,似乎操作原理大同小异,主要是在getFragmentManager这块不同,因为上面的操作得到的是activity对所包含fragment的Manager,而如果是fragment嵌套fragment,那么就需要利用getChildFragmentManager()了。

强烈建议看完官方的Fragment指南:http://developer.android.com/guide/components/fragments.html

在里面,还扩展了一些相关内容,比如状态的保存。这个功能我举个例子,我经常刷微博,有时刷到了一半,看到有评论所以跳到评论模块那里去看,等到看完微博回来,发现我刷了一半的微博又得从头开始刷了。所以状态的保存就可以让你在看玩评论之后回来还能继续刷微博(新浪微博HD就存在这个让人头疼的问题,手机版的就不会)。这中间涉及到生命周期以及参数的问题,所以建议上官方指南看,扩展内容很多。

posted @ 2013-05-15 22:48  yutoulck  阅读(586)  评论(0编辑  收藏  举报