1、fragment简单套用(静态调用):

新建一个fragment,其xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" /> </LinearLayout>

其java文件如下:

 public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}

在主Activity的布局中加入:

    <fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentdemo.Fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />

这样就可以了;

2、动态使用fragment

首先,Activity的布局中不用加fragment:

<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" > </LinearLayout>

在java中:

Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();

分四步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。

若是想在Fragment中调用Activity中的函数,可直接使用(MainActivity)getActivity()来获取活动;

若想在创建Fragment时,从Activity中传递数据给Fragment,可利用Bundle:

        FriendsFragment friendsFragment = new FriendsFragment();
Bundle bundle = new Bundle();
serializable = new DataSerializable();
serializable.setListFriend(myFriendList);
bundle.putSerializable("list_friend", serializable);
friendsFragment.setArguments(bundle);
fragmentList.add(friendsFragment);

Fragment代码中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_friends, container, false); DataSerializable dataSerializable = (DataSerializable)getArguments().getSerializable("list_friend");
List<MyFriend> myFriendsList = dataSerializable.getListFriend(); return view;
}

其中,DataSerializable是自己实现的一个类,实现了Serializable接口,这样就可以在Bundle中存储更多复杂类型的数据,简单数据比如int,直接用Bundle.putint();

DataSerializable代码如下:

public class DataSerializable implements Serializable {
private List<MyFriend> myFriendList; public void setListFriend(List<MyFriend> myFriendList){
this.myFriendList = myFriendList;
}
public List<MyFriend> getListFriend(){
return myFriendList;
}
}

如果在Activity中想调用Fragment中的函数:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

fragment 与activity通信 Bundle 序列化数据 Serializable的更多相关文章

  1. 关于Fragment与Fragment、Activity通信的四种方式

    一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...

  2. Android学习——Fragment与Activity通信(一)

    学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...

  3. Android 使用EventBus进行Fragment和Activity通信

    本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 Ev ...

  4. Android学习——Fragment与Activity通信(二)

    接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) ...

  5. Fragment 与 Activity 通信

    先说说背景知识: (From:http://blog.csdn.net/t12x3456/article/details/8119607) 尽管fragment的实现是独立于activity的,可以被 ...

  6. Activity通过bundle传递数据

    从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...

  7. android Fragment与Activity交互,互相发数据(附图具体解释)

    笔者最近看官方training.发现了非常多实用又好玩的知识. 当中.fragment与Activity通信就是一个. fragment与Activity通信主要是两点: 1.fragment传递信息 ...

  8. android中fragment与activity之间通信原理以及例子

    参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...

  9. Fragment间的通信

    在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...

随机推荐

  1. 使用STM32CubeMX生成待机开关功能

    使用的开发板为MINISTM32 通过长按数秒KEY_UP 按键开机,并且通过 DS1 的闪烁指示程序已经开始运行,再次长按该键,则进入待机模式, DS1 关闭,程序停止运行.利用STM32的stan ...

  2. react-native 插件汇总

    部分自己搜集 部分 来自别的网站 第三方路由插件 react-native-router-flux react-native-scrollable-tab-view 选项卡 测滑动菜单 react-n ...

  3. day37-常见内置模块六(其他模块)

    其他内置模块 1.subprocess(系统交互)模块 2.math模块 3.zipfile模块 4.keyword模块 5....

  4. Yum安装MySQL以及相关目录路径和修改目录

    有些时候,为了方便,有些同学喜欢通过yum的方式安装MySQL,没有设置统一的文件目录以及软件目录,那么就会为后续的维护工作带来很大的麻烦! 下面就简单介绍一下yum安装MySQL的步骤以及这类安装下 ...

  5. hadoop配置2.6.1 centos7

    上传文件(分发)的三种方式: 1.本地: -file 的模式,上传一些小的文件. 例如: -file ./test INPUT_FILE_PATH_1="/The_Man_of_Proper ...

  6. mui页面交互

    1.页面a准备函数 function hideBackBtn() { // $('.menua').removeClass('mui-icon-back').addClass('mui-icon-ba ...

  7. postman 请求种添加用户权限

    1. 打开postman, 在Tests输入以下内容: var jsonData =JSON.parse(responseBody);//获取body中返回的所有参数 postman.setGloba ...

  8. docker之手动构建新的镜像

    转自:https://www.cnblogs.com/jsonhc/p/7766561.html 查看本地现有镜像: [root@docker ~]# docker images REPOSITORY ...

  9. groovy语法

    1.注释1.1. 单行注释1.2. 多行注释1.3. GroovyDoc注释1.4. Shebang线2.关键词3.标识符3.1. 普通标识符3.2. 带引号的标识符4.字符串4.1. 单引号字符串4 ...

  10. Structs复习 Result第一部分

    Jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&q ...