Android-Fragment新增,删除,替换

第一步:

首先创建一个布局文件,包含三个按钮,分别是新建,删除,替换,对按钮添加相应的点击事件,还包括一个LinearLayout用来显示Fragment。源码如下:

activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="增加" 
        android:onClick="addFragment"
        />

    <Button
        android:id="@+id/replace"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="替换" 
        android:onClick="replaceFragment"
        />

    <Button
        android:id="@+id/remove"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除" 
        android:onClick="removeFragment"
        />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#66FF11"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

第二步,新建两个Xml布局文件,用来作为显示的测试:

first.xml

<?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:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第一个新建的Fragment"
        />

</LinearLayout>
second.xml:

<?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:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个Fragment,用来替换第一个"
        />

</LinearLayout>

第三步,创建两个Fragment的子类,重写onCreateView使用布局填充器填充资源:

Frist.java:

package com.example.day42_change;

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

public class First extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.first, null);
	}
}
Second.java:

package com.example.day42_change;

import android.app.Fragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Second extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.second, null);
	}
}

第四步,在MainActivity.java写布局activity_main.xml的按钮点击事件,实现对Fragment的增加,删除,替换

MainActivity.java:

package com.example.day42_change;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	Second second;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public void addFragment(View view){
		//获取碎片管理器
		FragmentManager manager = getFragmentManager();
		//创建事务
		FragmentTransaction tran = manager.beginTransaction();
		//创建子类对象
		First ff = new First();
		tran.add(R.id.ll, ff);
		//添加碎片
		tran.commit();
	}
	public void replaceFragment(View view){
		FragmentManager manager = getFragmentManager();
		FragmentTransaction tran = manager.beginTransaction();
		second = new Second();
		tran.replace(R.id.ll, second);
		tran.addToBackStack(null);
		tran.commit();
	}
	public void removeFragment(View view){
		FragmentManager manager = getFragmentManager();
		FragmentTransaction tran = manager.beginTransaction();
		tran.remove(second);
		tran.commit();
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值