Android組件--片段(fragment)

來源:互聯網
上載者:User

標籤:prot   tco   cal   encoding   div   tac   upd   nbsp   mat   

1. 基本概念

一.什麼是事務:

事務是應用程式中一系列嚴密的操作,所有操作必須成功完成,否則在每個操作中所作的所有更改都會被撤消。一個事務中的一系列的操作要麼全部成功,要麼一個都不做。 事務的結束有兩種,當事務中的所以步驟全部成功執行時,事務提交。如果其中一個步驟失敗,將發生復原操作,撤消撤消之前到事務開始時的所以操作。

 

二. 什麼是fragment:

Fragment是Android3.0後引入的一個新的API,他出現的初衷是為了適應大螢幕的平板電腦,當然現在他仍然是平板APP UI設計的寵兒,而且我們普通手機開發也會加入這個Fragment,我們可以把他看成一個小型的Activity,又稱Activity片段!想想,如果一個很大的介面,我們就一個布局,寫起介面來會有多麻煩,而且如果組件多的話是管理起來也很麻煩!而使用Fragment我們可以把螢幕劃分成幾塊,然後進行分組,進行一個模組化的管理!從而可以更加方便的在運行過程中動態地更新Activity的使用者介面。另外Fragment並不能單獨使用,他需要嵌套在Activity中使用,儘管他擁有自己的生命週期,但是還是會受到宿主Activity的生命週期的影響,比如Activity被destory銷毀了,他也會跟著銷毀。

 

2. 生命週期

3. 代碼

注意:因為Fragment是Activity的一部分,所以不需要再manifest.xml中註冊。

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="sample.android_serialport_api.adapter.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按鍵1"        android:id="@+id/fbt1"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按鍵2"        android:id="@+id/fbt2"/>    <FrameLayout        android:id="@+id/mycontainer"     //在所需要放置Fragment的Activity,添加一個,類似容器        android:layout_width="match_parent"        android:layout_height="match_parent" >    </FrameLayout></LinearLayout>

fragment_blank.xml
<FrameLayout 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:background="@color/colorPrimary"    tools:context="sample.android_serialport_api.adapter.BlankFragment1">    <!-- TODO: Update blank fragment layout -->    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我是framgment一號" /></FrameLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {    Button bt1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bt1 = (Button)findViewById(R.id.fbt1);        bt1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                System.out.println("按鍵1");                gotoFragment1();            }        });    }    private void gotoFragment1(){        System.out.println("Fragment 1");        //得到Fragment的管理者        FragmentManager fm = getFragmentManager();        //開始一個事務        FragmentTransaction ft = fm.beginTransaction();        //建立我們的fragment執行個體        BlankFragment1 bf = new BlankFragment1();        ft.replace(R.id.mycontainer, bf);        ft.commit();    }}

BlankFragment1.java
public class BlankFragment1 extends Fragment {    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // 返回一個View,因為fragment沒有View        return inflater.inflate(R.layout.fragment_blank, container, false);    }}

 

 

Android組件--片段(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.