fragment 与activity通信 Bundle 序列化数据 Serializable
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的更多相关文章
- 关于Fragment与Fragment、Activity通信的四种方式
一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...
- Android学习——Fragment与Activity通信(一)
学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...
- Android 使用EventBus进行Fragment和Activity通信
本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 Ev ...
- Android学习——Fragment与Activity通信(二)
接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) ...
- Fragment 与 Activity 通信
先说说背景知识: (From:http://blog.csdn.net/t12x3456/article/details/8119607) 尽管fragment的实现是独立于activity的,可以被 ...
- Activity通过bundle传递数据
从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...
- android Fragment与Activity交互,互相发数据(附图具体解释)
笔者最近看官方training.发现了非常多实用又好玩的知识. 当中.fragment与Activity通信就是一个. fragment与Activity通信主要是两点: 1.fragment传递信息 ...
- android中fragment与activity之间通信原理以及例子
参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...
- Fragment间的通信
在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...
随机推荐
- 【Source Insight 】之marco学习笔记1
我们学习编程语言都是从Hello World!,现在我们学习marco也不例外 打开C:\Users\%USERPROFILE%\Documents\Source Insight 4.0\Projec ...
- CKEditor 5
1.官网 https://ckeditor.com/ckeditor-5/download/ 2.
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...
- day41-解决粘包问题
一.socket缓冲区 研究粘包之前先看看socket缓冲区的问题: 二.socket缓存区的详细解释 每个socket被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send ...
- elasticSearch-DSL
DSL: query_string match match_phrase match_phrase_prefix multi_match simple_query_string term term ...
- Leetcode 题解 Combinations:回溯+求排列组合
罗列出从n中取k个数的组合数组. 首先,求C(n,k)这个实现,很粗糙,溢出也不考虑,好的方法也不考虑.笨蛋.心乱,上来就写.. 另外,发现在递归中,不能申请太大的数组?貌似不是这个问题,是我自己越界 ...
- byobu session window split
new session: Ctrl + Shift + F2 window: F2 split: Shift/Ctrl + F2 move session: Alt + Up/Down window ...
- get return value of python in shell
from: https://stackoverflow.com/questions/2115615/assigning-value-to-shell-variable-using-a-function ...
- 文件操作 freopen函数
转自http://blog.csdn.net/zhuyi2654715/article/details/6963673 当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC等)中运 ...
- 使用adb查看CPU和内存
adb shell ->cat/sys/class/net/wlan0/address 获取Mac地址 abd shell –>cat /proc/cpuinfo 获取CPU信息 adb ...