Android app開發(fā)中的Fragment入門學(xué)習(xí)教程
在Android3.0上開始引入了一個新概念叫Fragment。它有自己的布局文件,可以作為組件排布,也可以相互組合去實現(xiàn)不同的布局顯示。使用Fragment可以重復(fù)利用代碼,并且可以滿足不同設(shè)備尺寸的需求。Fragment不能單獨存在,只能存在于Activity中,而一個Activity可以擁有多個Fragment。很重要的一點是,F(xiàn)ragment可以和Activity中的其它組件一起使用,無需重寫所有Activity的接口。所以使用Fragment就可以這樣來完成上例中“主界面—詳細界面”的APP需求。
在手機上是這樣顯示的:

而在平板上是這樣的:

在一個小屏幕的設(shè)備上,一個activity通常占據(jù)了整個屏幕,同時顯示各種UI視圖組件。Activity實際上就是視圖的容器。然后,當一個activity被顯示在一個大屏幕的設(shè)備上,例如平板電腦,總會顯得有些不適應(yīng)。因為屏幕太大了,activity中的所有UI組件要充滿整個屏幕,這樣一來,視圖的層次結(jié)構(gòu)就很復(fù)雜了。一個更好的辦法是使用一種“輕量級”的activity,每個“輕量級”activity包含自己的視圖,互不干擾。在運行期間,根據(jù)屏幕的方向和尺寸,一個activity可以包含一個或多個“輕量級”activity。在Android3.0以上的版本,這種“輕量級”的activity叫做Fragment.
怎么創(chuàng)建一個Fragment
現(xiàn)在我們了解了Fragment的生命周期了,接著我們就需要知道怎么創(chuàng)建一個Fragment并綁定到Activity中,第一件要做的事就是繼承android.app.Fragment來寫一個Fragment,假設(shè)我們的Fragment叫做Fragment1,創(chuàng)建和定義如下:
public class Fragment1 extends Fragment {
...
}
就像我們上面說的,F(xiàn)ragment只能存在于Activity中,所以我們必須要在某處定義它,有兩種方式:
- 直接在xml布局文件中定義;
- 在xml布局文件中定義一個占位符,然后動態(tài)地在Activity中操作Fragment;
我們定義Fragment的方式會影響它的生命周期,因為在上述第一種情況下onInflate方法會被調(diào)用,而第二種情況下它的生命周期是從onAttach方法開始的。
如果我們在XML文件中定義Fragment的話,我們需要:
<fragment android:id="@+id/f1"
class="com.survivingwithandroid.fragment.Fragment1"
android:layout_width="match_parent"
android:layout_height="20dp"/>
然而如果我們在XML中用占位符的話,需要再做一些工作。
布局框架和Fragment
如果我們在XML布局文件中定義Fragment的話,就不能自由、動態(tài)修改Fragment了,還有別的方法可以讓我們可以更靈活地操作:使用時需要在XML文件中定義:
<FrameLayout android:id="@+id/fl1"
android:layout_width="match_parent"
android:layout_height="200dp"/>
在Activity里面還需要做一點工作,因為我們必須手動初始化Fragment,然后把它“插入”到FrameLayout中。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment2 f2 = new Fragment2();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fl1, f2);
ft.commit();
}
例子
可以把Fragment想象成Activity的另外一種形式。你創(chuàng)建fragments去包含UI組件,就像創(chuàng)建activities那樣。但是,F(xiàn)ragment總是被嵌在Activity中。
下面來通過一個例子看一下流程:
1.創(chuàng)建一個名為Fragments的工程。
2.在res/layout文件夾下,新建一個叫fragment1.xml的文件。
<?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:background="#00FF00"
android:orientation="vertical" >
<TextView
android:id="@+id/lblFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
3.在res/layout文件夾下,新建一個叫fragment2.xml的文件。
<?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:background="#FFFE00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textColor="#000000"
android:textSize="25sp" />
<Button
android:id="@+id/btnGetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Get text in Fragment #1"
android:textColor="#000000" />
</LinearLayout>
4.main.xml中的代碼。
<?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/fragment1"
android:name="net.learn2develop.Fragments.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="net.learn2develop.Fragments.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
5.新建兩個類:Fragment1.java和Fragment2.java。
6.Fragment1.java中的代碼。
package net.learn2develop.Fragments;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Fragment 1", "onCreateView");
// ---Inflate the layout for this fragment---
return inflater.inflate(R.layout.fragment1, container, false);
}
}
7.Fragment2.java中的代碼。
package net.learn2develop.Fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ---Inflate the layout for this fragment---
return inflater.inflate(R.layout.fragment2, container, false);
}
}
- Android App在ViewPager中使用Fragment的實例講解
- Android中ViewPager實現(xiàn)滑動指示條及與Fragment的配合
- Android App中使用ViewPager+Fragment實現(xiàn)滑動切換效果
- Android中使用TabHost 與 Fragment 制作頁面切換效果
- 淺談Android App開發(fā)中Fragment的創(chuàng)建與生命周期
- Android應(yīng)用開發(fā)中Fragment與Activity間通信示例講解
- Android應(yīng)用開發(fā)中Fragment間通信的實現(xiàn)教程
- 淺談Android app開發(fā)中Fragment的Transaction操作
- Android的Fragment的生命周期各狀態(tài)和回調(diào)函數(shù)使用
- Android App中使用ListFragment的實例教程
相關(guān)文章
Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點解析
這篇文章主要介紹了Android應(yīng)用開發(fā)中View繪制的一些優(yōu)化點解析,包括Layout布局和硬件加速等方面,需要的朋友可以參考下2016-03-03
Android數(shù)據(jù)庫LitePal的基本用法詳解
這篇文章主要介紹了Android數(shù)據(jù)庫LitePal的基本用法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Android從0到完整項目(1)使用Android studio 創(chuàng)建項目詳解
本篇文章主要介紹了Android從0到完整項目(1)使用Android studio 創(chuàng)建項目詳解,具有一定的參考價值,有興趣的可以了解一下2017-07-07
Android權(quán)限HaloPermission詳細使用
這篇文章主要介紹了Android權(quán)限HaloPermission詳細使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

