我个人将Fragment与Activity间的通信比喻为JSP与Servlet间的通信,fragment中用接口的方式来进行与Activity的通信。通信的结果可以作为数据传入另一个Fragment中。当然两个Fragment之间也是可以进行通信的~

注意加载或者切换Fragment时,必须new一个FragmentTransaction对象,不能用同一个FragmentTransaction对象进行对容器进行替换、增加fragment。将fragment填充进容器的方式是(这里分别给两个容器填充了两个fragment对象):

     FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.add(R.id.container_fragment, new MyFragment())
.commit(); //不能用同个FragmentTransaction来加载两个fragment
fragmentManager.beginTransaction()
.add(R.id.container_fragment02, new MyFragment02())
.commit();

更换fragment的方式是:

        getSupportFragmentManager().beginTransaction().
replace(R.id.container_fragment02, fragment)
.addToBackStack(null)
.commit();

  本实例完成的是点击一个Fragment中的按钮后,fragment给activity传值,activity接受到值后给另一个fragment(右边的)传值,以此改变textview中的内容。

activity_main.xml (定义了2个布局来作为fragment的容器)

<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:background="#000000"
android:orientation="horizontal" > <FrameLayout
android:id="@+id/container_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:layout_marginRight="1dp" /> <FrameLayout
android:id="@+id/container_fragment02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff"
android:layout_marginLeft="1dp" >
</FrameLayout> </LinearLayout>

fragment.xml

<RelativeLayout 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="${relativePackage}.${activityClass}" > <Button
android:id="@+id/fragment_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="41dp"
android:text="kale" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/fragment_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="46dp"
android:text="点击后传值"
android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>

MyFragment

package com.kale.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button; public class MyFragment extends Fragment{ private MyCallback mCallback;
//定义一个调用接口}
public interface MyCallback {
public void onBtnClick(View v);
}
String TAG = getClass().getName(); @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.e(TAG, "F------------onAttach->");
if(!(activity instanceof MyCallback)) {
throw new IllegalStateException("fragment所在的Activity必须实现Callbacks接口");
}
//把绑定的activity当成callback对象
mCallback = (MyCallback)activity;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "F------------onActivityCreated->");
//一定要在定义好视图后才来初始化控件,不能放在onCreat()里面
Button btn = (Button)getActivity().findViewById(R.id.fragment_button);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
mCallback.onBtnClick(v);
}
});
} @Override
public void onDetach() {
super.onDetach();
Log.e(TAG, "F------------onDetach->");
mCallback = null;//移除前幅值为空
} }

fragment02.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/fragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="null"
android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/fragment_textView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="传来的信息" /> </RelativeLayout>

MyFragment02

package com.kale.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment02 extends Fragment{
public static final String KEY = "key"; String TAG = getClass().getName(); @Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "F------------onCreateView->");
return inflater.inflate(R.layout.fragment02, container, false);
} @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "F------------onActivityCreated->");
//一定要在定义好视图后才来初始化控件,不能放在onCreat()里面
TextView tv = (TextView)getActivity().findViewById(R.id.fragment_textView); tv.setText("haha");
Bundle bundle = getArguments();
if (bundle == null) {
tv.setText("默认值01");
}
else {
tv.setText(bundle.getString(KEY, "默认值02"));
}
} }

MainActivity

package com.kale.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button; import com.kale.activity.MyFragment.MyCallback; public class MainActivity extends ActionBarActivity implements MyCallback{ String TAG = getClass().getName(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "------------onCreate------------"); FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.add(R.id.container_fragment, new MyFragment())
.commit(); //不能用同个FragmentTransaction来加载两个fragment
fragmentManager.beginTransaction()
.add(R.id.container_fragment02, new MyFragment02())
.commit();
} @Override
protected void onStart() {
super.onStart();
Log.e(TAG, "------------onStart------------");
} @Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "------------onRestart------------");
} @Override
protected void onResume() {
super.onResume();
Log.e(TAG, "------------onResume------------");
} @Override
protected void onPause() {
super.onPause();
Log.e(TAG, "------------onPause------------");
} @Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e(TAG, "------------onSaveInstanceState------------");
} @Override
protected void onStop() {
super.onStop();
Log.e(TAG, "------------onStop------------");
} @Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "------------onDestroy------------");
} @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.e(TAG, "------------onRestoreInstanceState------------");
} @Override
public void onBtnClick(View v) {
Button btn = (Button)v;
Log.i(TAG, btn.getText()+""); Bundle bundle = new Bundle();
bundle.putString(MyFragment02.KEY, btn.getText()+"");
MyFragment02 fragment = new MyFragment02();
fragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().
replace(R.id.container_fragment02, fragment)
.addToBackStack(null)
.commit(); }
}

源码下载:http://download.csdn.net/detail/shark0017/7709315

最新文章

  1. Spring 核心框架体系结构
  2. C#中如何定义全局变量及在各窗体中使用全局变量
  3. 看了汤姆大叔的“你真懂JavaScript吗?”的一些感慨
  4. mormot THttpApiServer使用例子
  5. Android应用安全之Android APP通用型拒绝服务漏洞
  6. HTML中行内元素的竖直方向的padding和margin是否真的无效
  7. data URI scheme
  8. Mac系统Safari浏览器启动无图模式
  9. PL/SQL客户端安装配置说明
  10. kafka configuration
  11. NEWS-包名-baseTest-类名-ConfigManager
  12. python之集合
  13. vue 组件中数组的更新
  14. linux服务器运维管理学习
  15. java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系
  16. 【XSY2887】【GDOI2018】小学生图论题 分治FFT 多项式exp
  17. A - Arcade Game Gym - 100814A (概率思维题)
  18. phpcms首页替换
  19. [Web安全] XXE漏洞攻防学习(上)
  20. html_常用技巧总结

热门文章

  1. [译]学习IPython进行交互式计算和数据可视化(六)
  2. C#串口通信—传输文件测试
  3. WebApp简单制作(后端也可以装逼啦)
  4. MFC文件操作
  5. iOS阶段学习第一天笔记(Mac终端的操作)
  6. C#制作RDLC报表
  7. 使用ActivityGroup类显示多个Activity
  8. Java 经典实例: Unicode字符和String之间的转换
  9. 模拟Executor策略的实现
  10. web桌面程序之图标拖动排序的分析