日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Fragment详解之二——基本使用方法

發布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fragment详解之二——基本使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:依然沒有前言……文章寫的太快,生活過得太有章程,前言都不知道寫什么了……


相關文章:

1、《Fragment詳解之一——概述》
2、《Fragment詳解之二——基本使用方法》
3、《Fragment詳解之三——管理Fragment(1)》
4、《Fragment詳解之四——管理Fragment(2)》
5、《Fragment詳解之五——Fragment間參數傳遞》
6、《Fragment詳解之六——如何監聽fragment中的回退事件與怎樣保存fragment狀態》


上一篇給大家簡單說了說Fragment是用來做什么的及生命周期的問題,這篇我們就用實例來看看我們在代碼中如何使用Fragment;

在這里我們全部使用android-support-v4.jar包里Fragment,不用系統自帶的Fragment;這兩個基本一樣,但V4包中的相對功能更強大一些。

一、靜態添加Fragment

新建一個項目harvicBlog2Static,在其中添加一個布局:fragment1.xml:

[html] view plaincopy
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"????
  • ????android:layout_width="match_parent"????
  • ????android:layout_height="match_parent"????
  • ????android:background="#00ff00"?>????
  • ????
  • ????<TextView????
  • ????????android:layout_width="wrap_content"????
  • ????????android:layout_height="wrap_content"????
  • ????????android:text="This?is?fragment?1"????
  • ????????android:textColor="#000000"????
  • ????????android:textSize="25sp"?/>????
  • ????
  • </LinearLayout>????
  • 可以看到,這個布局文件非常簡單,只有一個LinearLayout,里面加入了一個TextView。我們如法炮制再新建一個fragment2.xml :
    [html] view plaincopy
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:background="#ffff00"?>??
  • ??
  • ????<TextView??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="This?is?fragment?2"??
  • ????????android:textColor="#000000"??
  • ????????android:textSize="25sp"?/>??
  • ??
  • </LinearLayout>??
  • 然后新建一個類Fragment1,這個類是繼承自Fragment的:
    [java] view plaincopy
  • import?android.os.Bundle;??
  • import?android.support.v4.app.Fragment;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.view.ViewGroup;??
  • ??
  • /**?
  • ?*?Created?by?harvic?on?2015/4/5.?
  • ?*/??
  • public?class?Fragment1?extends?Fragment?{??
  • ????@Override??
  • ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?Bundle?savedInstanceState)?{??
  • ????????return?inflater.inflate(R.layout.fragment1,?container,?false);??
  • ????}??
  • }??
  • 注意,使用的V4包中的Fragment!這里的代碼主要就是加載了我們剛剛寫好的fragment1.xml布局文件并返回。同樣的方法,我們再寫好Fragment2 :
    [java] view plaincopy
  • import?android.os.Bundle;??
  • import?android.support.v4.app.Fragment;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.view.ViewGroup;??
  • ??
  • /**?
  • ?*?Created?by?harvic?on?2015/4/5.?
  • ?*/??
  • public?class?Fragment2?extends?Fragment?{??
  • ????@Override??
  • ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?Bundle?savedInstanceState)?{??
  • ????????return?inflater.inflate(R.layout.fragment2,?container,?false);??
  • ????}??
  • }??
  • 然后打開或新建activity_main.xml作為主Activity的布局文件,在里面加入兩個Fragment的引用, 使用android:name前綴來引用具體的Fragment:
    [java] view plaincopy
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:baselineAligned="false"?>??
  • ??
  • ????<fragment??
  • ????????android:id="@+id/fragment1"??
  • ????????android:name="com.harvic.com.harvicblog2.Fragment1"??
  • ????????android:layout_width="0dip"??
  • ????????android:layout_height="match_parent"??
  • ????????android:layout_weight="1"?/>??
  • ??
  • ????<fragment??
  • ????????android:id="@+id/fragment2"??
  • ????????android:name="com.harvic.com.harvicblog2.Fragment2"??
  • ????????android:layout_width="0dip"??
  • ????????android:layout_height="match_parent"??
  • ????????android:layout_weight="1"?/>??
  • ??
  • </LinearLayout>??
  • 至于MainActivity,由于我們使用的V4包,必須將MainActivity派生自FragmentActivity,否則根本無法啟動程序!因為系統的Activity只能用來盛裝系統自帶的Fragment,而無法盛裝V4包中的Fragment,因為系統的Activity根本無法識別V4包中的Fragment,因為這根本就不是一塊的代碼!如果不使用V4包,使用系統自帶的Fragment則不必將MainActivity派生自FragmentActivity。
    [java] view plaincopy
  • import?android.os.Bundle;??
  • import?android.support.v4.app.FragmentActivity;??
  • ??
  • public?class?MainActivity?extends?FragmentActivity?{??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ????}??
  • }??
  • 效果圖如下:

    源碼在文章最底部給出

    二、動態添加Fragment

    你已經學會了如何在XML中使用Fragment,但是這僅僅是Fragment最簡單的功能而已。Fragment真正的強大之處在于可以動態地添加到Activity當中,因此這也是你必須要掌握的東西。當你學會了在程序運行時向Activity添加Fragment,程序的界面就可以定制的更加多樣化。下面我們立刻來看看,如何動態添加Fragment。

    還是在上一節代碼的基礎上修改,打開activity_main.xml,將其中代碼全部刪除,改成下面的樣子:

    [html] view plaincopy
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:orientation="vertical"??
  • ????android:baselineAligned="false"?>??
  • ??
  • ????<Button??
  • ????????android:id="@+id/btn_show_fragment1"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="顯示Fragment1"/>??
  • ??
  • ????<Button??
  • ????????android:id="@+id/btn_show_fragment2"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="顯示Fragment2"/>??
  • ??
  • ????<FrameLayout??
  • ????????android:id="@+id/fragment_container"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="match_parent"/>??
  • ??
  • </LinearLayout>??
  • 主界面上有兩個按鈕和一個FrameLayout布局。這兩個按鈕分別用來在這個FrameLayout加載Fragment1和Fragment2的實例。效果如下:
    (由于在錄GIF時,綠色會出問題,所以把fragment1的背景改成了紫色)

    其它代碼都沒有動,主要的是在MainActivity里,點擊這兩個按鈕時做的處理:

    [java] view plaincopy
  • public?class?MainActivity?extends?FragmentActivity?{??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ??
  • ????????Button?btnLoadFrag1?=?(Button)findViewById(R.id.btn_show_fragment1);??
  • ????????btnLoadFrag1.setOnClickListener(new?View.OnClickListener()?{??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????FragmentManager?manager?=?getSupportFragmentManager();??
  • ????????????????FragmentTransaction?transaction?=?manager.beginTransaction();??
  • ????????????????Fragment1?fragment1?=?new?Fragment1();??
  • ????????????????transaction.add(R.id.fragment_container,?fragment1);??
  • ????????????????transaction.commit();??
  • ????????????}??
  • ????????});??
  • ??
  • ????????Button?btnLoagFrag2?=?(Button)findViewById(R.id.btn_show_fragment2);??
  • ????????btnLoagFrag2.setOnClickListener(new?View.OnClickListener()?{??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????FragmentManager?manager?=?getSupportFragmentManager();??
  • ????????????????FragmentTransaction?transaction?=?manager.beginTransaction();??
  • ????????????????Fragment2?fragment2?=?new?Fragment2();??
  • ????????????????transaction.add(R.id.fragment_container,?fragment2);??
  • ????????????????transaction.commit();??
  • ????????????}??
  • ????????});??
  • ????}??
  • }??
  • 看上面的代碼很容易明白,在點擊按鈕時都做了類似的操作:
    [java] view plaincopy
  • FragmentManager?manager?=?getSupportFragmentManager();??
  • FragmentTransaction?transaction?=?manager.beginTransaction();??
  • Fragment1?fragment1?=?new?Fragment1();??
  • transaction.add(R.id.fragment_container,?fragment1);??
  • transaction.commit();??
  • 動態添加Fragment主要分為4步:
    • 1.獲取到FragmentManager,在V4包中通過getSupportFragmentManager,在系統中原生的Fragment是通過getFragmentManager獲得的。
    • 2.開啟一個事務,通過調用beginTransaction方法開啟。
    • 3.向容器內加入Fragment,一般使用add或者replace方法實現,需要傳入容器的id和Fragment的實例。
    • 4.提交事務,調用commit方法提交。?
    這部分有關fragment的操作看不大懂也沒關系,下節我們會具體講有關Fragment的管理!

    總結

    以上是生活随笔為你收集整理的Fragment详解之二——基本使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。