Fragment之10_____碎片之间的通信方式 之一

1.  碎片与碎片之间直接通信
FragmentOther   other = (FragmentOther)  getFragmentManager(). findFragmentByTag("other");

other.callMethod(...);


2. 当一个碎片希望启动另一个碎片时, Fragment类 setTargetFragment(Fragment  f,  int  requestCode)方法  支持调用碎片使用被调用碎片来设置它的身份.看一段示例代码:

Fragment  calledFragment = new CalledFragment();
calledFragment.setTargetFragment(this, 0);
getFragmentManager().beginTransaction().add(calledFragment,  "work").commit();

这里创建一个新calledFragment 对象,在被调用碎片calledFragment 上将调用碎片设置为当前碎片 this,  使用事务将calledFragment 添加到碎片管理器和Activity  中, 当被 调用碎片calledFragment运行时, 它可以调用 getTargetFragment(), 将返回调用碎片的引用.  使用此引用, 被调用碎片可以调用 this(即调用碎片) 上的方法.


3.  目标碎片(即被调用碎片)  到原碎片(即调用碎片) 的通信:
TextView tv =  (TextView)getTargetFragment().getView().findViewById(R.id.text1);
tv.setText("Set from  the  called  fragment");
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值