Android Fragment和FragmentActivity區別和用法

來源:互聯網
上載者:User

標籤:

Android Fragment是Android4.0以上才有的;而FragmentActivity是為了相容4.0以下版本的Fragment使用的。

所以如果你想相容4.0以下Android版本使用Fragment的話,架構Activity需要繼承FragmentActivity,FragmentActivity這個類是在android.support.v4.app.FragmentActivity裡的。

下面介紹2種用法:

1、繼承Activity的。

(這個只針對4.0以上的Android平台使用Fragment)。

架構Activity:

package com.tandong.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 不相容4.0以下模式Fragment
 * 
 * @author tandong
 * 
 */

public class Mt_Activity extends Activity implements OnClickListener {
    private Button btn_first, btn_second;
    private Fragment Fragment_first, Fragment_Second;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        initView();

        Fragment_Second = new Fragment_Second();
        fragmentManager = this.getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second,"second_Fragment");
        fragmentTransaction.commit();
    }

    private void initView() {
        btn_first = (Button) this.findViewById(R.id.btn_first);
        btn_second = (Button) this.findViewById(R.id.btn_second);
        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btn_first:
            // 載入不同的Fragment
            if (null == Fragment_first) {
                Fragment_first = new Fragment_first();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
            fragmentTransaction.commit();
            break;
        case R.id.btn_second:
            if (null == Fragment_first) {
                Fragment_Second = new Fragment_Second();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
            fragmentTransaction.commit();
            break;
        default:
            break;
        }

    }

}

 

Fragment代碼:

package com.tandong.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_Second extends Fragment {
    private View rootView;

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

        return rootView;
    }
}

2.繼承FragmentActivity的(向下相容4.0以下版本使用Fragment,匯入的是android.support.v4包裡的內容)

架構Activity:

package com.tandong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 相容4.0以下模式Fragment
 * 
 * @author tandong
 * 
 */

public class Mt_Activity extends FragmentActivity implements OnClickListener {
    private Button btn_first, btn_second;
    private Fragment Fragment_first, Fragment_Second;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        initView();

        Fragment_first = new Fragment_first();
        fragmentManager = this.getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first,"first_Fragment");
        fragmentTransaction.commit();
    }

    private void initView() {
        btn_first = (Button) this.findViewById(R.id.btn_first);
        btn_second = (Button) this.findViewById(R.id.btn_second);
        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btn_first:
            // 載入不同的Fragment
            if (null == Fragment_first) {
                Fragment_first = new Fragment_first();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
            fragmentTransaction.commit();
            break;
        case R.id.btn_second:
            if (null == Fragment_first) {
                Fragment_Second = new Fragment_Second();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
            fragmentTransaction.commit();
            break;
        default:
            break;
        }

    }

}

Fragment代碼:

package com.tandong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_first extends Fragment {
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(container==null)
           return null;
        rootView = inflater.inflate(R.layout.fragment_first, container,false);

        return rootView;
    }
}

最後再說一句布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/top_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/top_bar_bg"
            android:text="按鈕一" />

        <Button
            android:id="@+id/btn_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/top_bar_bg"
            android:text="按鈕二" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/fragment_replace_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_bar_layout"
        android:background="#ff0000" >
    </LinearLayout>

</RelativeLayout>

布局類似這種布局即可,並不是一定非要FrameLayout

Android Fragment和FragmentActivity區別和用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.