android Fragment FragmentActivity 选项卡 replace add hide show

android Fragment FragmentActivity 选项卡 replace add hide show

1、获取FragmentManage的方式:
getFragmentManager() // v4中,getSupportFragmentManager
2、主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),
这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。

Activity.java

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.R;

/**
 * 俩个Fragment切换 AFragment BFragment
 *
 */
public class MainActivity extends FragmentActivity implements OnClickListener {

	private static final int TYPE_201 = 0;
	private static final int TYPE_202 = 1;
	private FragmentManager fragmentManager;
	private FragmentTransaction transaction;
	private Fragment fragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bizData(TYPE_201);// 设置默认的Fragment
	}


	public void bizData(int type) {
		switch (type) {
		case TYPE_201:
			fragment = new AFragment();
			break;
		case TYPE_202:
			fragment = new BFragment();
			break;
		default:
			break;
		}
		fragmentManager = getSupportFragmentManager();
		transaction = fragmentManager.beginTransaction()
				.setCustomAnimations(android.R.anim.fade_in,
						android.R.anim.fade_out);
		transaction.(R.id.container, fragment);
		transaction.commit();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_continuing:
			bizData(TYPE_201);
			break;
		case R.id.tv_exam:
			bizData(TYPE_202);
			break;
		default:
			break;
		}
	}


}

activity_main.xml 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F39801"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_continuing"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="继续教育"
            android:textColor="#ffffff"
            android:textSize="13sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#ffffff" />

        <TextView
            android:id="@+id/tv_exam"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="考试培训"
            android:textColor="#ffffff"
            android:textSize="13sp" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

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

public class AFragment extends Fragment {

	private View view;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_a, container, false);
		return view;
	}

}

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

public class BFragment extends Fragment {

	private View view;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_b, container, false);
		return view;
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值