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. linux 任务调度

    crontab 定时任务 -e  编辑 -l  查看 -r  删除 参数 * * * * * 分钟  小时  天数   月   星期几

  2. JDK7和JDK8concurrentHashmap区别

    哈希表 1.介绍 哈希表就是一种以 键-值(key-indexed) 存储数据的结构,我们只要输入待查找的值即key,即可查找到其对应的值. 哈希的思路很简单,如果所有的键都是整数,那么就可以使用一个 ...

  3. 线程池构造类 ThreadPoolExecutor 的 5 个参数

    1.corePoolSize :核心线程数 2.maxPoolSize: 最大线程数 3.keepAliveTime :闲置线程存活时间 4.unit:参数keepAliveTime的时间单位,有7种 ...

  4. 深度学习原理与框架-神经网络-cifar10分类(代码) 1.np.concatenate(进行数据串接) 2.np.hstack(将数据横着排列) 3.hasattr(判断.py文件的函数是否存在) 4.reshape(维度重构) 5.tanspose(维度位置变化) 6.pickle.load(f文件读入) 7.np.argmax(获得最大值索引) 8.np.maximum(阈值比较)

    横1. np.concatenate(list, axis=0) 将数据进行串接,这里主要是可以将列表进行x轴获得y轴的串接 参数说明:list表示需要串接的列表,axis=0,表示从上到下进行串接 ...

  5. C# 申请非托管内存

    方式一:使用 stackalloc 关键字 int* block = stackalloc int[100]; 注:此关键字仅在局部变量初始值设定项中有效. 以下代码导致编译器错误. int* blo ...

  6. day10-列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 1.生成一个列表 a = [i for i in range(1,100) if ...

  7. 虚拟机mac 与主机的网络共享

    1. 主机建立共享文件夹 aaa 2.虚拟机采用桥接 3.mac打开Finder 找到 “前往” 连接服务器”输入“smb://192.168.1.xx”(你电脑的ip地址),点击连接.

  8. DotNet菜鸟入门之无限极分类(一)设计篇

    写这个教程的原因,是因为,无限极分类,在许多项目中,都用得到.而对于新手来说,不是很好理解,同时,操作上也有一些误区或者不当之处.所以我就斗胆,抛砖引玉一下,已一个常见的后台左侧频道树为例子,讲解一下 ...

  9. leetcode ex3 找出穿过最多点的直线 Max Points on a Line

    题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...

  10. python实现排序算法二:归并排序

    ##归并排序 ##基本思想:对于两个排好序的数组A和B,逐一比较A和B的元素,将较小值放入数组C中,当A或者B数组元素查询完后,将A或者B剩余的元素直接添加到C数组中,此时C数组即为有序数组,这就是归 ...