Fragment亩鬼,翻譯為碎片本谜、片段纹冤,Android 在 Android 3.0(API 11 )中引入了Fragment清酥,主要是為了給大屏幕(如平板電腦)上更加動態(tài)和靈活的 UI 設計提供支持,本文帶來Fragment完全解析凿傅,關于Fragment缠犀,你應該了解的一切!
一聪舒、Fragment概述
二辨液、靜態(tài)Fragment用法與解析
三、動態(tài)Fragment用法與解析
四箱残、Fragment生命周期
五滔迈、Fragment與Activity通信
六、Fragment總結
一被辑、Fragment概述
1.Fragment是什么
2.Fragment的設計目的
這部分主要簡單介紹下Fragment是什么燎悍,與Activity的關系,以及Fragment的設計目的等部分盼理,主要是給大家一個總體上的把握谈山,之后會有Fragment的用法和詳細解析。
1. Fragment是什么
AFragmentrepresents a behavior or a portion of user interface in anActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Fragment表示Activity中的行為或者用戶界面的一部分宏怔,我們可以在單個Activity中組合多個Fragment來構建多格的界面奏路,也可以在多個Activity之間重用同一個Fragment。我們可以把Fragment想象成Activity的一個模塊部分臊诊,它有自己的生命周期思劳,可以接受用戶事件,允許你在Activity正常運行期間添加或者移除妨猩,類似一個你可以在不同的Activity之間復用的子Activity。
Fragment必須綁定在Activity之中秽褒,它的生命周期也直接受著宿主Activity生命周期的影響壶硅。例如,當Activity暫停時销斟,其中的所有Fragment也暫停庐椒,當Activity被銷毀時,其中的所有Fragment也銷毀蚂踊。但是约谈,當Activity正常運行時,你可以單獨地管理每個Fragment,添加或移除棱诱。
2. Fragment的設計目的
Android應用運行在各種各樣的設備中泼橘,有小屏幕的手機,超大屏的平板甚至電視迈勋。針對屏幕尺寸的差距炬灭,很多情況下,都是先針對手機開發(fā)一套App靡菇,然后拷貝一份重归,修改布局以適應不同設備的需要。那么有沒有辦法可以做到一個App同時適應手機和平板呢厦凤?Fragment的出現(xiàn)就是為了解決這樣的問題的鼻吮。
Android 在 Android 3.0(API 11 級)中引入了片段,主要是為了給像平板電腦這類設備的大屏幕更加動態(tài)和靈活的 UI 設計提供支持较鼓。由于平板電腦的屏幕比手機屏幕大得多椎木,因此可用于組合和交換 UI 組件的空間更大。利用片段實現(xiàn)此類設計時笨腥,您無需管理對視圖層次結構的復雜更改拓哺。 通過將 Activity 布局分成片段,您可以在運行時修改 Activity 的外觀脖母,并在由 Activity 管理的返回棧中保留這些更改士鸥。
例如,新聞應用可以使用一個Fragment 在左側顯示文章列表谆级,使用另一個Fragment 在右側顯示文章烤礁,兩個片段并排顯示在一個 Activity 中,每個片段都具有自己的一套生命周期回調方法肥照,并各自處理自己的用戶輸入事件脚仔。 因此,用戶不需要使用一個 Activity 來選擇文章舆绎,然后使用另一個 Activity 來閱讀文章鲤脏,而是可以在同一個 Activity 內選擇文章并進行閱讀,如圖所示吕朵。
不同設備上的展示方式
二猎醇、靜態(tài)Fragment用法與解析
之所以稱之為靜態(tài),是因為我們在布局文件里直接把Fragment看做是View作為Activity布局的一部分努溃,大家可以看下面這張圖硫嘶,我已經標出了1、2兩部分梧税,的確沦疾,它是由兩個Fragment組成称近,上面是標題,下面是內容部分哮塞,內容部分還有一個按鈕刨秆。
下面我們按步驟創(chuàng)建出上面的布局,主要是讓大家熟悉Fragment的基本使用彻桃,在用到Fragment導入包時坛善,我選擇的是不包含V4的包,因為我的minSdkVersion設置成了14邻眷,如果你的應用要支持11即Android 3.0以下的版本眠屎,則導入的是v4的包。
- 創(chuàng)建TopFragment肆饶,主要就是重寫onCreateView方法初始化界面改衩。
/**
* Created by JackalTsc on 2016/7/11.
*/
public class TopFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//初始化Fragment界面
View view = inflater.inflate(R.layout.view_fragment_top, container, false);
return view;
}
}
- 創(chuàng)建TopFragment的布局 view_fragment_top.xml,其中只有一個TextView驯镊。
<?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="50dp"
android:background="#0000ff"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="應用寶"
android:textColor="#ffffff"/>
</LinearLayout>
- 創(chuàng)建BottomFragment葫督, 方法onCreateView里初始化BottomFragment界面。
/**
* Created by JackalTsc on 2016/7/11.
*/
public class BottomFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//初始化Fragment界面
View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_fragment_bottom, container, false);
Button btnHome = (Button) view.findViewById(R.id.btn_home);
btnHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "選擇了首頁", Toast.LENGTH_LONG).show();
}
});
return view;
}
}
4.創(chuàng)建BottomFragment的布局 view_fragment_bottom.xml板惑,其中包含一個TextView和一個Button橄镜。
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="首頁內容"
android:textSize="50sp"/>
<Button
android:id="@+id/btn_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="首頁"/>
</RelativeLayout>
5、創(chuàng)建Activity的布局 layout_activity_fragment.xml冯乘,其中包含兩個Fragment洽胶。
<?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_top"
android:name="com.jackaltsc.android.mydemoproject.fragment.TopFragment"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<fragment
android:id="@+id/fragment_bottom"
android:name="com.jackaltsc.android.mydemoproject.fragment.BottomFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
6、最后我們只需要在Activity里設置布局即可裆馒,看姊氓,這里的邏輯十分簡單,當我們點擊評論按鈕時喷好,會有一個Toast消息翔横,但是邏輯是在BottomFragment里進行處理的。有人可能會問梗搅,做了這么多工作好像很麻煩禾唁,但是我們應該記住,如果Fragment的界面有很好的通用性无切,那么重用的地方就會很多蟀俊,為我們之后的工作節(jié)省不少功夫。
/**
* Created by JackalTsc on 2016/7/11.
*/
public class Fragment1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_fragment);
}
}
靜態(tài)Fragment小結:
- Fragment里面重寫回調方法onCreateView用來初始化Fragment的界面订雾,方法里LayoutInflater對象可以擴展指定布局,返回View即為Fragment布局矛洞,這樣Fragment即可作為Activity界面的一部分洼哎。
- Activity的界面里在包含F(xiàn)ragment時烫映,F(xiàn)ragment的android:name屬性為全限定名,用來實例化指定的Fragment噩峦,同時別忘了要給Fragment一個唯一的id锭沟,這個很重要,系統(tǒng)在Activity恢復時用它來存儲狀態(tài)识补。
三族淮、動態(tài)Fragment用法與解析
動態(tài)Fragment主要是可以在Activity正常運行期間的任意時刻添加、刪除或者替換Fragment凭涂。我們可以看下面這張圖祝辣,它是由兩部分組成,第1部分是內容切油,第2部分是兩個按鈕蝙斜,當點擊不同的按鈕時,內容部分是會變化的澎胡。
下面是詳細的步驟孕荠。
1、首先是HomeFragment
/**
* Created by JackalTsc on 2016/7/11.
*/
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_fragment_home, container,false);
return view;
}
}
2攻谁、它的布局為 layout_fragment_home.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:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="首頁內容"/>
</LinearLayout>
3稚伍、其次是FindFragment
/**
* Created by JackalTsc on 2016/7/11.
*/
public class FindFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_fragment_find, container,false);
return view;
}
}
4、它的布局為 layout_fragment_find.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:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)現(xiàn)內容"
android:textSize="50sp"/>
</LinearLayout>
5戚宦、Activity的布局為 layout_activity_fragment2.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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="首頁"/>
<Button
android:id="@+id/btn_find"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="發(fā)現(xiàn)"/>
</LinearLayout>
</LinearLayout>
6个曙、最后Activity里的代碼
/**
* Created by JackalTsc on 2016/7/11.
*/
public class Fragment2Activity extends Activity implements View.OnClickListener {
private Button btnHome,btnFind;
private HomeFragment homeFragment;
private FindFragment findFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_fragment2);
//初始化
init();
}
private void init() {
btnHome = (Button) findViewById(R.id.btn_home);
btnFind = (Button) findViewById(R.id.btn_find);
btnHome.setOnClickListener(this);
btnFind.setOnClickListener(this);
getFragment(0);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_home:
Toast.makeText(Fragment2Activity.this, "首頁", Toast.LENGTH_SHORT).show();
getFragment(0);
break;
case R.id.btn_find:
Toast.makeText(Fragment2Activity.this, "發(fā)現(xiàn)", Toast.LENGTH_SHORT).show();
getFragment(1);
break;
}
}
private void getFragment(int i) {
//Fragment管理器
FragmentManager manager = getFragmentManager();
//事務
FragmentTransaction transaction = manager.beginTransaction();
switch (i) {
case 0:
if (homeFragment == null) {
homeFragment = new HomeFragment();
}
//選中首頁 內容部分就替換為首頁的Fragment
transaction.replace(R.id.fragment_container, homeFragment);
break;
case 1:
if (findFragment == null) {
findFragment = new FindFragment();
}
//選中發(fā)現(xiàn) 內容部分就替換為發(fā)現(xiàn)的Fragment
transaction.replace(R.id.fragment_container, findFragment);
break;
}
//只有提交才有效果
transaction.commit();
}
}
這樣,你應該可以看到了阁苞,點擊不同的Button時困檩,內容部分是會切換的。Fragment2Activity里的代碼可能比較長那槽,但是并不復雜悼沿,我們重點關注getFragment這個函數里的東西,它主要是先用getFragmentManager()獲取Fragment管理器骚灸,再獲得Fragment事務糟趾,由事務進行替換,最后commit()甚牲。
動態(tài)Fragment使用小結
-
關于FragmentManager
FragmentManager义郑,F(xiàn)ragment管理器,是在Activity里進行Fragment交互的接口丈钙,可以通過getFragmentManager()方法獲取當前Activity相關的事務管理器非驮。 - **關于FragmentTransaction **
FragmentTransaction,F(xiàn)ragment事務雏赦,是執(zhí)行Fragment一系列操作的接口劫笙,可以通過FragmentManager對象的beginTransaction()獲得芙扎,一般常用的操作主要有add,replace填大,remove戒洼,show,hide等等允华。當然圈浇,F(xiàn)ragment的操作最后都要commit提交才有效。
四靴寂、Fragment生命周期
Fragment的生命周期與Activity的生命有很多相似的地方磷蜀,同時也有不同的地方,比如Fragment與Activity一樣都有三種狀態(tài):
- Resumed
Fragment在運行的Activity里處于可見狀態(tài)
-
Paused
別的Activity正常運行狀態(tài)榨汤,但是Fragment的宿主Activity仍處于可見狀態(tài) -
Stopped
Fragment處于不可見狀態(tài)
Activity的生命周期對它的影響蠕搜。官網上的這張圖很清楚。
下面介紹幾個Fragment中的主要回調方法收壕,
-
onAttach()
當fragment和activity關聯(lián)時調用妓灌。 -
onCreateView()
當創(chuàng)建Fragment的View視圖時調用 -
onActivityCreated()
當Activity的onCreate()方法返回時調用 -
onDestroyView()
當移除Fragment的View視圖時調用 -
onDetach()
當fragment和activity解除關聯(lián)時調用。
Activity和Fragment生命周期最大的不同之處在于它們在各自的回退棧中的存儲方式不一樣蜜宪。默認情況下虫埂,一個Activity在停止時是存放在由系統(tǒng)管理的Activity回退棧中。但是圃验,F(xiàn)ragment在移除的事務處理中掉伏,只有在你使用addToBackStack()方法明確請求保存實例狀態(tài)時,它才會存放在由宿主Activity管理的回退棧中澳窑。
關于Fragment回退棧
類似與Android系統(tǒng)為Activity維護一個任務棧斧散,我們也可以通過Activity維護一個回退棧來保存每次Fragment事務發(fā)生的變化。如果你將Fragment任務添加到回退棧摊聋,當用戶點擊后退按鈕時鸡捐,將看到上一次的保存的Fragment。一旦Fragment完全從后退棧中彈出麻裁,用戶再次點擊后退鍵箍镜,則退出當前Activity。
五煎源、Fragment與Activity通信
雖然Fragment是實現(xiàn)成一個獨立于Activity的對象并且可以在多個Activity中重復使用色迂,但是一個指定的Fragment實例是直接與包含它的Activity綁定的。Fragment與Activity通信情況主要有以下幾種手销。
-
Fragment獲取所在的Activity
在Fragment中可以用getActivity()來訪問Activity的實例歇僧,并且可以執(zhí)行一些操作,比如查找一個視圖:
View listView = getActivity().findViewById(R.id.list);
-
Activity獲取它包含的Fragment
Activity 可以通過從FragmentManager獲取對Fragment的引用來調用片段中的方法锋拖,findFragmentById()或findFragmentByTag()诈悍。
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
-
Fragment與Activity共享數據
有時候埂淮,如果需要Fragment和Activity共享事件,最好的方法是在Fragment里定義一個內部回調接口写隶,再讓包含該Fragment的Activity實現(xiàn)該回調接口,這樣Fragment即可調用該回調方法將數據傳給Activity讲仰。
針對第3種情況慕趴,這里有個簡單的例子,是我參考瘋狂Android講義上的內容修改而成的鄙陡,是個不錯的學習Demo冕房。如下圖所示,分成兩個部分趁矾,左邊是食物的標題耙册,右邊是詳細介紹。點擊不同的標題時毫捣,詳情部分會切換详拙。
- 首先,新建一個FoodUtil 類蔓同,這個類主要包含一個實體FoodEntity 和相關數據集合饶辙,在之后會有用處。
/**
* Created by JackalTsc on 2016/7/12.
*/
public class FoodUtil {
//Food實體
public static class FoodEntity {
private int fId;
private String fTitle;
private String fDetail;
public FoodEntity(int fId, String fTitle, String fDetail) {
this.fId = fId;
this.fTitle = fTitle;
this.fDetail = fDetail;
}
@Override
public String toString() {
return getfTitle();
}
public String getfTitle() {
return fTitle;
}
public void setfTitle(String fTitle) {
this.fTitle = fTitle;
}
public int getfId() {
return fId;
}
public void setfId(int fId) {
this.fId = fId;
}
public String getfDetail() {
return fDetail;
}
public void setfContent(String fContent) {
this.fDetail = fDetail;
}
}
//Book相關數據集合
public static List<FoodEntity> foodList = new ArrayList<>();
public static Map<Integer, FoodEntity> foodMap = new HashMap<>();
//添加數據
static {
addData(new FoodEntity(1, "土豆", "屬茄科多年生草本植物斑粱,塊莖可供食用弃揽,是全球第四大重要的糧食作物。"));
addData(new FoodEntity(2, "花生", "原名落花生则北,是我國產量豐富矿微、食用廣泛的一種堅果,又名“長生果”尚揣、“泥豆”等涌矢。"));
addData(new FoodEntity(3, "番茄", "是茄科番茄屬一年生或多年生草本植物,體高0.6-2米惑艇,全體生粘質腺毛蒿辙,有強烈氣味,莖易倒伏滨巴。"));
}
private static void addData(FoodEntity food) {
foodList.add(food);
foodMap.put(food.fId, food);
}
}
2思灌、之后是左邊的標題TitleFragment,它是一個繼承ListFragment的類恭取,設置了數據適配器時會有列表的效果泰偿。
/**
* Created by JackalTsc on 2016/7/12.
*/
public class TitleFragment extends ListFragment {
private Callbacks mCallbacks;
//ListFragment的某項被選中時的回調接口
public interface Callbacks {
public void onItemSelected(Integer id);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ListFragment創(chuàng)建時設置數據適配器 這里FoodUtil.foodList是我們靜態(tài)設置的數據集合
setListAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, FoodUtil.foodList));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//Activity與Fragment關聯(lián)時獲取初始化回調
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("BookListFragment 所在的Activity必須實現(xiàn)Callbacks接口!");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null; //解除關聯(lián)時銷毀接口
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//發(fā)生點擊事件時 傳入數據集合對應位置的id 這樣即可獲取到詳情內容
mCallbacks.onItemSelected(FoodUtil.foodList.get(position).getfId());
}
}
3蜈垮、之后是右邊的食物詳細介紹部分耗跛,DetailFragment裕照。
public class DetailFragment extends Fragment {
private FoodUtil.FoodEntity mFoodEntity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//創(chuàng)建時如果有fid 那么就根據fid獲取對應的詳細內容
if (getArguments().containsKey("fid")) {
mFoodEntity = FoodUtil.foodMap.get(getArguments().getInt("fid"));
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_fragment_fdetail, container, false);
//如果有數據 就初始化界面數據
if (mFoodEntity != null) {
((TextView) view.findViewById(R.id.tv_title)).setText(mFoodEntity.getfTitle());
((TextView) view.findViewById(R.id.tv_detail)).setText(mFoodEntity.getfDetail());
}
return view;
}
}
4、DetailFragment要有一個布局调塌。
<?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:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="標題"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="詳情"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
5晋南、最后是Activity的布局部分,layout_activity_fragment3.xml羔砾,注意其中的fragment的name屬性換成你自己的TitleFragment的完整路徑负间。
<?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="horizontal">
<fragment
android:id="@+id/fragment_title"
android:name="com.jackaltsc.android.mydemoproject.fragment.TitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/container_food_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
6、Activity里的代碼
/**
* Created by JackalTsc on 2016/7/12.
*/
public class Fragment3Activity extends Activity implements TitleFragment.Callbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_fragment3);
}
@Override
public void onItemSelected(Integer id) {
//獲取選中時對應的fid
Bundle bundle = new Bundle();
bundle.putInt("fid", id);
//新建Fragment 并填充數據
DetailFragment detailFragment = new DetailFragment();
detailFragment.setArguments(bundle);
//替換詳情Fragment部分
getFragmentManager().beginTransaction()
.replace(R.id.container_food_detail, detailFragment)
.commit();
}
}
這樣就可以看到效果了姜凄,如果有問題政溃,歡迎私信我。
六态秧、Fragment總結
- Fragment總是作為Activity界面的組成部分董虱,F(xiàn)ragment可以調用getActivity()方法獲取它所在的Activity,Activity可以調用FragmentManager的findFragmentById()或者findFragmentByTag()來獲取Fragment申鱼。
- 在Activity運行過程中愤诱,可以通過FragmentTransaction對Fragment進行操作,如添加润讥、刪除转锈、替換等。
- 一個Activity可以同時組合多個Fragment楚殿,一個Fragment也可以被多個Activity復用撮慨。
- Fragment可以響應自己的輸入事件,并擁有自己的生命周期脆粥,但它們的生命周期被其所屬的Activity的生命