Fragment之间的通信

fragment之间永远不能通信都是通过他们所依附的Activity来通信的,通过接口回调的方式来通信

Activity--->Fragment:在activity中创建Bundle数据包,并调用fragment的setArguments(Bundle bundle)方法

fragment--->Activity:在fragment类中定义一个接口,并在他所属的activity中实现该接口,fragment在他的onAttach()方法执行期间捕获该接口的实现,然后就可以调用该接口方法,以便跟activity通信。

下面的代码实现了在一个Activity下有两个Fragment,在一个Fragment中输入值在另一个Fragment显示

public class MainActivity extends Activity implements MyListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        Fragment1 fragment1 = new Fragment1();
        fragmentTransaction.add(R.id.layout1, fragment1);
        fragmentTransaction.commit();

    }
       //在mainActivity中实现MyListener接口并实现它未实现的方法,在该方法中传递数据
    @Override
    public void send(String info) {
        Toast.makeText(this, "Activity成功接收" + info, 0).show();        
        Fragment2 fragment2 = new Fragment2();
        Bundle bundle = new Bundle();
        bundle.putString("name", info);
        fragment2.setArguments(bundle);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        //替换该Fragment                
        fragmentTransaction.replace(R.id.layout2, fragment2, "fragment2");

        fragmentTransaction.commit();
    }

}

上面的MainActivity布局就是添加两个Linearlayout,下面是创建两个Fragment,Fragment1的布局就是一个EditText一个Button,Fragment2的布局就是一个TextView。

 public class Fragment1 extends Fragment {
    EditText et;
    Button enter;
    private String info;
    @Override
    public void onAttach(Activity activity) {
        listener = (MyListener) activity;
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment1_lay,container, false);
        et = (EditText) view.findViewById(R.id.et);
        enter = (Button) view.findViewById(R.id.enter);
        enter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                info = et.getText().toString();
                listener.send(info);

            }
        });
        return view;
    }
    public MyListener listener;
        //定义一个接口
    public interface MyListener{ 
        public void send(String info);
    }

}
public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2_lay,container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(getArguments().get("name")+"");//获取传递的值
        Toast.makeText(getActivity(), "fragment"+getArguments().get("name")+"", 0).show();
        return view;
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoqiang_0719

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值