Fragment翻譯爲碎片,功能和Activity類似,依賴於父Activity,有自己獨立的聲明週期,用來描述一些行爲或一部分用戶界面,在一個Activity中你可以合併多個fragment,在一個單獨的activity中建立多個UI面板,同時重用fragment在多個activity中使用.你可以認爲fragment作爲一個activity中的一子模塊 ,接收自己的輸入事件,你可以向運行中的activity添加或移除fragment。兩個Activity通過兩個Fragment合併到一個Activity的佈局方式.
下面上例子:
效果圖:
首先創建main.xml爲第一啓動頁面
在一個activity的佈局文件中嵌入兩個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.jiayuan.fragment.fragmentImp.FirstFragment"
android:id="@+id/firstFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment android:name="com.jiayuan.fragment.fragmentImp.SecondFragment"
android:id="@+id/secondFragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<Button android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱藏"
/>
<Button android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱藏"
/>
</LinearLayout>
分別使用getFragmentManager().findFragmentById(id);
getFragmentManager().findFragmentByTag(id);
FragmentManager:
可以使用FragmentManager來管理Fragment。在Activity中通過getFragmentManager來獲得。FragmentManager
類一些主要的方法有通過findFragmentById()來獲取一個Activity中有關Fragment佈局。
當然還有類似findFragmentByTag()方法,以及當Fragment中出棧的popBackStack()同時可以註冊 addOnBackStackChangedListener()管理。具體的可以在android.app.FragmentManager類中瞭解。
可以使用FragmentManager進行事物處理。通過begintransaction方法獲取一個事物處理實例。
FragmentTransaction transaction = fragmentManager.beginTransaction();
在這期間可以使用 add(), remove()和replace()。最終需要改變時執行 commit()。
然後創建兩個fragment的佈局文件first.xml和second.xml,其實這兩個佈局文件和activity文件第一完全相同,這個就隨意定義兩個
frist.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello,你是美女?" />
</LinearLayout>
second.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sorry,我是醜男...." />
</LinearLayout>
在然後定義兩個fragement的實現類FirstFragment.java和SecondFragment.java
1、FirstFragment.java
package com.jiayuan.fragment.fragmentImp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
public class FirstFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//繪製視圖
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.first, container, false);
registerForContextMenu(root.findViewById(R.id.editText1));
return root;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, 0, Menu.NONE, "菜單1");
menu.add(Menu.NONE, 1, Menu.NONE, "菜單2");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
}
2、SecondFragment.java
package com.jiayuan.fragment.fragmentImp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.second, container, false);
}
}
做幾點說明:
1、你可以在fragement的實現方法的onCreate生命週期方法中使用res/layout/xxx.xml文件,就像activity一樣。
2、如果你顯示的爲列表,那麼你就可以使用PreferenceFragment來實現,它裏封裝了ListView用作列表顯示。其使用的佈局文件爲res/xml/xxx.xml
3、當系統調用fragment在首次繪製用戶界面時,如果畫一個UI在你的fragment你必須返回一個View當然了你可以返回null代表這個fragment沒有UI.
在然後是在activity中使用
TestFragmentActivity.java
package com.jiayuan.frament.activity;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestFragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// FirstFragment firstFragment=new FirstFragment();
// //在Activity中通過這個與Fragment通訊
// getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit();
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment));
addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment));
}
void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findViewById(buttonId);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
//爲Fragment設置淡入淡出效果
ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
if (fragment.isHidden()) {
ft.show(fragment);
button.setText("隱藏");
} else {
ft.hide(fragment);
button.setText("顯示");
}
ft.commit();
}
});
}
}
描述一下fragement的生命週期
生命週期圖:
1、從這個圖裏面可以看出,fragment的聲明週期方法並不像activity那樣容易理解
2、一個fragment必須總是嵌入在一個activity中,同時fragment的生命週期受activity而影響
3、這是activity和fragment的回調順序:
activity.onCreate
fragment.onAttach
fragment.onCreate
activity.onStart
fragment.onActivityCreated
fragment.onStart
activity.onResume
fragment.onResume
fragment.onStop
activity.onStop
fragment.onDestroyView
fragment.onDestroy
fragment.onDetach
activity.onDestroy
4、當activity 暫停,那麼所有在這個activity的fragments將被destroy釋放。 你可以在activity的不同生命週期中,對fragment執行不同操作
這是本人在研究android3.0特性的過程中,個人整理的一些資料,