Android Fragment和FragmentActivity区别和用法

Android Fragment和FragmentActivity区别和用法

发表于 1年前
分享到
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_Second) {
            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_Second) {
            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;
}

}

最后再说一句布局:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值