a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android Fragment 基本了解(圖文介紹)

2020-04-11 12:35:28
字體:
供稿:網(wǎng)友
Fragment
Android是在Android 3.0 (API level 11)開始引入Fragment的。
可以把Fragment想成Activity中的模塊,這個(gè)模塊有自己的布局,有自己的生命周期,單獨(dú)處理自己的輸入,在Activity運(yùn)行的時(shí)候可以加載或者移除Fragment模塊。
可以把Fragment設(shè)計(jì)成可以在多個(gè)Activity中復(fù)用的模塊。
當(dāng)開發(fā)的應(yīng)用程序同時(shí)適用于平板電腦和手機(jī)時(shí),可以利用Fragment實(shí)現(xiàn)靈活的布局,改善用戶體驗(yàn)。
如圖:
 
Fragment的生命周期
因?yàn)镕ragment必須嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相關(guān)的。
如果Activity是暫停狀態(tài),其中所有的Fragment都是暫停狀態(tài);如果Activity是stopped狀態(tài),這個(gè)Activity中所有的Fragment都不能被啟動(dòng);如果Activity被銷毀,那么它其中的所有Fragment都會(huì)被銷毀。
但是,當(dāng)Activity在活動(dòng)狀態(tài),可以獨(dú)立控制Fragment的狀態(tài),比如加上或者移除Fragment。
當(dāng)這樣進(jìn)行fragment transaction(轉(zhuǎn)換)的時(shí)候,可以把fragment放入Activity的back stack中,這樣用戶就可以進(jìn)行返回操作。
 
Fragment的使用相關(guān)
使用Fragment時(shí),需要繼承Fragment或者Fragment的子類(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),所以Fragment的代碼看起來和Activity的類似。
使用Support Library
Support Library是一個(gè)提供了API庫函數(shù)的JAR文件,這樣就可以在舊版本的Android上使用一些新版本的APIs。
比如android-support-v4.jar.它的完整路徑是:
<sdk>/extras/android/support/v4/android-support-v4.jar.
它就提供了Fragment的APIs,使得在Android 1.6 (API level 4)以上的系統(tǒng)都可以使用Fragment。
為了確定沒有在舊版本系統(tǒng)上使用新版本的APIs,需要如下導(dǎo)入語句:
復(fù)制代碼 代碼如下:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

同時(shí)應(yīng)該將上述的包拷入libs項(xiàng)目下的libs文件夾,然后在項(xiàng)目的Properties中添加:右鍵單擊項(xiàng)目,選Properties,左邊選Java Build Path,然后Add External JARs…,添加android-support-v4.jar.
 
當(dāng)創(chuàng)建包含F(xiàn)ragment的Activity時(shí),如果用的是Support Library,那么繼承的就應(yīng)該是FragmentActivity而不是Activity。
必須實(shí)現(xiàn)的三個(gè)回調(diào)函數(shù)
onCreate()
系統(tǒng)在創(chuàng)建Fragment的時(shí)候調(diào)用這個(gè)方法,這里應(yīng)該初始化相關(guān)的組件,一些即便是被暫停或者被停止時(shí)依然需要保留的東西。
onCreateView()
當(dāng)?shù)谝淮卫L制Fragment的UI時(shí)系統(tǒng)調(diào)用這個(gè)方法,必須返回一個(gè)View,如果Fragment不提供UI也可以返回null。
注意,如果繼承自ListFragment,onCreateView()默認(rèn)的實(shí)現(xiàn)會(huì)返回一個(gè)ListView,所以不用自己實(shí)現(xiàn)。
onPause()
當(dāng)用戶離開Fragment時(shí)第一個(gè)調(diào)用這個(gè)方法,需要提交一些變化,因?yàn)橛脩艉芸赡懿辉俜祷貋怼?
實(shí)現(xiàn)Fragment的UI
提供Fragment的UI,必須實(shí)現(xiàn)onCreateView()方法。
假設(shè)Fragment的布局設(shè)置寫在example_fragment.xml資源文件中,那么onCreateView()方法可以如下寫:
復(fù)制代碼 代碼如下:

public static class ExampleFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}

onCreateView()中container參數(shù)代表該Fragment在Activity中的父控件;savedInstanceState提供了上一個(gè)實(shí)例的數(shù)據(jù)。
inflate()方法的三個(gè)參數(shù)
第一個(gè)是resource ID,指明了當(dāng)前的Fragment對(duì)應(yīng)的資源文件;
第二個(gè)參數(shù)是父容器控件;
第三個(gè)布爾值參數(shù)表明是否連接該布局和其父容器控件,在這里的情況設(shè)置為false,因?yàn)橄到y(tǒng)已經(jīng)插入了這個(gè)布局到父控件,設(shè)置為true將會(huì)產(chǎn)生多余的一個(gè)View Group。
把Fragment加入Activity
當(dāng)Fragment被加入Activity中時(shí),它會(huì)處在對(duì)應(yīng)的View Group中。
Fragment有兩種加載方式:一種是在Activity的layout中使用標(biāo)簽<fragment>聲明;另一種方法是在代碼中把它加入到一個(gè)指定的ViewGroup中。
另外,F(xiàn)ragment它可以并不是Activity布局中的任何一部分,它可以是一個(gè)不可見的部分。這部分內(nèi)容先略過。
加載方式1:通過Activity的布局文件將Fragment加入Activity
在Activity的布局文件中,將Fragment作為一個(gè)子標(biāo)簽加入即可。
如:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

