一、什麼是Fragment?
1、Fragment是activity的界面中的一部分;多個Fragment們組合到一個activity中;多個activity中可重用一個Fragment。即Fragment相當於模塊化的一段activity;
2、具有自己的生命週期,接收自己的事件;
3、在activity運行時被添加或刪除。
二、爲什麼要使用Fragment?
1、支持更動態靈活的界面設計;2、activity的layout分成Fragment。
三、如何使用Fragment?
1、Create Fragment類 — onCreate() onCreateView() onPause()
2、Add Fragment
(1) Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.chenjinhua.redcircle.FragmentTest"
></fragment>
</LinearLayout>
activity_fragment.xml佈局文件裏添加如上代碼後,運行,報如下錯誤:非法狀態異常,Fragment沒有創建View。
解決方法:
首先新建一個view佈局文件如之前第二週寫的item_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<ImageView
android:id="@+id/avatar_imageview"
android:layout_width="50dp"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:layout_toRightOf="@+id/avatar_imageview"/>
<TextView
android:id="@+id/item_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="age"
android:layout_below="@+id/item_name"
android:layout_toRightOf="@+id/avatar_imageview"/>
</RelativeLayout>
然後在FragmentTest類的onCreateView()方法裏獲取view:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_listview,container);
return view;
}
(2)Java Code
FragmentManager;
FragmentTransaction;
Add/Remove
FragmentTestActivity類
package com.example.chenjinhua.redcircle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* Created by chenjinhua on 16/3/19.
*/
public class FragmentTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentTest fragmentTest = new FragmentTest();
fragmentTransaction.add(R.id.fragment_test_view,fragmentTest).commit();
}
}
activity_fragment.xml佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.chenjinhua.redcircle.FragmentTest"
></fragment>
<RelativeLayout
android:layout_marginTop="100dp"
android:id="@+id/fragment_test_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
FragmentTest類 同上。
運行報如下錯誤:
FragmentTestActivity類裏fragment_test_view是父view;FragmentTest類裏item_listview是子view。
解決辦法:修改FragmentTest類裏的onCreateView()方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//解析出來的xml視圖放到container裏,不綁定
View view = inflater.inflate(R.layout.item_listview,container,false);
return view;
}
程序運行結果:
第一個Fragment是通過佈局文件生成的,第二個Fragment是通過java code生成的。
刪除fragment:修改FragmentTestActivity類代碼爲d
fragmentTransaction.add(R.id.fragment_test_view,fragmentTest);
fragmentTransaction.remove(fragmentTest).commit();
四、如何管理Fragment?
1、查找Fragment — findFragmentById() 和 findFragmentByTag()
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_test);
if (fragment instanceof FragmentTest){
// TODO: Do your Action.
}else{
throw new IllegalStateException("iis not fragmentTest");
}
java裏面的二元運算符,判斷左邊的對象是否是右邊類的實例。假如是的話,返回true;假如不是的話,返回false
2、Fragment的後退 — Fragment Stack; popBackStack(); addOnBackStackChangedListener()
五、Fragment 生命週期
參考博客:
http://blog.csdn.net/forever_crying/article/details/8238863/