使用Fragment
- Fragment的介紹
- Fragment的生命周期
- 創建Fragment
- 在Activity中添加Fragment
- 實體——川菜菜譜
Fragment的介紹
Fragment(碎片)是一種嵌入在Activity中的UI片段,它可以用來描述Activity中的一部分布局,如果Activity界面布局中的控制元件比較多比較復雜,那么Activity管理起來就比較麻煩,我們可以使用Fragment把螢屏劃分成幾個片段,進行模塊化的管理,從而使程式更加合理和充分地利用的空間,
Fragment的生命周期
Activity生命周期中有5中狀態,分別是啟動狀態、運行狀態、暫停狀態、停止狀態和銷毀狀態,
因為Fragment是被嵌入到Activity中使用的,因此它的生命周期的狀態是直接受其所屬Activity的生命周期狀態影響,

Fragment生命周期與Activity的生命周期十分相似,Fragment生命周期比Activity多了以下幾個方法,具體如下:
- onAttach():Fragment和Activity建立關聯時呼叫
- onCreateView():Fragment創造視圖時呼叫
- onActivityCreate():Fragment相關聯的Activity已經創建完成時呼叫
- onDestroyView():Fragment關聯的視圖被移除時呼叫
- onDetach():Fragment和Activity解除關聯時呼叫
創建Fragment
public class NewsListFragment extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment,container,false);
return v;
}
}
上述代碼重寫了Fragment和onCreateView()方法,并在該方法中通過LayoutInflater的inflate()方法將布局檔案fragment.xml動態加載Fragment中
在Activity中添加Fragment
Fragment創建完成后并不能單獨使用,還需要將Fragment添加到Activity中,在Activity中添加Fragment有兩種方式:
- 在布局檔案中添加Fragment
在Activity參考的布局檔案中添加Fragment時,需要使用標簽,該標簽與其他控制元件的標簽類似,但必須指定android:name屬性,其屬性值為Fragment的全路徑名稱,在LinearLayout布局中添加NewListFragment代碼:
<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"
tools:context=".MainActivity"
>
<fragment
android:name="cn.itcast.NewListFragment"
android:id="@+id/newslist"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- 在Activity中動態加載Fragment
當Activity運行時,也可以將Fragment動態添加到Activity中,具體步驟如下:
(1)創建一個Fragment的實體物件
(2)獲取FragmentManager的實體
(3)開啟FragmentTransaction
(4)向Activity的布局容器中添加Fragment
(5)通過commit()方法提交事務
在Activity中添加Fragment的代碼如下:
public class MainActivity extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewsListFragment fragment = new NewsListFragment();//實體化Fragment物件
FragmentManager fm = getFragmentManager();//獲取FragmentManager實體
FragmentTransaction beginTransaction = fm.beginTransaction();//開啟FragmentTransaction
beginTransaction.replace(R.id.ll,fragment);//添加一個Fragment
beginTransaction.commit();//提交事務
}
}
實體——川菜菜譜

-
創建工程
創建一個名為SichuanCuisine的應用程式 -
放置界面控制元件


-
放置界面控制元件

-
創建兩個Fragment的布局檔案


-
創建川菜串列Item界面

-
創建ContentFragment

ContentFragment類繼承自Fragment,在該類中獲取界面控制元件并將菜品做法的資料顯示在控制元件上
通過setText()方法將獲取的Activity中設定的菜品做法資料資訊顯示到界面控制元件上,創建一個initView()方法,在該方法中獲取菜品做法資訊的控制元件
- 創建MenuFragment


MenuFragment類繼承自Fragment,在該類中實作顯示川菜串列的資訊,點擊串列Item,在界面右側會出現對應菜品的做法資訊
通過setOnItemClickListener()方法為串列中的Item添加點擊事件的監聽器,在該監聽器中重寫onItemClick()方法,在onItemClick()方法中首先通過getActivity()方法獲取Activity的實體物件,接著通過該物件的getFragmentManager()方法獲取FragmentManager的實體物件,最后通過findFragmentById()方法獲取到ContentFragment物件listFragment,并呼叫setText()方法設定點擊的Item對應的菜品做法資訊 - 撰寫MAinActivity


在MAinActivity中將MenuFragment與ContentFragment添加到MAinActivity界面上
通過replace()方法將ContentFragment和MenuFragment添加到對應的布局中
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275024.html
標籤:其他
上一篇:細談Activity四種啟動模式