其中android:name屬性填上你自己創(chuàng)建的fragment的完整類名。
當(dāng)系統(tǒng)創(chuàng)建這個(gè)Activity的布局文件時(shí),系統(tǒng)會(huì)實(shí)例化每一個(gè)fragment,并且調(diào)用它們的onCreateView()方法,來獲得相應(yīng)fragment的布局,并將返回值插入fragment標(biāo)簽所在的地方。
有三種方法為Fragment提供ID
android:id屬性:唯一的id
android:tag屬性:唯一的字符串
如果上面兩個(gè)都沒提供,系統(tǒng)使用容器view的ID。
加載方式2:通過編程的方式將Fragment加入到一個(gè)ViewGroup中
當(dāng)Activity處于Running狀態(tài)下的時(shí)候,可以在Activity的布局中動(dòng)態(tài)地加入Fragment,只需要指定加入這個(gè)Fragment的父View Group即可。
首先,需要一個(gè)FragmentTransaction實(shí)例: 
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();(注,如果import android.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

之后,用add()方法加上Fragment的對(duì)象:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
其中第一個(gè)參數(shù)是這個(gè)fragment的容器,即父控件組。
最后需要調(diào)用commit()方法使得FragmentTransaction實(shí)例的改變生效。
實(shí)例
練習(xí)的例子:
寫一個(gè)類繼承自Fragment類,并且寫好其布局文件(本例中是兩個(gè)TextView),在Fragment類的onCreateView()方法中加入該布局。
之后用兩種方法在Activity中加入這個(gè)fragment:
第一種是在Activity的布局文件中加入<fragment>標(biāo)簽;
第二種是在Activity的代碼中使用FragmentTransaction的add()方法加入fragment。
貼出代碼:
自己定義的fragment類: 
復(fù)制代碼 代碼如下:

ExampleFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExampleFragment extends Fragment
{
//三個(gè)一般必須重載的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("ExampleFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("ExampleFragment--onCreateView");
return inflater.inflate(R.layout.example_fragment_layout, container, false);
}
@Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("ExampleFragment--onPause");
}
@Override
public void onResume()
{
// TODO Auto-generated method stub
super.onResume();
System.out.println("ExampleFragment--onResume");
}
@Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
System.out.println("ExampleFragment--onStop");
}
}

fragment的布局文件
復(fù)制代碼 代碼如下:

example_fragment_layout.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2"
/>
</LinearLayout>

主Activity
復(fù)制代碼 代碼如下:

LearnFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class LearnFragment extends FragmentActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn_fragment);
//在程序中加入Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.linear, fragment);
fragmentTransaction.commit();
}
}

Activity的布局文件
復(fù)制代碼 代碼如下:

activity_learn_fragment.xml
<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"
android:orientation="vertical"
>
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"
/>
<fragment
android:name="com.example.learningfragment.ExampleFragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3"
/>
</LinearLayout>
</LinearLayout>

運(yùn)行結(jié)果如下:
可以看到第二種方式加入fragment的時(shí)候,指定了父容器(一個(gè)線性布局)的id,其中已經(jīng)有一個(gè)Button 3,所以fragment加在其后。
參考資源
Fragment類文檔:
http://developer.android.com/reference/android/app/Fragment.html
Training:Building a Dynamic UI with Fragments
http://developer.android.com/training/basics/fragments/index.html
Fragments Develop Guide:
http://developer.android.com/guide/components/fragments.html
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久久久一区二区三区 | 欧美色综合天天久久综合精品 | 好姑娘影视在线观看高清 | 精品国产一区二区三区久久久蜜月 | 国产传媒日韩欧美 | 五月激情综合 | 国产美女久久 | 国产成人精品一区二区三区四区 | 久久三区| 好看的一级毛片 | 亚洲欧美另类国产 | 亚洲精品久久一区二区三区 | 黄色片在线免费观看 | 欧美色综合一区二区三区 | 成人一级电影在线观看 | 欧美九九| 国产高清成人久久 | 99久久精品国产一区二区三区 | 色伊人 | 一区二区视频 | 成人欧美一区二区三区黑人麻豆 | 一个人看的www日本高清视频 | 久久久久久国产精品 | 2018狠狠干| 夜夜夜操操操 | 久久国产视频一区二区 | 国产日韩欧美一区二区在线观看 | 亚洲在线播放 | 国产精品一区二区在线 | 99re在线观看 | 欧美日韩一区二区三区四区 | 婷婷久久综合 | 久久午夜影视 | 国产成人毛片 | 一本色道久久综合亚洲精品不 | 亚洲协和影视 | 欧美日韩国产综合视频 | 国产精品美女久久久久久久久久久 | 亚洲一区二区三区四区五区午夜 | 亚洲不卡在线观看 | 五月婷婷激情 |