Fragment的使用有两种方式,一种是静态加载Fragment,一种是动态加载。

一、静态加载

这个比较简单,创建一个类,继承Fragment,如下:

 

 1 package com.example.mycontentresolver;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 
 9 public class InsertFragment extends Fragment {
10     View rootView = null;
11 
12     @Override
13     public View onCreateView(LayoutInflater inflater, ViewGroup container,
14             Bundle savedInstanceState) {
15         // TODO Auto-generated method stub
16         /*if(container != null){
17             ViewGroup parent = (ViewGroup) container.getParent();
18             if(parent != null){
19                 parent.removeAllViews();
20             }
21         }*/
22         if(rootView == null){
23             rootView = inflater.inflate(R.layout.insert_data, container, false);
24         }
25         return rootView;
26         //return super.onCreateView(inflater, container, savedInstanceState);
27     }
28 }

对应的insert_data.xml很简单:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView android:layout_width="match_parent"
 7         android:layout_height="wrap_content"
 8         android:layout_gravity="center_horizontal"
 9         android:text="please inset data"/>
10     
11 
12 </LinearLayout>


然后创建MainActivity.java,继承Activity,如下:

 1 package com.example.mycontentresolver;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.Fragment;
 6 import android.app.FragmentManager;
 7 import android.app.FragmentTransaction;
 8 import android.view.Menu;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class MainActivity extends Activity{
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18     }
19     
20 
21     @Override
22     public boolean onCreateOptionsMenu(Menu menu) {
23         // Inflate the menu; this adds items to the action bar if it is present.
24         getMenuInflater().inflate(R.menu.main, menu);
25         return true;
26     }
27 
28 
29 }

activity_main.xml代码如下:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2      android:orientation="vertical"
3      android:layout_width="fill_parent"
4      android:layout_height="fill_parent">
5       <fragment android:name="com.example.mycontentresolver.InsertFragment"
6          android:id="@+id/fragment"
7         android:layout_width="match_parent"
8         android:layout_height="match_parent"/>
9 </LinearLayout>


这样InsertFragment就显示在了activity中

二、动态加载

需要注意的是要使用FragmentManager对象获取transaction去添加或者替换Fragment,并且提交事务。注意,每次使用必须重新构建,负责出现线程报满的错误。

如上中的InsertFragment,继续添加DeleteFragment、UpdateFragment、QueryFragment,对应的xml文件也都类似,这里不列举。

在显示的Activity中的布局文件中添加四个按钮,用来切换以上四个Fragment,需要注意的是这时候不能使用fragment标签,需要使用容器,比如FrameLayout用来容纳和显示我们的Fragment,代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2      android:orientation="vertical"
 3      android:layout_width="fill_parent"
 4      android:layout_height="fill_parent">
 5      <LinearLayout android:orientation="horizontal"
 6          android:layout_width="match_parent"
 7          android:layout_height="wrap_content">
 8          <Button android:id="@+id/insert_button"
 9              android:layout_width="0dp"
10              android:layout_weight="1"
11              android:layout_height="wrap_content"
12              android:text="Insert"/>
13          <Button android:id="@+id/delete_button"
14              android:layout_width="0dp"
15              android:layout_height="wrap_content"
16              android:layout_weight="1"
17              android:text="Delete"/>
18          <Button android:id="@+id/update_button"
19              android:layout_width="0dp"
20              android:layout_height="wrap_content"
21              android:layout_weight="1"
22              android:text="Update"/>
23          <Button android:id="@+id/query_button"
24              android:layout_width="0dp"
25              android:layout_height="wrap_content"
26              android:layout_weight="1"
27              android:text="Query"/>
28      </LinearLayout>
29      <FrameLayout android:id="@+id/fragment"
30         android:layout_width="match_parent"
31         android:layout_height="match_parent">
32      </FrameLayout>
33      
34 </LinearLayout>

MainActivity.java中这样写:

 1 package com.example.mycontentresolver;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.Fragment;
 6 import android.app.FragmentManager;
 7 import android.app.FragmentTransaction;
 8 import android.view.Menu;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class MainActivity extends Activity implements OnClickListener{
14     Fragment insertFragment, updateFragment, deleteFragment, queryFragment;
15     
16     Button insert, delete, update, query;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         insert = (Button) findViewById(R.id.insert_button);
22         delete = (Button) findViewById(R.id.delete_button);
23         update = (Button) findViewById(R.id.update_button);
24         query = (Button) findViewById(R.id.query_button);
25         insert.setOnClickListener(this);
26         delete.setOnClickListener(this);
27         update.setOnClickListener(this);
28         query.setOnClickListener(this);
29         setDefaultFragment();
30     }
31     
32     public void setDefaultFragment(){
33         FragmentManager fm = getFragmentManager();;
34         FragmentTransaction transaction = fm.beginTransaction();
35         insertFragment = new InsertFragment();
36         transaction.add(R.id.fragment, insertFragment);
37         transaction.commit();
38     }
39 
40     @Override
41     public boolean onCreateOptionsMenu(Menu menu) {
42         // Inflate the menu; this adds items to the action bar if it is present.
43         getMenuInflater().inflate(R.menu.main, menu);
44         return true;
45     }
46 
47     @Override
48     public void onClick(View v) {
49         // TODO Auto-generated method stub
50         
51         FragmentManager fm = getFragmentManager();;
52         FragmentTransaction transaction = fm.beginTransaction();
53         int viewId = v.getId();
54         switch (viewId) {
55         case R.id.insert_button:
56             
57             if(insertFragment == null)
58                 insertFragment = new InsertFragment();
59             transaction.replace(R.id.fragment, insertFragment);
60             break;
61         case R.id.delete_button:
62             
63             if(deleteFragment == null)
64                 deleteFragment = new DeleteFragment();
65             transaction.replace(R.id.fragment, deleteFragment);
66             break;
67         case R.id.update_button:
68             
69             updateFragment = new UpdateFragment();
70             transaction.replace(R.id.fragment, updateFragment);
71             break;
72         case R.id.query_button:
73             
74             queryFragment = new QueryFragment();
75             transaction.replace(R.id.fragment, queryFragment);
76             break;
77         default:
78             break;
79         }
80         transaction.commit();
81     }
82 
83 }

虽然代码和逻辑看上去简单,但是对于新手来说写起来还是有些问题的,会遇到以上我提到的注意中的问题

问题1、提示parent已经添加了view,需要使用removeView删除后再添加

问题2、提示线程报满