概述
Fragment是Android honeycomb 3.0新增的概念,,在Android——Fragment介绍、Android Fragment使用、Android FragmentManage FragmentTransaction介绍中做了关于Fragment的详细介绍。Fragment的添加有两种方式,第一种是直接静态的写在XML中,而另一种就是动态的用JAVA添加,下面将一一的介绍.
Fragment的添加静态xml
这里将介绍如何静态的添加Fragment.
创建Fragment
public class FooFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Defines the xml file for the fragmentView view = inflater.inflate(R.layout.foo, container, false);// Setup handles to view objects here// etFoo = (EditText) view.findViewById(R.id.etFoo);return view;}}需要注意的是这里的Fragent的包必须与后面将使用的FragmentActivity的包出至相同packet(android.app或android.support.v4.app).
然后简单的添加布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent" android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /></LinearLayout>接下来编写Activity的布局文件注意其中的name为上面实现的类全名<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /><fragmentandroid:name="com.example.android.FooFragment"android:id="@+id/fooFragment"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>简单的添加布局到Activity即可(Activity继承至FragmentActivity)import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {// …}动态添加Fragment
要改变的是Activity的布局,不是使用<fragment>标签,代替他的是<FrameLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" > <TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /> <FrameLayoutandroid:id="@+id/your_placeholder"android:layout_height="match_parent"> </FrameLayout></LinearLayout>值得注意的是在此处的Activity不需要再继承至FragmentActivity,
在Activity的onCreat()方法中动态的加载
// Begin the transactionFragmentTransaction ft = getSupportFragmentManager().beginTransaction();// Replace the container with the new fragmentft.replace(R.id.your_placeholder, new FooFragment());// or ft.add(R.id.your_placeholder, new FooFragment());// Execute the changes specifiedft.commit();此时原布局的FrameLayout被自定义的Fragment替换Fragment的生命周期
管理Fragment的回退栈
有时,我们希望在Fragment A点击某个组件后跳到B,然后在点击到C,在C页面按back按钮回到B,再按回到A,实现以下效果
此时我们需要使用API
FragmentTransaction addToBackStack()
我们需要在每次提交之前保存该fragment到栈内
// Create the transactionFragmentTransaction fts = getSupportFragmentManager().beginTransaction();// Replace the content of the containerfts.replace(R.id.flContainer, new FirstFragment());// Append this transaction to the backstackfts.addToBackStack("optional tag");// Commit the changesfts.commit();
注意如需要多次执行commit,每次都要重新获取FragmentTransaction,.因为会话只能提交一次,否则异常.
不仅仅只有再按back按钮是可以实现以上效果,只要有我们书写一下代码,既可以响应任何时候的Fragment返回
FragmentManager fragmentManager = getSupportFragmentManager();if (fragmentManager.getBackStackEntryCount() > 0) {fragmentManager.popBackStack();}Fragment与ActionBar添加Menu放下一种执着,收获一种自在。放下既是一种理性抉择,也是一种豁达美。