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

所属分类: 软件编程 / Android 阅读数: 1115
收藏 0 赞 0 分享

首先,如果你想在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的一次完美演绎!
下面通过一个例子来说明:
首先,我们看下界面

2016225172715543.jpg (480×800)

2016225172753509.jpg (480×800)

左边的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。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多