Android 動態建立Fragment,androidfragment

來源:互聯網
上載者:User

Android 動態建立Fragment,androidfragment

Fragment是activity的介面中的一部分或一種行為。可以把多個Fragment組合到一個activity中來建立一個多介面並且可以在多個activity中重用一個Fragment。可以把Fragment任務模組化的一段activity,它具有自己的生命週期,接收它自己的事件,並可以在activity運行時被添加或刪除。

本文地址:http://www.cnblogs.com/wuyudong/p/5893804.html,轉載請註明源地址。

Fragment不能獨立存在,它必須嵌入到activity中,而且Fragment的生命週期直接受所在的activity的影響。例如:當activity暫停時,他擁有的所有的Fragment都暫停了,當activity銷毀時,他擁有的所有Fragment都被銷毀。然而,當activity運行時(在onResume()之後,onPause()之前),可以單獨地操作每個Fragment,比如添加或刪除它們。當中執行上述針對Fragment的事務時,可以將事務添加到一個棧中,這個棧被activity管理,棧中的每一條都是一個Fragment的一次事務。有了這個棧,就可以反向執行Fragment的事務,這樣就可以在Fragment級支援“返回”鍵(向後導航)。

當向activity中添加一個Fragment時,它須置於ViewGroup控制項中,並且需定義Fragment自己的介面。可以在layout.xml布局檔案中聲明Fragment,元素為:<fragment>;也可以在代碼中建立Fragment,然後把它加入到ViewGroup控制項中。然而,Fragment不一定非要放在activity的介面中,它可以隱藏在後台為activity工作。

實戰一下

項目布局檔案代碼:

<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="horizontal"    tools:context=".MainActivity" >    <fragment        android:id="@+id/fragment1"        android:name="com.wuyudong.fragment.Fragment1"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" >    </fragment>    <fragment        android:id="@+id/fragment2"        android:name="com.wuyudong.fragment.Fragment2"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" >    </fragment></LinearLayout>

接著在layout檔案夾下建立兩個fragment.xml檔案

fragment1.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:background="#0000ff"    android:orientation="vertical" ></LinearLayout>

fragment2.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:background="#ff00"    android:orientation="vertical" ></LinearLayout>

接著在項目原始碼檔案夾下建立兩個java檔案

Fragment1.java

public class Fragment1 extends Fragment {    /**     * 當fragment被建立的時候,調用的方法,返回當前fragment顯示的內容      */    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment1, null);    }}

Fragment2.java

public class Fragment2 extends Fragment {    /**     * 當fragment被建立的時候,調用的方法,返回當前fragment顯示的內容      */    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment2, null);    }}

運行項目

接下來實現動態建立Fragment

在剛才的項目的基礎上,建立一個項目。刪除原來activity_main.xml代碼中的fragment1與fragment2程式碼片段,其他的代碼不變

接下來在MainActivity中添加下面的代碼:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 1、判斷當前手機的朝向        int width = getWindowManager().getDefaultDisplay().getWidth();        int height = getWindowManager().getDefaultDisplay().getHeight();        Fragment1 fragment1 = new Fragment1();        Fragment2 fragment2 = new Fragment2();        FragmentManager fm = getFragmentManager();        FragmentTransaction ft = fm.beginTransaction();        if (width > height) {            // 水平方向            ft.replace(android.R.id.content, fragment1);        } else {            ft.replace(android.R.id.content, fragment2);        }        ft.commit();    }}

上面的代碼實現了當手機不同朝向的時候,顯示的不同的fragment

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.