1>Fragment能夠做爲activity界面的一部分組成出現。
2>能夠在一個activity中同時出現多個fragment,而且一個fragment也能夠在多個activity中使用。
3>在activity運行過程當中,能夠添加、移除或者替換Fragment。
4>Fragment能夠響應本身的輸入事件,而且有本身的生命週期,他們的生命週期會受宿主activity的生命週期的影響。
Fragment第一次繪製它的用戶界面的時候,系統會調用onCreateView()方法爲了繪製Fragment的UI,此方法必須返回view,若是不顯示UI,返回NULL便可。
Fragment的加載方式:
在activity的layout文件中聲明Fragment,須要特別注意的是<Fragment >中的android:name屬性指定了在layout中實例化的Fragment類。
標識Fragment的方法:
android:id 屬性提供一個惟一的ID
android:tag 屬性提供一個惟一的字符串
Fragmen 靜態加載
mian.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
fragment
android:id
=
"@+id/fragment"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:name
=
"com.example.fragment.MyFragment"
/>
</
LinearLayout
>
Fragment.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TextView
android:id
=
"@+id/textview"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
Button
android:id
=
"@+id/button"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
</
LinearLayout
>
public
class
MyFragment
extends
Fragment {
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
* layout佈局文件轉換爲view對象
* resource:fragment須要加載的佈局文件
* root:加載layout的父 viewgroup
* attachToRoot: boolean false,不返回父 viewgroup
*/
View view=inflater.inflate(R.layout.
fragment
, container,
false
);
TextView textView=(TextView) view.findViewById(R.id.
textview
);
return
view;
}
}
public
class
MainActivity2
extends
Activity{
private
TextView
textView
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
mian2
);
textView
=(TextView) findViewById(R.id.
textview
);
Button button=(Button) findViewById(R.id.
button
);
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
textView
.setText(
"az"
);
}
public
class
MainActivity
extends
ActionBarActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
Intent intent=
new
Intent(
this
,MainActivity2.
class
);
startActivity(intent);
}
}
Fragment動態加載
撰寫代碼將fragment添加到一個activity layout中
add(): 添加一個Fragment(指定要添加的fragment和插入的view)與此相似的還有remove()、替換().
處理Fragment事務
根據用戶的交互狀況,對fragment進行添加、移除、替換,以及執行其餘動做,提交給activity的每一套變化被稱做一個事務。
FragmentManager fragmentManager=getFragmentManager();
FragmentTranSactioin beginTransaction=fragmentManager.beginTransaction();
每個事務都是同事執行一套變化,能夠在一個事務中設置你想要執行的變化、包括add()、remove()、replace(),而後提交給activity,必須調用commit()方法。
若是容許用戶經過按下BACK按鈕返回到前一個Fragment狀態,調用commit()以前能夠加入addToBackStack()方法。
MyFragment2 fragment2=
new
MyFragment2();
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction beginTransaction=fragmentManager.beginTransaction();
beginTransaction.add(R.id.
from
, fragment2);
beginTransaction.addToBackStack(
null
);
//容許物理按鈕回退
beginTransaction.commit();
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
RelativeLayout
android:id
=
"@+id/from"
></
RelativeLayout
>
</
LinearLayout
>
Fragment的生命週期
public
class
MyFragment
extends
Fragment {
/**
* 每次穿件都會繪製fragment的view組件式回調該方法
*/
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//
TODO
Auto-generated method stub
return
super
.onCreateView(inflater, container, savedInstanceState);
}
/**
* 當fragment被添加到activity時回調該方法,只會回調一次
*/
@Override
public
void
onAttach(Activity activity) {
super
.onAttach(activity);
}
/**
* 建立fragment時回調,只會回調一次
*/
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
}
/**
* 當fragment所在activity啓動完成後調用
*/
@Override
public
void
onActivityCreated(Bundle savedInstanceState) {
super
.onActivityCreated(savedInstanceState);
}
/**
* 啓動fragment
*/
@Override
public
void
onStart() {
super
.onStart();
}
/**
* 回覆fragment時會被回調,調用 onstart()方法後面必定會調用onResume()方法
*/
@Override
public
void
onResume() {
super
.onResume();
}
/**
* 暫停fragment
*/
@Override
public
void
onPause() {
super
.onPause();
}
/**
* 中止fragment
*/
@Override
public
void
onStop() {
super
.onStop();
}
/**
* 銷燬fragment所包含的view組件式纔會調用
*/
@Override
public
void
onDestroyView() {
super
.onDestroyView();
}
/**
* 銷燬fragment時會被回調
*/
@Override
public
void
onDestroy() {
super
.onDestroy();
}
/**
* fragment從activity中刪除時會回調該方法,並只會回調一次
*/
@Override
public
void
onDetach() {
super
.onDetach();
}
}