今天总结一下Fragment间的参数传递及结果返回的方法。

效果图:
1、点击“加载第二个Fragment按钮”,加载出第二个Fragment,同时传递过去参数:“从Fragment1传来的参数”这几个String;
2、当用户点击第二个Fragment中的几个图片时,将点中的结果返回给第一个Fragment,将用户的选择在第一个Fragment显示出来
一、基本架构搭建
首先,我们要把整个架构搭起来,然后再进行参数传递和回传
(一)、基本XML构建:
根据上面的效果,大家很容易看到两个Fragment的布局:
1、Fragment1的布局:(fragment1.xml)
很简单,垂直布局,上面一个ImageView来盛装返回过来的图片结果,下面一个Button来用来点击加载第二个Fragment;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <ImageView android:id="@+id/img_result" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="center"/> <Button android:id="@+id/load_fragment2_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="加载第二个Fragment"/> </LinearLayout>
2、Fragment2的布局:(fragment2.xml)
这个也是垂直布局,上面的一个TextView用来盛装从Fragment1传过来的String参数,下面的几个ImageView用来显示几个供用户选择的图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <ImageView android:id="@+id/img1" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal1"/> <ImageView android:id="@+id/img2" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal2"/> <ImageView android:id="@+id/img3" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal3"/> <ImageView android:id="@+id/img4" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal4"/> </LinearLayout>
(二)对应的Fragment类
1、在MainActivity初始化时,将Fragment1显示出来:
MainActivity对应的XML文件:(main_activity.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
对应的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
}
}
2、Fragment1:在用户点击时,将fragment2添加到当前页面显示出来;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
Button btn = (Button)view.findViewById(R.id.load_fragment2_btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(final View view) {
Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.main_layout, fragment2);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
3、Fragment2:至于目前的它还是很简单的,只要能显示出来 就好了,所以他的代码为:
public class Fragment2 extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
二、Fragment间参数传递
至于Fragment间参数为什么要用SetArguments来传递,我就不讲了,看这篇文章:《Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数》,我这里只说项目中如何使用:
在Fragment2中,新建一个函数:newInstance(String text)来接收传过来的参数:
新建一个Fragment2实例,然后将参数通过SetArguments设置到其中;
public static Fragment2 newInstance(String text) {
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
args.putString("param", text);
fragment.setArguments(args);
return fragment;
}
然后在Fragment2的OnCreateView的时候再从arguments中获取参数:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
if (getArguments() != null) {
String mParam1 = getArguments().getString("param");
TextView tv = (TextView)view.findViewById(R.id.textview);
tv.setText(mParam1);
}
return view;
}
在Fragment1中,在调起Fragmen2t时,通过调用newInstance函数来获取实例并传递参数:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
Button btn = (Button)view.findViewById(R.id.load_fragment2_btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(final View view) {
Fragment2 fragment2 = Fragment2.newInstance("从Fragment1传来的参数");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.main_layout, fragment2);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
(三)、从Fragment2向Fragment1回传参数
这里只有利用回调,有关回调传递参数的问题,我在前一篇文章中:《详解Dialog(三)——自定义对话框视图及参数传递》第三部分:参数传递;详细讲过,大家可以先看源码,如果源码不懂,可以参考下这篇文章,这里就不再赘述。
# fragment传递参数
# Android中给fragment写入参数的轻量开发包FragmentArgs简介
# 第二个
# 几个
# 加载
# 第一个
# 很简单
# 要用
# 这篇文章
# 回调
# 回传
# 我就
# 不懂
# 很容易
# 要把
# 然后再
# 自定义
# 这几个
# 在前
# 只说
# 如何使用
# 对话框
相关文章:
临沂网站制作企业,临沂第三中学官方网站?
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
建站主机如何安装配置?新手必看操作指南
手机网站制作与建设方案,手机网站如何建设?
制作国外网站的软件,国外有哪些比较优质的网站推荐?
建站主机选哪家性价比最高?
全景视频制作网站有哪些,全景图怎么做成网页?
免费ppt制作网站,有没有值得推荐的免费PPT网站?
清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?
股票网站制作软件,网上股票怎么开户?
制作网站的公司有哪些,做一个公司网站要多少钱?
建站之星24小时客服电话如何获取?
如何选择美橙互联多站合一建站方案?
实例解析angularjs的filter过滤器
上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?
微信推文制作网站有哪些,怎么做微信推文,急?
孙琪峥织梦建站教程如何优化数据库安全?
学校免费自助建站系统:智能生成+拖拽设计+多端适配
定制建站流程解析:需求评估与SEO优化功能开发指南
网站制作员失业,怎样查看自己网站的注册者?
网站网页制作专业公司,怎样制作自己的网页?
郑州企业网站制作公司,郑州招聘网站有哪些?
如何在IIS中配置站点IP、端口及主机头?
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
网站app免费制作软件,能免费看各大网站视频的手机app?
制作网站的模板软件,网站怎么建设?
威客平台建站流程解析:高效搭建教程与设计优化方案
网站制作大概多少钱一个,做一个平台网站大概多少钱?
,怎么用自己头像做动态表情包?
兔展官网 在线制作,怎样制作微信请帖?
阿里云高弹*务器配置方案|支持分布式架构与多节点部署
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
如何零成本快速生成个人自助网站?
微信小程序 input输入框控件详解及实例(多种示例)
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
建站之星代理费用多少?最新价格详情介绍
制作销售网站教学视频,销售网站有哪些?
开心动漫网站制作软件下载,十分开心动画为何停播?
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
制作网站的软件免费下载,免费制作app哪个平台好?
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
php8.4新语法match怎么用_php8.4match表达式替代switch【方法】
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
建站之星在线客服如何快速接入解答?
如何快速搭建高效可靠的建站解决方案?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
中山网站制作网页,中山新生登记系统登记流程?
建站之星后台密码遗忘或太弱?如何重置与强化?
电商网站制作公司有哪些,1688网是什么意思?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
*请认真填写需求信息,我们会在24小时内与您取得联系。