1. 程式人生 > >理解學習 UI fragment與fragment管理器

理解學習 UI fragment與fragment管理器

先導部分

       在開發有些應用程式的時候,平板裝置和大尺寸手機螢幕較大,顯示的功能和正常手機顯示部分不相同,不過希望都能夠有較好的視覺效果,因而介面的設定不同,需要有靈活多變的UI設計。但是activity自身並不能具有這樣的靈活性,fragment就這樣被用來管理應用UI,繞開Android系統activity使用規則的限制(也就是UI fragment)。

 定義 UI fragment

       管理使用者介面的fragment又被稱為UI fragment,它自己有產生於佈局檔案的檢視,也包含了使用者可以互動的視覺化UI元素。而fragment是一種控制器物件,activity可委派他執行任務,這些任務通常就是管理使用者介面,收管的使用者介面可以是一整屏或是一整屏的一部分。

fragement切換

管理fragment生命週期

如下圖所示,fragment生命週期與activity生命週期類似,它具有停止、暫停以及執行狀態,也擁有可以覆蓋的方法,用來在關鍵節點完成一些任務。而fragment生命週期與activity生命週期的一個關鍵區別在於,fragment生命週期由託管activity而不是作業系統呼叫,作業系統不關心activity用來管理檢視的fragment。fragment的使用是activity內部的事。

建立 UI fragment

步驟1.定義使用者佈局檔案

步驟2.建立fragment類並設定器檢視為定義的佈局

步驟3.編寫程式碼以例項化元件

初始化工作

<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="30dp"
        android:layout_gravity="center_vertical"
        android:text="這裡是MainActivity"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonShowFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顯示FragmentNow"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/ButtonShowFragmentTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顯示FragmentTwo"
            android:textAllCaps="false" />
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonAmendFragmentTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改FragmentTwo"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/ButtonReplaceFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="替換" />

        <Button
            android:id="@+id/ButtonDeleteFragmentNow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刪除FragmentNow"
            android:textAllCaps="false" />
    </LinearLayout>

    <TextView
        android:id="@+id/TextView"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:text="這就是用來被Fragment修改的"
        android:textAllCaps="false"
        android:textSize="18sp" />

    <FrameLayout
        android:id="@+id/FrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D02090" />

</LinearLayout>

定義兩個fragment佈局檔案

fragment_now

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#9aff9a">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ButtonFragmentNow"
        android:text="修改Activity"
        android:textAllCaps="false"
        android:layout_gravity="center"/>

</LinearLayout>

fragment_two

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d1eeee"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TextVIewFragmentTwo"
        android:text="註定被修改"
        android:layout_gravity="center"
        android:textSize="30sp"/>

</LinearLayout>
定義兩個Fragment類
public class FragmentTwo extends Fragment {
    private View mView;
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_two, null);
        return mView;
    }

    public void Amend(){
        mTextView = (TextView) mView.findViewById(R.id.TextVIewFragmentTwo);
        mTextView.setText("這是FragmentTwo中已經被修改了的值");
    }
}
public class FragmentNow extends Fragment {
    private View mView;
    private Button mButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_now, null);
        mButton = (Button) mView.findViewById(R.id.ButtonFragmentNow);

        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.Amend();
            }
        });
    }
}

寫入MainActivity

    private void init() {
        mFragmentNow = new FragmentNow();
        mFragmentTwo = new FragmentTwo();
        mButtonShowFragmentNow = (Button) findViewById(R.id.ButtonShowFragmentNow);
        mButtonShowFragmentTwo = (Button) findViewById(R.id.ButtonShowFragmentTwo);
        mButtonAmendFragmentTwo = (Button) findViewById(R.id.ButtonAmendFragmentTwo);
        mButtonReplaceFragmentNow = (Button) findViewById(R.id.ButtonReplaceFragmentNow);
        mButtonDeleteFragmentNow = (Button) findViewById(R.id.ButtonDeleteFragmentNow);
        mBoolean = true;
    }
 mButtonAmendFragmentTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFragmentTwo.Amend();
            }
        });
        mButtonDeleteFragmentNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .remove(mFragmentNow).commit();
            }
        });
    public void Amend() {
        TextView textView = (TextView) findViewById(R.id.TextView);
        textView.setText("你看吧,Activity中的TextView已經被修改了。");
    }
對於fragment,需要堅持AUF(Always Use Fragments)原則,即“總是使用fragment”。

參考自https://blog.csdn.net/cc_xz/article/details/61618922



相關推薦

理解學習 UI fragmentfragment管理

先導部分       在開發有些應用程式的時候,平板裝置和大尺寸手機螢幕較大,顯示的功能和正常手機顯示部分不相同,不過希望都能夠有較好的視覺效果,因而介面的設定不同,需要有靈活多變的UI設計。但是activity自身並不能具有這樣的靈活性,fragment就這樣被用來管理應用

如何正確理解關鍵字"with"上下文管理(轉載)

name self 限制 ioerror mysql ops tput 可能 簡化   如果你有閱讀源碼的習慣,可能會看到一些優秀的代碼經常出現帶有 “with” 關鍵字的語句,它通常用在什麽場景呢?今天就來說說 with 和 上下文管理器。   

如何正確理解關鍵字"with"上下文管理

如果你有閱讀原始碼的習慣,可能會看到一些優秀的程式碼經常出現帶有 “with” 關鍵字的語句,它通常用在什麼場景呢?今天就來說說 with 和 上下文管理器。 對於系統資源如檔案、資料庫連線、socket 而言,應用程式開啟這些資源並執行完業務邏輯之後,必須做的一件事就是要關閉(斷開)該資源

