文章目錄
一、Fragment爲何被稱爲第5大組件
1、爲何稱爲第5組件
1)、fragment具有生命週期
2)、fragment比activity更節省內存,ui的切換效果也更加的舒適;
3)、但是fragment必須依附於activity, 加載到activity中去;
2、Fragment加載到activity的兩種方式
1)、添加Fragment到activity的佈局文件中
2)、動態在activity 中添加fragment
public class MainActivity extends Activity implements OnClickListener{
private Button mTabWeixin;
private Button mTabFriend;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化控件和聲明事件
mTabWeixin = (Button) findViewById(R.id.button);
mTabFriend = (Button) findViewById(R.id.button2);
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
// 設置默認的Fragment
setDefaultFragment();
}
private void setDefaultFragment() {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.id_content, new ContentFragment());
transaction.commit();
}
@Override
public void onClick(View v){
// 開啓Fragment事務
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (v.getId()){
case R.id.button:
// 使用當前Fragment的佈局替代id_content的控件
transaction.replace(R.id.id_content, new ContentFragment());
break;
case R.id.button2:
transaction.replace(R.id.id_content, new FriendFragment());
break;
}
transaction.commit();
}
}
public class ContentFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content, container, false);
}
}
public class FriendFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_friend, container, false);
}
}
public class TitleFragment extends Fragment {
private ImageButton mLeftMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "i am an ImageButton in TitleFragment ! ",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
3、FragmentPageAdapter和FragmentStatePageAdapter區別
- 、FragmentPageAdapter適用於頁面較少的情況,FragmentStatePageAdapter適用於頁面較多的情況
因爲FragmentStatePageAdapter每次切換fragment的時候是回收內存的,所以適合頁面多情況
因爲FragmentPageAdapter每次切換fragment的時候只是分離fragment,適合頁面少的情況,也對系統內存沒太大的影響。
二、Fragment的生命週期
onAttach():fragment與Activity關聯後調用。
onCreate(): 初次創建fragment調用,此時的activity並沒有創建完成。
onCreateView():系統在Fragment首次繪製用戶界面的時候調用,該方法返回的view必須是Fragment佈局的根視圖。
onViewCreated(): Fragment的界面UI被繪製好的時候調用,這時候可以初始化fragment裏面的控件資源。
activity的onCreate() : 初始化activity的佈局等
onActivityCreated() : activity被渲染繪製成功之後調用
activity的onStrart() : acticity可見了
onStart() : activity可見之後調用,此時Fragment也是可見的了
activity的onResume() : acticity可交互了
onResume() : Fragment可交互了,可以在Fragment上進行滑動,點擊等等
到這完成了Fragment從啓動到展現的操作;
onPause() : fragment不能和用戶交互了。
activity的onPause() : acticity不可交互了。
onstop():
activity的onstop() :
onDestoryView(): Fragment即將結束,會被保存。
onDestory():
onDetach():Fragment對象被銷燬。
activity的onDestory():整個activity被回收了。
三、Fragment的通信
四、Fragment的replace、add、remove
add()是將Fragment實例添加到activity的Fragment隊列的最上層
remove()是將Fragment實例從到activity的Fragment隊列中刪除
replace()是將activity的Fragment隊列的最上層的fragment實例替換爲要替換的fragment