通常,一个活动可能包含一个或多个协同工作的Fragment以向用户展现一致的UI。在这种情况下,Fragment之间就需要彼此通信并交换数据,这是非常重要的。例如,一个Fragment可能包含了一个条目列表(如来自一个RSS提要的帖子)。当用户轻点Fragment上的某个条目时,所选条目的详细信息可能会显示在另一个Fragment上。
下面的“试一试”介绍了一个Fragment如何访问另一个Fragment中的视图。

(1) 使用上一节创建的项目,向Fragment1.xml文件中添加如下所示的粗体代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="fill_parent"
  7.    android:background="#00FF00"
  8.    >
  9. <TextView
  10.    android:id="@+id/lblFragment1"
  11.    android:layout_width="fill_parent"
  12.    android:layout_height="wrap_content"
  13.    android:text="This is fragment #1" />
  14. </LinearLayout>
(2) 向fragment2.xml文件中添加如下所示的粗体代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="fill_parent"
  6.    android:layout_height="fill_parent"
  7.    android:background="#FFFE00"
  8.    >
  9. <TextView
  10.    android:layout_width="fill_parent"
  11.    android:layout_height="wrap_content"
  12.    android:text="This is fragment #2" />
  13. <Button android:id="@+id/btnGetText"
  14.      android:layout_width="wrap_content"
  15.      android:layout_height="wrap_content"
  16.      android:text="Get text in Fragment #1" />
  17. </LinearLayout>
(3) 修改MainActivity.java文件,将前几节添加的代码注释掉。修改后的代码如下所示:

  1. package net.learn2develop.Fragments;
  2. import net.learn2develop.Fragments.R;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. public class MainActivity extends Activity {
  6.    /** Called when the activity is first created. */
  7.    @Override
  8.    public void onCreate(Bundle savedInstanceState) {
  9.      super.onCreate(savedInstanceState);
  10.      setContentView(R.layout.main);
  11.    }
  12. }
(4) 向Fragment2.java文件中添加如下所示的粗体代码:

  1. package net.learn2develop.Fragments;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. public class Fragment2 extends Fragment {
  11.    @Override
  12.    public View onCreateView(LayoutInflater inflater,
  13.    ViewGroup container, Bundle savedInstanceState) {
  14.      // Inflate the layout for this fragment
  15.      return Inflater.inflate(R.layout.fragment2,
  16.         container, false);
  17.    }
  18.    
  19.    @Override
  20.    public void onStart() {
  21.      super.onStart();
  22.      //---Button view---
  23.      Button btnGetText = (Button)
  24.        getActivity().findViewById(R.id.btnGetText);
  25.      btnGetText.setOnClickListener(new View.OnClickListener() {
  26.        public void onClick(View v) {
  27.          TextView lbl = (TextView)
  28.            getActivity().findViewById(R.id.lblFragment1);
  29.          Toast.makeText(getActivity(), lbl.getText(),
  30.            Toast.LENGTH_SHORT).show();
  31.        }
  32.      });
  33.    }
  34. }
(5) 将这两个Fragment放到main.xml中:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <fragment
  8.      android:name="net.learn2develop.Fragments.Fragment1"
  9.      android:id="@+id/fragment1"
  10.      android:layout_weight="1"
  11.      android:layout_width="0px"
  12.      android:layout_height="match_parent" />
  13.    <fragment
  14.      android:name="net.learn2develop.Fragments.Fragment2"
  15.      android:id="@+id/fragment2"
  16.      android:layout_weight="1"
  17.      android:layout_width="0px"
  18.      android:layout_height="match_parent" />
  19. </LinearLayout>
(6) 按下F11键在Android模拟器上调试应用。在右边第2个Fragment中单击按钮,将会看到Toast类显示出文本This is fragment #1(如图2-13所示)。

示例说明

由于Fragment被嵌入到了活动中,因此可以通过getActivity()方法获取Fragment当前所嵌入到的活动,然后使用findViewById()方法找到Fragment中包含的视图:

  1.  TextView lbl = (TextView)
  2.        getActivity().findViewById(R.id.lblFragment1);
  3.      Toast.makeText(getActivity(), lbl.getText(),
  4.  Toast.LENGTH_SHORT).show();

Fragment之间的交互的更多相关文章

  1. 安卓任意两个或多个Fragment之间的交互与刷新界面

    平时项目中遇到一个问题:在子fragment中刷新父fragment的界面,通俗的说也就是在任何一个fragment中来刷新另一个fragment.大家都知道activity和fragment之间的交 ...

  2. Fragments之间的交互(实现参数传递)

    Fragments之间的交互(实现参数传递) 日常开发中,通常Fragments之间可能需要交互,比如基于用户事件改变Fragment的内容.所有Fragment之间的交互需要通过他们关联的Activ ...

  3. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  4. Android中使用开源框架EventBus3.0实现Fragment之间的通信交互

    1.概述 在之前的博文中简单介绍过如何实现fragment之间的信息交互:<Android中Fragment与Activity之间的交互(两种实现方式)>,今天继续给大家介绍一种可以实现此 ...

  5. fragment之间的信息交互——onActivityResult()不经过Activity

    1.本文讲述如何fragment与fragment之间互传信息,不用使用Activity的onActivityResult()方法 核心思想:FirstFragment获取到SecondFragmen ...

  6. Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信

    为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...

  7. Android基础——Fragment与Activity交互

    今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起.下面我们看 ...

  8. 我的Android第五章:通过Intent实现活动与活动之间的交互

    Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互              同时Inten ...

  9. Fragment之间的通信(四)

    自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...

随机推荐

  1. OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解 (转)

    cv::Matdepth/dims/channels/step/data/elemSizeThe class Mat represents an n-dimensional dense numeric ...

  2. MatLab 2014a编译jar包时mcc无法使用的问题

    http://blog.csdn.net/heroafei/article/details/43273373 MatLab 2014a编译jar包时mcc无法使用的问题 2015-01-29 16:5 ...

  3. JSP—作用域

    application: 用于同一个应用内,所有用户之间的数据共享 作用域: request作用域: 在页面转发,包含中同样有效. <% pageContext.include("te ...

  4. Linux服务器---安装bind

    安装bind 1.安装bind软件,需要安装3 个bind.bind-chroot.bind-util [root@localhost pub]# yum install -y bind bind-c ...

  5. POI Excel文件的读取与写入

    1. 创建目录 if(!(new File(path).isDirectory())){ new File(path).mkdirs();} 2. 读取Excel文件,并进行写入操作 Workbook ...

  6. 远程获得的有趣的linux命令

    使用这些工具从远程了解天气.阅读资料等. 我们即将结束为期 24 天的 Linux 命令行玩具日历.希望你有一直在看,如果没有,请回到开始,从头看过来.你会发现 Linux 终端有很多游戏.消遣和奇怪 ...

  7. MySQL数据库----函数

    函数 MySQL中提供了许多内置函数,例如: CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENG ...

  8. GitHub+Hexo 搭建个人网站

    GitHub+Hexo 搭建个人网站 转自 https://www.sufaith.com/article/561.html 一.创建GitHub Pages站点 GitHub Pages是一种静态站 ...

  9. SQL学习之Can't connect to MySQL server on localhost (10061)

    最近升级 了系统,开机后连接MySQL报错,Can't connect to MySQL server on localhost (10061): 估计是升级系统清除了以前的缓存设置,网上很多方法是命 ...

  10. python的re正则表达式模块

    元字符  .   *   +   ?   ^   $   { }     [ ]     -     \ .  匹配除了/n之外的任意一个字符 * 匹配*前面的单个字符任意次,即[0,+∞] + 匹配 ...