Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)

Fragment与Activity之间的数据交换,大体上包括三种:

一、Fragment从Activity获取数据(本文章只介绍第一种);

二、Activity从Fragment获取数据;

三、Fragment之间获取数据。


实现效果图:

从Activity传递数据到两个Fragment中,Fragment获取数据后,展示出来。


源代码:


布局文件:

activity_main:

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

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="2" 
        android:background="#CCCCCC">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:layout_weight="2">
    </LinearLayout>
   
</LinearLayout>

MyFragment1的布局文件f1:


<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="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MyFragment2的布局文件f2:

<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="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

代码文件:

MainActivity:

package com.fragmentdemo5_commute;

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

/**
 * 一、Fragment从Activity获取数据。
 */
public class MainActivity extends Activity {
	private FragmentManager manager;
	private FragmentTransaction transaction;

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

		manager = getFragmentManager();
		transaction = manager.beginTransaction();

		MyFragment1 fragment1 = new MyFragment1();
		Bundle bundle1 = new Bundle();
		bundle1.putString("id", "Activity发送给MyFragment1的数据");
		fragment1.setArguments(bundle1);
		transaction.replace(R.id.left, fragment1, "left");

		MyFragment2 fragment2 = new MyFragment2();
		Bundle bundle2 = new Bundle();
		bundle2.putString("id", "Activity发送给MyFragment2的数据");
		fragment2.setArguments(bundle2);
		transaction.replace(R.id.right, fragment2, "right");

		transaction.commit();
	}

}

MyFragment1:

package com.fragmentdemo5_commute;

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

public class MyFragment1 extends Fragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f1, null);
		
		TextView textView = (TextView) view.findViewById(R.id.textView);
		Bundle bundle1 = getArguments();
		textView.setText(bundle1.getString("id"));
		return view;
	}

}

MyFragment2:


package com.fragmentdemo5_commute;

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

public class MyFragment2 extends Fragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f2, null);
		
		TextView textView = (TextView) view.findViewById(R.id.textView);
		Bundle bundle2 = getArguments();
		textView.setText(bundle2.getString("id"));
		return view;
	}

}

源代码下载:

点击下载源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红日666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值