Android應(yīng)用開(kāi)發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例
1、Fragment的靜態(tài)使用
Fragment是作為Activity的UI的一部分,它內(nèi)嵌在Activity中,多個(gè)Fragment可以把一個(gè)Activity分成多個(gè)部分,這在大屏幕手機(jī)或者平板電腦中會(huì)比較多的用到,這樣就不用使用多個(gè)Activity來(lái)切換這么麻煩了。當(dāng)然Fragment也可以不顯示,只在后臺(tái)處理一些數(shù)據(jù),這篇文章中就暫時(shí)不談到這個(gè)。以下來(lái)看怎么靜態(tài)地在Activity的布局文件中添加Fragment.
自定義的Fragment通常要繼承Fragment這個(gè)類,也有一些特殊的是繼承ListFragment,DialogFragment等。繼承Fragment類通常要實(shí)現(xiàn)三個(gè)方法:onCreate(), onCreateView(), onPause();
我在Activity中定義了兩個(gè)Fragment,一個(gè)是放在左邊的LeftFragment,一個(gè)是放在右邊的RightFragment.以下是代碼:首先我們要實(shí)現(xiàn)自己的Fragment類
LeftFragment類:
public class LeftFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
System.out.println("LeftFragment onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
System.out.println("LeftFragment onCreateView");
// 第一個(gè)參數(shù)是這個(gè)Fragment將要顯示的界面布局,第二個(gè)參數(shù)是這個(gè)Fragment所屬的Activity,第三個(gè)參數(shù)是決定此fragment是否附屬于Activity
return inflater.inflate(R.layout.leftfragment, container, true);
}
@Override
public void onResume()
{
super.onResume();
System.out.println("LeftFragment onResume");
}
@Override
public void onPause()
{
super.onPause();
System.out.println("LeftFragment onPause");
}
@Override
public void onStop()
{
super.onStop();
System.out.println("LeftFragment onStop");
}
}
這里實(shí)現(xiàn)了幾種回調(diào)函數(shù),主要是為了看清Activity和Fragment生命周期之間的關(guān)系.其中onCreateView()方法是將本Fragment對(duì)應(yīng)的布局返回給Activity的布局,讓Activity進(jìn)行加載. inflater.inflate(R.layout.leftfragment, container, true)方法中的第一個(gè)參數(shù)R.layout.leftfragment是這個(gè)Fragment對(duì)應(yīng)的布局文件ID, 第二個(gè)參數(shù)container是要插入的目標(biāo)Activity, 第三個(gè)參數(shù)是決定這個(gè)Fragment是否依附于這個(gè)container.
LeftFragment對(duì)應(yīng)的布局文件:
<?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:background="@android:color/holo_orange_dark"
android:orientation="vertical" >
<Button
android:id="@+id/previous_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/previous_button" />
<Button
android:id="@+id/next_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/next_button" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_button" />
</LinearLayout>
RightFragment類:和LeftFragment類似
public class RightFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
System.out.println("RightFragment onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
System.out.println("RightFragment onCreateView");
return inflater.inflate(R.layout.rightfragment, container, true);
}
@Override
public void onResume()
{
super.onResume();
System.out.println("RightFragment onResume");
}
@Override
public void onPause()
{
super.onPause();
System.out.println("RightFragment onPause");
}
@Override
public void onStop()
{
super.onStop();
System.out.println("RightFragment onStop");
}
}
RightFragment對(duì)應(yīng)的布局文件:
<?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/show_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/holo_blue_dark"
android:text="@string/show_message" />
</LinearLayout>
最后是Activity的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/left_fragment"
android:name="com.sunflower.LeftFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3" />
<fragment
android:id="@+id/right_fragment"
android:name="com.sunflower.RightFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
在Activity中的布局文件中加入Fragment標(biāo)簽,其中android:name屬性對(duì)應(yīng)的就是自定義Fragment類的全名,系統(tǒng)會(huì)根據(jù)這個(gè)調(diào)用指定的Fragment的onCreateView()方法來(lái)得到這個(gè)Fragment的布局,然后加入Activity中. onCreateView()方法中的Container參數(shù)就是這時(shí)候傳遞過(guò)去的。
看看顯示結(jié)果:

打開(kāi)程序時(shí)生命周期顯示:

按返回鍵時(shí)生命周期顯示:

