Android应用开发中Fragment与Activity间通信示例讲解

首先,如果你想在android3.0及以下版本使用fragment,你必须引用android-support-v4.jar这个包
然后你写的activity不能再继承自Activity类了,而是要继承android.support.v4.app.FragmentActivity,一些其他的父类也有相应的变化.
由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护.fragment实例化后会到activity中的fragmentmanager去注册一下,这个动作封装在fragment对象的onAttach中,所以你可以在fragment中声明一些回调接口,当fragment调用onAttach时,将这些回调接口实例化,这样fragment就能调用各个activity的成员函数了,当然activity必须implements这些接口,否则会包classcasterror
fragment和activity的回调机制又是OOP的一次完美演绎!
下面通过一个例子来说明:
首先,我们看下界面

左边的TextView会根据右边点击button的不同而改变。

下面开始介绍代码:

1.在layout里新建fragment1.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>

可以看出,这里就只有一个TextView

2.在layout里新建fragment2.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 3" /> </LinearLayout>

这里是三个button

3.创建类Fragment1继承Fragment

package lgx.fram.framents; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } }

重写onCreateView()方法,这里 return inflater.inflate(R.layout.fragment1, container, false); 这句话是重点

4.创建类Fragment2继承Fragment

package lgx.fram.framents; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } TextView textview; Button button, button2, button3; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button = (Button) getActivity().findViewById(R.id.button); button2 = (Button) getActivity().findViewById(R.id.button2); button3 = (Button) getActivity().findViewById(R.id.button3); textview = (TextView) getActivity().findViewById(R.id.fragment_text); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button.getText()); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button2.getText()); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(button3.getText()); } }); } } button = (Button) getActivity().findViewById(R.id.button);

通过这种方法来得到fragment上面的控件

5.activity_fragment.xml里面的代码是这个样子的

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="lgx.fram.framents.Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="lgx.fram.framents.Fragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>

注意:控件fragment里的android:name=" "里面填写的是你的Fragment类的绝对路径(脑子突然短路,是这样说的吗??),id用来标示fragment。

6.FragmentActivity是最简单的,就只是setContentView,并没有进行其他改变。看下面

package lgx.fram.framents; import android.app.Activity; import android.os.Bundle; public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); } }

在这里我的整个小应用就做完了。我这里的Fragment通过布局文件加入到Activity里的,还有另一种方式是通过编程的方式将Fragment加入Activity里。这里我简单叙述

上面的1,2,3,4都不需要动。

第5步骤,activity_fragment.xml里面的代码变成下面的

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > </LinearLayout>

你会发现我知识去掉了两个Fragment,整个LinearLayout加进去了id

第6个步骤,里面的注释,已经写得很清楚了:

package lgx.fram.framents; import android.os.Bundle; import android.app.Activity; import android.view.Display; import android.view.Menu;

@author lenovo 动态添加Fragment主要分为4步:
(1)获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。
(2)开启一个事务,通过调用beginTransaction方法开启。
(3)向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。
(4)提交事务,调用commit方法提交。

public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); Display display = getWindowManager().getDefaultDisplay(); if (display.getWidth() > display.getHeight()) { Fragment1 fragment1 = new Fragment1(); getFragmentManager().beginTransaction() .replace(R.id.main_layout, fragment1).commit(); } else { Fragment2 fragment2 = new Fragment2(); getFragmentManager().beginTransaction() .replace(R.id.main_layout, fragment2).commit(); } } }

这个代码的意思是,横竖屏显示不同的Fragment。如果是模拟机测试,请按Ctrl+F11。

时间: 2024-09-20 06:11:01

Android应用开发中Fragment与Activity间通信示例讲解的相关文章

Android中Service和Activity相互通信示例代码

前言 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,本文就给大家详细介绍了关于Android中Service和Activity相互通信的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. Activity向Service通信 第一种方式:通过MyBinder方式调用Service方法 MainActivity public class Ma

Android应用开发中Fragment间通信的实现教程

为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让fragment的自包含和模块化.一旦你定义了这些可重用的Fragment,你可以通过Activity将它们关联起来并通过程序的逻辑代码将他们连接起来以实现整体组合的UI. 你会经常想要一个fragment与另一个fragment通信,例如基于用户事件改变fragment中的内容.所有fragment质检单额通信都是通过Activity关联起来的.两个fragment之间不应该直接进行通

Android应用开发中Fragment存储功能的基本用法_Android

一.引言 在移动应用程序的架构设计中,界面与数据即不可分割又不可混淆.在绝大部分的开发经历中,我们都是使用Fragment来进行界面编程,即使保存数据基本上也只是界面相关控件的数据,很少做其他的数据保存,毕竟这样与开发原则相背,而今天这一篇博客就要来介绍一下Fragment的另类用法,只是用来保存数据而没有任何界面元素. 二.实现背景 对于Fragment的数据保存方法,不难想到还是与setRetainInstance有关系的.这样一来所处的背景也是在屏幕旋转或其他配置改变时需要用到.无论在开发

Android应用开发中Fragment存储功能的基本用法

一.引言 在移动应用程序的架构设计中,界面与数据即不可分割又不可混淆.在绝大部分的开发经历中,我们都是使用Fragment来进行界面编程,即使保存数据基本上也只是界面相关控件的数据,很少做其他的数据保存,毕竟这样与开发原则相背,而今天这一篇博客就要来介绍一下Fragment的另类用法,只是用来保存数据而没有任何界面元素. 二.实现背景 对于Fragment的数据保存方法,不难想到还是与setRetainInstance有关系的.这样一来所处的背景也是在屏幕旋转或其他配置改变时需要用到.无论在开发

浅谈Android App开发中Fragment的创建与生命周期_Android

Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除. Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响.例如:当activity暂停时

实例讲解Android应用开发中Fragment生命周期的控制_Android

一.Fragment的生命周期初探 因为Fragment必须嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相关的. 如果Activity是暂停状态,其中所有的Fragment都是暂停状态:如果Activity是stopped状态,这个Activity中所有的Fragment都不能被启动:如果Activity被销毁,那么它其中的所有Fragment都会被销毁. 但是,当Activity在活动状态,可以独立控制Fragment的状态,比如加上或者移除F

浅谈Android app开发中Fragment的Transaction操作_Android

在Android中,对Fragment的操作都是通过FragmentTransaction来执行.而从Fragment的结果来看,FragmentTransaction中对Fragment的操作大致可以分为两类: 显示:add() replace() show() attach() 隐藏:remove() hide() detach() 对于每一组方法,虽然最后产生的效果类似,但方法背后带来的副作用以及对Fragment的生命周期的影响都不尽相同. add() vs. replace()只有在F

Android应用开发中Fragment的静态加载与动态加载实例_Android

1.Fragment的静态使用Fragment是作为Activity的UI的一部分,它内嵌在Activity中,多个Fragment可以把一个Activity分成多个部分,这在大屏幕手机或者平板电脑中会比较多的用到,这样就不用使用多个Activity来切换这么麻烦了.当然Fragment也可以不显示,只在后台处理一些数据,这篇文章中就暂时不谈到这个.以下来看怎么静态地在Activity的布局文件中添加Fragment. 自定义的Fragment通常要继承Fragment这个类,也有一些特殊的是继

浅谈Android App开发中Fragment的创建与生命周期

Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除. Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响.例如:当activity暂停时