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,你就可以把 ...
随机推荐
- leetcode1014
这道题暴力算法,会超时: class Solution(object): def maxScoreSightseeingPair(self, A: 'List[int]') -> int: n ...
- java 常用第3方工具
https://www.cnblogs.com/chenpi/p/5608628.html#_label4
- day23-类的封装
1.封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容.所以,在使用面向对象的封装特性时,需要:1)将内容封装到某处2)从某处调用被封装的内容 第一步:将内容封装到某处 cl ...
- spring异常
1.The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirectly refe ...
- java web项目使用IDEA打成war包
步骤: 1.点击 File -->Project Structure...如下图: 2.出现如下界面后点击 Artifacts--> 绿色加号-->Web Application:A ...
- vue:页面跳转和传参(页面之间,父传子,子传父)
1.返回上一个页面: A.<a @click="$router.back(-1)" class="btn">重新加载</a> B.thi ...
- ReactiveX 学习笔记(3)转换数据流
Transforming Observables 本文的主题为转换 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(二)Transform ...
- idea将项目打成war包
idea将项目打成war包(转载) 2018年02月28日 20:08:03 沈行的专栏 阅读数:13773更多 个人分类: Java 首先点击这里进入项目的配置页面 在Artifacts栏里点击 ...
- 测试Array对象的sort方法的作用。将1985,1970,1999,1998,2000,1963这些年份按升序输出。
<script type="text/javascript">var years = new Array(1985,1970,1999,1998,2000,1963); ...
- Hibernate 再接触 关系映射 一对一单向外键关联
对象之间的关系 数据库之间的关系只有外键 注意说关系的时候一定要反面也要说通 CRUD 数据库之间设计 主键关联 单向的外键关联 中间表 一对一单向外键关联 Husband.java package ...