Anctivity与Fragment不得不说的事情(activity动态替换添加fragment)

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);
    }
}


fragment2的代码处理:

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);
    }
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值