交际Android上的片段之间 [英] Communicative Between Fragments on Android

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

问题描述

我试图建立一个有两个标签,一个是文本框/按钮,TreeMenu(其中每个元素都有一个与之关联的复选框)的名单和其他Android应用程序。我也用ActionBarSherlock。我已经成功地写入程序在一个单一的主要活动,但我有困难搞清楚如何把这个原来的活动与我需要为每个选项卡中的两个新片段实例对应。此外,每个项目在第一标签加入时间(无论是从被检查过,或添加到文本框),在第二窗口中的列表应该认识到更新。

I am attempting to build an Android application that has two tabs, one for a textField/button and TreeMenu (where each element has a checkbox associated with it) and another for a list. I am also using an ActionBarSherlock. I have already successfully written the program in a single main activity, but am having difficulty figuring out how to divide this original activity to correspond with the two new fragment instances I need to create for each tab. Moreover, each time an item is added in the first tab (whether it is from being checked off or added to the textField), the list in the second window ought to recognize the update.

要建立行动起来吧,我可以做到这一点...

To build the action bar I can do this...

ActionBar actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setTitle("AppName");

要创建的标签。

 ActionBar.Tab Frag1Tab = actionbar.newTab().setText("InputTab");
 ActionBar.Tab Frag2Tab = actionbar.newTab().setText("ListTab");

要创建每个标签背后的碎片和听众...

To create the fragments and its listeners underlying each tab...

Fragment Fragment1 = new Fragment_1();
Fragment Fragment2 = new Fragment_2();
Frag1Tab.setTabListener(new MyTabsListener(Fragment1));
Frag2Tab.setTabListener(new MyTabsListener(Fragment2));

要它们添加到行动吧...

To add them to the action bar...

actionbar.addTab(Frag1Tab);
actionbar.addTab(Frag2Tab);

这一切都出现在我的MainActivity内。我想,例如,一个ArrayList变量可以访问这两个片段是这样,就像我说的,我可以更新列表。我很想听到任何帮助,您可以提供。我很想看看如何奥托API可以为这样的事情工作,但我不挑剔!

All of this appears within my MainActivity. I want, for example, an ArrayList variable to be accessible to both Fragments so, like I said, I can update the list. I would love to hear any help you can provide. I would be interested to see how the Otto API could work for something like this, but I am not picky!

推荐答案

一个单独的类可以帮助您解决问题。

A singleton class could help solve your problem.

public class GlobalApp {
    private static GlobalApp instance = new GlobalApp();

    private GlobalApp() {}

    public static GlobalApp getInstance() {
        return instance;
    }

    public ArrayList < ClassName > varName = new ArrayList < ClassName > ();

}

然后在你的类像这样使用。

Then use in your class like this..

GlobalApp.getInstance().varName

这篇关于交际Android上的片段之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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