关闭

Fragment学习进阶(二)----->动态显示和移除

标签: FragmentActivityFragmentManager
130人阅读 评论(0) 收藏 举报
分类:


MainActivity

package com.pry.fragmentdemo2;

import com.example.fragmentdemo2.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

/**
 * 主页面 继承 FragmentActivity
 * 
 * @author Administrator
 */
public class MainActivity extends FragmentActivity {

	MyFragment2 fragment2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 创建fragment实例
		MyFragment fragment = new MyFragment();

		// 得到FragmentManager
		FragmentManager fm = getSupportFragmentManager();

		// 开始事务
		FragmentTransaction beginTransaction = fm.beginTransaction();

		// 添加操作,把Fragment添加到MainActivity里面
		beginTransaction.add(R.id.ll_main, fragment);

		// 提交操作
		beginTransaction.commit();
	}

	/**
	 * 显示Fragment2界面
	 * 
	 * @param v
	 */

	public void showFragment2(View v) {
		// 初始化Fragment2
		fragment2 = new MyFragment2();

		// 得到FragmentManager
		FragmentManager fm = getSupportFragmentManager();

		// 开启事务,并且替换 fragment2
		FragmentTransaction transaction = fm.beginTransaction().replace(
				R.id.ll_main, fragment2);

		// 将此次事物添加到回退栈中, 点击back, 可以返回到事务提交前的状态
		transaction.addToBackStack(null);

		// 提交事务
		transaction.commit();
	}

	/**
	 * 移除Fragment2界面
	 * 
	 * @param v
	 */
	public void deleteFragment2(View v) {

		if(fragment2 != null){
			
			// 得到FragmentManager
			FragmentManager fm = getSupportFragmentManager();
			
			// 开启事务,并且移除 fragment2(方法链调用)
			FragmentTransaction transaction = fm.beginTransaction().remove(fragment2);
			
			// 将此次事物添加到回退栈中, 点击back, 可以返回到事务提交前的状态
			transaction.addToBackStack(null);
			
			// 提交事务
			transaction.commit();
			
			//将Fragment2对象置为空
			fragment2 = null;
		}
	}
}


activity_main


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

    <!-- 线性布局 两个按钮 -->
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="showFragment2"
            android:text="显示fragment2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="deleteFragment2"
            android:text="移除fragment2" />
    </LinearLayout>

    <!-- 线性布局 用来替换fragment-->
    <LinearLayout
        android:id="@+id/ll_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

MyFragment


package com.pry.fragmentdemo2;

import com.example.fragmentdemo2.R;

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

/**
 * 自定义MyFragment
 * @author Administrator
 *
 */
public class MyFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return View.inflate(getActivity(), R.layout.fragment_article, null);
	}
}

fragment_article


<?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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是MyFragment界面" />

</LinearLayout>


MyFragment2


package com.pry.fragmentdemo2;

import com.example.fragmentdemo2.R;

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

/**
 * 自定义MyFragment
 * @author Administrator
 *
 */
public class MyFragment2 extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return View.inflate(getActivity(), R.layout.fragment_article2, null);
	}
}


fragment_article2


<?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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是MyFragment2界面" />

</LinearLayout>


效果图





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13490次
    • 积分:489
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条