调用方法其间片段,其在标签 [英] Calling method inbetween Fragments which are in tabs

查看:105
本文介绍了调用方法其间片段,其在标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有哪些都组织在选项卡中的几个片段,我想片段来调用FRAG B.所以我知道我需要调用,然后调用B中的功能活性的方法,我看到有人提使用

I currently have a few fragments which are all organised in tabs and i want fragment A to call a method in frag B. So I know I need to call the activity which then calls the function in B, I have seen people mentioning to use

ExampleFragment fragment = (ExampleFragment)  
 getFragmentManager().findFragmentById(R.id.example_fragment);


fragment.<specific_function_name>();

我有,虽然我的片段添加了一个选项卡监听器不是fragmentmanager一个问题,所以我不知道如何使用它来调用一个方法。下面是我的标签添加。

I am having a problem though as my fragments are added with a tab listener not a fragmentmanager so I have no idea how to use this to call a method. Here is how my tabs are added.

public class fifaActivity extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    final ActionBar bar = getActionBar();
    bar.setTitle(TourneyName);
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setIcon(R.drawable.fifa);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        bar.addTab(bar
            .newTab()
            .setText("Tables")
            .setTabListener(
                    new TabListener<fifa.tables>(this, "tables",
                            fifa.tables.class)));

        bar.addTab(bar
            .newTab()
            .setText("Knockouts")
            .setTabListener(
                    new TabListener<fifa.knockouts>(this, "tables",
                            fifa.knockouts.class)));




    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex())

}

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        this(activity, tag, clz, null);
    }

    public TabListener(Activity activity, String tag, Class<T> clz,
            Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager()
                    .beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        }
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                    mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }


}

这是如何调用从fifa.tables片段到fifa.knockouts片段的方法将是巨大的AP preciated任何帮助

Any help on how to call a method from the fifa.tables fragment to the fifa.knockouts fragment will be hugely appreciated.

谢谢,奥利

推荐答案

您可以只创建将由主办的活动来实现的接口。 该片段A将持有引用活动为这个接口实例,当它要调用片段B的作用,它会调用的接口功能。 在主办活动的接口实现,该活动将打了一个电话到片段B的功能。

You can just create an interface the will be implemented by the hosting Activity. The Fragment A will hold reference to the Activity as this interface instance, and when it wants to call the function in Fragment B it will call the function in the interface. In the interface implementation in the hosting Activity, the Activity will made a call to the function in Fragment B.

编辑:

我将展示一些例子。

您需要创建一个片段A将用于调用方法'片段B',一个接口,例如:

You need to create an interface that 'Fragment A' will use to call method in 'Fragment B', for example:

public interface FragmentBMethodsCaller{
    void callTheMethodInFragmentB();
}

现在,你需要实现它。比方说,你的活动就叫做HostActivity:

Now, you need to implement it. Let's say your activity is called HostActivity:

public class HostActivity extends Activity implements FragmentBMethodsCaller{
    ...
    public void callTheMethodInFragmentB(){
         --Implementation--
    }
    ...
}

现在,您需要一个片段里面称之为最后一件事情:

Now, last thing you need to call it inside Fragment A:

FragmentBMethodsCaller fbmc = (FragmentBMethodsCaller)getActivity();
fbmc.callTheMethodInFragmentB();

祝你好运。

这篇关于调用方法其间片段,其在标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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