學習筆記- GC記憶體管理

深入GC與記憶體管理 託管堆中存放引用型別物件,因此GC的記憶體管理的目標主要都是引用型別物件,本文中涉及的物件如無明確說明都指的是引用型別物件。  物件建立及生命週期 一個物件的生命週期簡單概括就是:建立>使用>釋放,在.NET中一個物件的生命週期: n

activityfragment以及fragmentfragment之間的通訊

activity向其引用的fragment傳值 第一步 : Activity中建立Fragment物件 , 呼叫setArguments(bundle) 方法儲存值 第二步 : Fragment

效能測試六:jmeter進階之Cookieheader管理

    一、http cookie管理器 可以在瀏覽器中抓取到cookie資訊,然後通過http cookie管理器為http請求新增cookie資訊 新增cookie管理器後,Jmeter可以自動處理cookie 登入頁面: http://localhost:8080/Perf

Tanks(二)—— 攝像機遊戲管理指令碼

Camera - CameraControl 沒看明白啊,有空再看 using UnityEngine; namespace Complete { public class CameraControl : MonoBehaviour {

with“上下文管理

with與“上下文管理器” 如果你有閱讀原始碼的習慣,可能會看到一些優秀的程式碼經常出現帶有 “with” 關鍵字的語句,它通常用在什麼場景呢?今 對於系統資源如檔案、資料庫連線、socket 而言,應用程式開啟這些資源並執行完業務邏輯之後,必須做的一件事就是要關閉(斷開)該資源。 比如

with上下文管理

imp __init__ 解決 def text name urn 重寫 style with主要為了解決資源釋放問題,可以簡化代碼,下面是兩種應用with的例子: 1、通過重寫__enter__和__exit__方法實現: 1 # coding:utf-8 2

Android面試篇之ActivityFragmentFragmentFragment之間的通訊

Activity與Fragment Activity向Fragment通訊 ① 拿到Fragment的引用,直接呼叫其public方法。 ② 如果Activity中未儲存任何Fragment的引用,

Reporting Services 中 Web服務URL 報表管理URL

Reporting Services 配置管理器中,可以看到有兩個URL: 含義如下: Web服務URL: 用於訪問報表伺服器的URL,如http://localhost/ReportServer,比如在report builder 中連線和建立報表時,用的就是這個URL

Linux 基礎學習 Linux檔案目錄管理

Linux distribution : ubuntu 16.04 LTS 參考書籍: 《鳥哥的Linux私房菜 基礎學習篇(第三版)》 今天白天除錯了一天的Wifi模組透傳,有點小累,不過還是要繼續學習Linux基礎。 1.本章中主要使用的常見的處理目錄的命令:  

VS專案屬性頁 Property page 屬性管理 Property manager 中配置關係

在新建工程時,總是會有配置路徑、連結庫、生成目標等問題,這可以通過修改專案屬性配置來達到目的。 VS 中在一個專案上我們可以通過專案的屬性頁(Project Property page)來修改專案相關屬性,也可以通過修改屬性管理器(Property Manager)中的相關

android之fragmentfragment、activityactivity、fragmentactivity之間的通訊

Broadcast廣播接受者可以實現所有通訊;-----------activity與activity之間的通訊--------- **********傳給上一個activity********* //右側+按鈕的點選事件 public void addClick(V

Linux學習--檔案許可權目錄管理

檔案許可權:-rwxrwxrwx,‘-’這一位代表的是檔案型別,這裡邊‘-’代表的是檔案,後邊的‘rwxrwxrwx’代表的是許可權,‘r’代表讀許可權,‘w’代表寫許可權,‘x’可執行許可權注意:1.之所以有三個rwx,這是分表代表三類人使用,第一個是自己的許可權,第二個是

Linux學習:檔案目錄管理

Linux 檔案與目錄管理Linux的目錄結構為樹狀結構,最頂級的目錄為根目錄 /。其他目錄通過掛載可以將它們新增到樹中,通過解除掛載可以移除它們。什麼是絕對路徑與相對路徑?絕對路徑:路徑的寫法,由根目錄 / 寫起,例如: /usr/share/doc 這個目錄。相對路徑:路

【Linux 學習】檔案目錄管理

檔案與目錄管理 主要介紹和總結Linux系統中的檔案與目錄的基本管理。 1、檢視當前目錄下的檔案和子目錄 ls -aAdfFhilnrRSt 目錄名稱 選項: -a 列出全部檔案與目錄(包含隱藏檔案和當前目錄.以及上層目錄..) -A 除了當前目錄和上層目錄外的其他所有

Fragment學習之使用介面回撥的方式實現FragmentActivity通訊

Fragment與Fragment之間可以進行資訊傳遞,同樣,Fragment與Activity也可以進行資訊的傳遞。 下面是一個演示在Activity中獲取來自Fragment的資訊,使用介面回撥的方法在Activity中接收資訊 MainActivity.java:

Android程式設計學習筆記 之 FragmentActivity的資料傳遞

傳遞方向的不同: ①Activity----->Fragment: 在Activity中建立Bundle資料包,並呼叫Fragment的setArguments(Bundle bundle)方法 ②Fragment----->Activity: 需要在Frag

最新ActivityFragment完全理解

Android是通過FragmentManager來管理Fragment,每次對Fragment進行新增和移除時需要開啟事務,通過事務處理這些相應的操作,然後commit事務。 1、新增、移除Fragment的幾種方式 在對Fragment進行管理前,需要開啟一個事務,如下: