1.activity中动态添加删除替换fragment,实例:一个页面,上下两个按钮,分别用以替换fragment和activity与fragment2通信;activity中央有一个textview用一下显示fragment2传递过来的数据,fragment2中心有一个TextView用以显示activity的传至;
activity的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:text="点击切换"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="50dp" />
<FrameLayout
android:orientation="horizontal"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="点击传值"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="50dp" />
<TextView
android:textColor="#e00000"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="展示frament传输过来的数据"/>
</RelativeLayout>
activity的代码处理:
package com.example.shibo.testfragment0321;
import android.app.Activity;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
TextView text;
Button button;
Button button1;
//需要替换的fragment
Fragment fragment1;
Fragment fragment2;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
fragment1=new Fragment1();
transaction.add(R.id.fragment,fragment1);
transaction.commit();
//这个button是更改fragment显示的
button= (Button) findViewById(R.id.button);
//这个button是传至的
button1= (Button) findViewById(R.id.button1);
//显示fragment2传输过来的数据
text= (TextView) findViewById(R.id.text);
/**
* 传至的方法
*/
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2= (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment);
Log.e("fragment 2", String.valueOf(fragment2));
fragment2.showMessageFromActivity("我是传过来的数据");
}
});
/**
* 替换fragment的方法
*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这个事物需要重新实例化一下,要不然报错
FragmentTransaction transaction=manager.beginTransaction();
transaction.remove(fragment1);
fragment2=new Fragment2();
transaction.replace(R.id.fragment,fragment2);
transaction.commit();
}
});
}
public void showFromFragment2(String message){
text.setText(message);
}
}
package com.example.shibo.testfragment0321;
import android.app.Activity;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
TextView text;
Button button;
Button button1;
//需要替换的fragment
Fragment fragment1;
Fragment fragment2;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
fragment1=new Fragment1();
transaction.add(R.id.fragment,fragment1);
transaction.commit();
//这个button是更改fragment显示的
button= (Button) findViewById(R.id.button);
//这个button是传至的
button1= (Button) findViewById(R.id.button1);
//显示fragment2传输过来的数据
text= (TextView) findViewById(R.id.text);
/**
* 传至的方法
*/
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2= (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment);
Log.e("fragment 2", String.valueOf(fragment2));
fragment2.showMessageFromActivity("我是传过来的数据");
}
});
/**
* 替换fragment的方法
*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这个事物需要重新实例化一下,要不然报错
FragmentTransaction transaction=manager.beginTransaction();
transaction.remove(fragment1);
fragment2=new Fragment2();
transaction.replace(R.id.fragment,fragment2);
transaction.commit();
}
});
}
public void showFromFragment2(String message){
text.setText(message);
}
}