2、動(dòng)態(tài)地使用Fragment
上面已經(jīng)演示了最簡(jiǎn)單的使用Fragment的方式,下面分享一下如何動(dòng)態(tài)的添加、更新、以及刪除Fragment。
首先是,MainActivity的布局文件activity_main.xml,該文件布局文件上面的頂部是一個(gè)TitleFragment,是一個(gè)靜態(tài)聲明的Fragment。
中間也是一個(gè)Fragment,但是這個(gè)Fragment是動(dòng)態(tài)使用的。
最下面是四個(gè)按鈕。用include標(biāo)簽包含外部的布局文件進(jìn)來(lái)的。
<RelativeLayout 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" >
<fragment
android:id="@+id/id_fragment_title"
android:name="com.example.dynamicfragment.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp" />
<include
android:id="@+id/id_ly_bottombar"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/bottombar" />
<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title" />
</RelativeLayout>
然后是,MainActivity.java文件。也是我們這個(gè)demo當(dāng)中最重要的代碼文件,首先是將上面的布局文件通過(guò)setContentView()加載進(jìn)來(lái).然后是通過(guò)setDefaultFragment();將默認(rèn)的ContentFragment動(dòng)態(tài)的加載進(jìn)來(lái)。接下來(lái)就是通過(guò)我們?cè)谧钕旅娣乐沟乃膫€(gè)按鈕可以隨意的動(dòng)態(tài)切換Fragment。這也是為什么Fragment會(huì)有如此火的原因吧~~~^^
public class MainActivity extends ActionBarActivity implements OnClickListener {
private ImageButton mTabWeixin;
private ImageButton mTabFriend;
private ImageButton mTabDiscover;
private ImageButton mTabMe;
private ContentFragment mWeiXinFragment;
private FriendFragment mFriendFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
}
public void initView() {
// 初始化控件和聲明事件
mTabWeixin = (ImageButton) findViewById(R.id.weixin);
mTabFriend = (ImageButton) findViewById(R.id.friend);
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
// 設(shè)置默認(rèn)的Fragment
setDefaultFragment();
}
@SuppressLint("NewApi")
private void setDefaultFragment() {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
mWeiXinFragment = new ContentFragment();
transaction.replace(R.id.id_content, mWeiXinFragment);
transaction.commit();
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
// 開(kāi)啟Fragment事務(wù)
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.weixin:
if (mWeiXinFragment == null) {
mWeiXinFragment = new ContentFragment();
}
// 使用當(dāng)前Fragment的布局替代id_content的控件
transaction.replace(R.id.id_content, mWeiXinFragment);
break;
case R.id.friend:
if (mFriendFragment == null) {
mFriendFragment = new FriendFragment();
}
transaction.replace(R.id.id_content, mFriendFragment);
break;
}
// transaction.addToBackStack();
// 事務(wù)提交
transaction.commit();
}
}
從上面的代碼,我們可以看出,我們可以使用FragmentManager對(duì)Fragment進(jìn)行動(dòng)態(tài)的加載,這里使用的replace方法~~~下一節(jié)我們會(huì)詳細(xì)的介紹FragmentManager的常用API。。。。^^
注:如果使用android3.0一下的版本,需要引入v4的包,然后Activity繼承FragmentActivity,然后通過(guò)getSupportFragmentManager()獲得FragmentManager對(duì)象,不過(guò)還是建議把Menifest文件的uses-sdk的minSdkVersion和targetSdkVersion都改為11以上,這樣就不必引入v4的包了。
代碼的中間有倆個(gè)動(dòng)態(tài)加載進(jìn)來(lái)的Fragment,這個(gè)和靜態(tài)使用Fragment的聲明方式是一樣的,寫一個(gè)繼承Fragment的類,然后設(shè)置相應(yīng)的布局,由于時(shí)間的關(guān)系,我這里只寫了倆個(gè)Fragment,現(xiàn)在把這倆個(gè)的代碼頁(yè)貼出來(lái):
第一個(gè)Fragment和他相應(yīng)的布局文件:
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_content, container, false);
}
}
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="weixin"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
第二個(gè)Fragment和他相應(yīng)的布局文件:
public class FriendFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_friend, container, false);
}
}
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="friend"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
好了,現(xiàn)在基本的代碼都有了,我們把demo的運(yùn)行圖貼出來(lái)給大家分享一下(注:時(shí)間原因,沒(méi)注意布局以及圖片的美化,只是功能的實(shí)現(xiàn)),這是分別點(diǎn)擊下面第一個(gè)和第二個(gè)按鈕的效果圖,從而實(shí)現(xiàn)了中間用一個(gè)Fragment動(dòng)態(tài)的加載這倆個(gè)Fragment的顯示。


ps:為了代碼的簡(jiǎn)潔,就不添加按鈕的點(diǎn)擊變化什么的了,主要講解功能了~~~
相關(guān)文章
Android實(shí)現(xiàn)圖片瀏覽并改變透明度
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片瀏覽并改變透明度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android實(shí)現(xiàn)快遞單號(hào)查詢快遞狀態(tài)信息
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)快遞單號(hào)查詢快遞狀態(tài)信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android開(kāi)發(fā)環(huán)境搭建過(guò)程圖文詳解
這篇文章主要介紹了Android開(kāi)發(fā)環(huán)境搭建過(guò)程圖文詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android畫圖實(shí)現(xiàn)MPAndroidchart折線圖示例詳解
這篇文章主要為大家介紹了Android畫圖實(shí)現(xiàn)MPAndroidchart折線圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Flutter快速制作一個(gè)水印組件實(shí)例詳解
這篇文章主要為大家介紹了Flutter快速制作一個(gè)水印組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能
這篇文章主要為大家詳細(xì)介紹了Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05
Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

