多个片段到活动通信 [英] Multiple Fragment to Activity communication

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

问题描述

在这里,我们又来了。
我写使用片段的应用程序。斯特凡·德布鲁因认为这将是比使用pcated TabHost去$ P $更好,他是对的,谢谢斯特凡。

here we go again. I am writing an app using fragments. Stefan de Bruijn suggested this would be better than using the deprecated TabHost and he was right, Thank you Stefan.

我也终于得到了通信从一个片段到我的活动工作多亏其他成员的帮助(你知道你是谁,谢谢大家)。

I have finally got communication from one fragment to me Activity working thanks to help from other members (You know who you are, Thank you all).

我现在有什么希望是最后一个问题。我的应用程序具有文本框在该是活动的一部分,在左侧和的FrameLayout在右侧的永久ListFragment,以允许显示不同的碎片的顶

I now have what hopefully is one last problem. My app has TextBox at the top that is part of the Activity, a permanent ListFragment on the left and FrameLayout on the right, to allow different Fragments to be displayed.

有没有,如果你在活动一样,所有的不同片段可以谈创建一个通用的听众的方法吗?

Is there any way of creating a generic "listener" if you like in the Activity that all the different Fragments can talk to?

要得到一个片段传递数据我已经使用了以下内容。

To get one Fragment passing data I have used the following.

MainActivity

MainActivity

import com.example.fragger.CoreFragment.OnDataPass;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity implements OnDataPass {

和片段code: -

and Fragment Code:-

package com.example.fragger;


import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View.OnClickListener;


public class CoreFragment extends Fragment{

int index;
Button Button1,Button2,Button3;
String Str,data;
OnDataPass dataPasser;

@Override
public void onAttach(Activity a) {
    super.onAttach(a);
        dataPasser = (OnDataPass) a;
}


public static CoreFragment newInstance(int index) {
    CoreFragment coreFragment = new CoreFragment();
    coreFragment.index = index;
    return coreFragment;
}


public interface OnDataPass {
    public void onDataPass(String data);

}

这是一个好主意,直​​到我显示我的框架(例如PlaceFragment)不同的片段。由于onDataPass从CoreFragment进口和实施,我不能与任何其他使用它。

Which is all well and good until I show a different Fragment in my Frame (e.g. PlaceFragment). As the onDataPass is imported from CoreFragment and implemented, I cannot use it with anything else.

有没有解决这个办法吗?

Is there any way around this?

感谢所有提前。
加里

Thanks all in advance. Gary

推荐答案

有关通信片段之间可以使用EventBus。该EventBus让您的活动和片段松耦合。

For communication between fragments you can use an EventBus. The EventBus makes your activity and fragments loosly coupled.

第一步是定义一个事件类型。例如: CarSelectedEvent

The first step is defining an EventType. For example: CarSelectedEvent

当一个汽车(或在您的情况下,一些文本型)CarSelectedEvent的选择必须在EventBus公布。
例如:

Upon selection of a Car (or some text type in your case) the CarSelectedEvent must be posted on the EventBus. Example:

eventBus.post(new CarSelectedEvent("volvo"));

有兴趣的事件的所有片段或活动,必须实现一个名为方式:

All fragments or activities interested in the Event have to implement a method called:

onEvent(CarSelectedEvent event){
... update your view
}

假设你有3个片段展示车的细节,每一个片段接收CarSelectedEvent,可以更新视图。当从屏幕(一个较小的屏幕或屏幕上旋转例如)除去一个片段的逻辑不会改变。唯一的区别是较少的碎片接收事件

Assume your have 3 fragments showing car details, each fragment receives the CarSelectedEvent and can update the view. When removing a fragment from the screen (for example on a smaller screen or a screen rotation) the logic does not change. The only difference is less fragments receiving the event.

您可以找到有关 https://github.com/greenrobot/EventBus EventBus更多信息。

You can find more information about EventBus on https://github.com/greenrobot/EventBus.

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

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