Android中Fragment与Fragment,Fragment与Activity之间的通信

本文介绍了在Android开发中,如何处理Fragment与Fragment以及Fragment与Activity之间的通信问题。通过实例展示了当点击Fragment中的按钮时,如何影响其他Fragment或Activity中的内容。主要探讨了两种不同的实现通信的方法。
摘要由CSDN通过智能技术生成

上一篇文章讲解了Fragment的一些基本用法,主要包括Fragment的动态和静态添加的问题。但是在实际的项目开发中,Fragment与Fragment,Fragment与Activity之间的数据通信也是经常会用遇到的问题。比如你点击一个Fragment中的按钮,使得另一个Fragment中的内容发生改变。下面就用两个例子使用用不同的方法来实现这个功能。

Fragment与Fragment之间的通信

一个Activity中经常会包含多个Fragment,多个Fragment之间的数据通信就变成了一个问题,下面是一个两个Fragment之间的通信的例子,理解了这个,多个Fragment之间通信也就不难理解了。

FragmentDemoActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.daniel.test.activities.FragmentDemoActivity">

    <fragment
        <strong>android:id="@+id/fragment_send"</strong>
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.example.daniel.test.fragments.SendFragment"
        tools:layout="@layout/fragment_send" />

    <fragment
        <strong>android:id="@+id/fragment_receive"</strong>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.daniel.test.fragments.ReceiveFragment"
        tools:layout="@layout/fragment_receive" />
</LinearLayout>

public class FragmentDemoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_demo);
    }
}


ReceiveFragment

public class ReceiveFragment extends Fragment {

    public ReceiveFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_receive, container, false);
    }
}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.daniel.test.fragments.ReceiveFragment">

    <TextView
        android:id="@+id/textview_receive"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:textSize="24sp"
        />

</FrameLayout>
SendFragment

public class SendFragment extends Fragment {


    public SendFragment() {
        // Required empty public constructor
    }
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_send, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        <strong>Button sendBtn = (Button)getActivity().findViewById(R.id.btn_send_sendFragment);</strong>
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView receiveTexView = (TextView) getActivity().findViewById(R.id.textview_receive);
                receiveTexView.setText("Send message received!");
            }
        });
    }
}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.daniel.test.fragments.SendFragment">

    <Button
        android:id="@+id/btn_send_sendFragment"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />
</FrameLayout>
通过这个例子,就能发现,fragment可以通过getActivity()这个方法获得所在Activity的引用,进而可以对在同一个Activity中的其他Fragment进行操作。

注意:
1. 在Activity的xml中,fragment的id一定要写,不然会报XML解析错误。
2. 在SendFragment中获得sendBtn的操作不能放在onCreateView里,因为此时Fragment的view还没有被inflate。放在onActivityCreated中即可,关于Fragment的生命周期不清楚的,可以参考我的上一篇博客。
 

Fragment与Activity通信

上面那个例子中,虽然也实现了我们要的效果,但是代码耦合程度太高,在SendFragment中直接用代码控制ReceiveFragment,这样一来SendFragment不易维护和扩展。下面的例子中,SendFragment向Activity发送请求,由Activity来决定ReceiveFragment改怎么样做,这样代码的耦合程度就降低了。

SendFragment修改为

package com.example.daniel.test.fragments;


import android.content.Context;
import android.os.Bundle;
<strong>import android.support.v4.app.Fragment;</strong>
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.daniel.test.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class SendFragment extends Fragment {


    private SendBtnListener mSendBtnListener;

    public SendFragment() {
        // Required empty public constructor
    }

    public interface SendBtnListener{
        public void sendBtnClick();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d("Debug","onAttach invoked!");

            if(context instanceof SendBtnListener){
                mSendBtnListener = (SendBtnListener)context;
            }
            else {
                throw new ClassCastException(context.toString()+"must implement SendBtnListener!");
            }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_send, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Button sendBtn = (Button)getActivity().findViewById(R.id.btn_send_sendFragment);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TextView receiveTexView = (TextView) getActivity().findViewById(R.id.textview_receive);
//                receiveTexView.setText("Send message received!");
                mSendBtnListener.sendBtnClick();
            }
        });
    }
}
 
 
FragmentDemoActivity 修改为
 
 
public class FragmentDemoActivity extends AppCompatActivity implements SendFragment.SendBtnListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_demo);
    }

    @Override
    public void sendBtnClick() {
        TextView receiveTexView = (TextView) findViewById(R.id.textview_receive);
        receiveTexView.setText("Send message received!");
    }
}

注意:在API 23中Fragment有个Bug,新的public void onAttach(Context context)不被调用的问题,应使用support Fragment代替。详情请查看我的博客。

点击Send按钮前
 
 

 
 
点击Send按钮后
 
 

 
 
实现的流程为:
1)在SendFragment中声明一个Interface,称为回调接口。
2)Activity实现这个接口。
3)在Fragment的onAttach()方法中使用Interface的变量获得Activity的引用,用这个接口的引用调用Activity中的方法。
4)Activity中的方法决定ReceiveFragment的响应。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值