两个片段之间的基本通信 [英] Basic communication between two fragments

查看:21
本文介绍了两个片段之间的基本通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项活动 - MainActivity.在此活动中,我有两个片段,这两个片段都是我在 xml 中以声明方式创建的.

I have one activity - MainActivity. Within this activity I have two fragments, both of which I created declaratively within the xml.

我试图将用户输入的文本String 传递到Fragment AFragment B 中的文本视图.然而,事实证明这是非常困难的.有谁知道我如何实现这一目标?

I am trying to pass the String of text input by the user into Fragment A to the text view in Fragment B. However, this is proving to be very difficult. Does anyone know how I might achieve this?

我知道片段可以使用 getActivity() 获取对其活动的引用.所以我猜我会从那里开始?

I am aware that a fragment can get a reference to it's activity using getActivity(). So I'm guessing I would start there?

推荐答案

查看 Android 开发者页面:http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Have a look at the Android developers page: http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

基本上,您在 Fragment A 中定义一个接口,并让您的 Activity 实现该接口.现在您可以在您的 Fragment 中调用接口方法,您的 Activity 将接收该事件.现在在您的活动中,您可以调用您的第二个 Fragment 以使用接收到的值更新文本视图

Basically, you define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event. Now in your activity, you can call your second Fragment to update the textview with the received value

您的 Activity 实现了您的接口(参见下面的 FragmentA)

Your Activity implements your interface (See FragmentA below)

public class YourActivity implements FragmentA.TextClicked{
    @Override
    public void sendText(String text){
        // Get Fragment B
        FraB frag = (FragB)
            getSupportFragmentManager().findFragmentById(R.id.fragment_b);
        frag.updateText(text);
    }
}

Fragment A 定义了一个接口,并在需要时调用该方法

Fragment A defines an Interface, and calls the method when needed

public class FragA extends Fragment{

    TextClicked mCallback;

    public interface TextClicked{
        public void sendText(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (TextClicked) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement TextClicked");
        }
    }

    public void someMethod(){
        mCallback.sendText("YOUR TEXT");
    }

    @Override
    public void onDetach() {
        mCallback = null; // => avoid leaking, thanks @Deepscorn
        super.onDetach();
    }
}

片段 B 有一个公共方法来处理文本

Fragment B has a public method to do something with the text

public class FragB extends Fragment{

    public void updateText(String text){
        // Here you have it
    }
}

这篇关于两个片段之间的基本通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