Calling method inbetween Fragments which are in tabs

夙愿已清 提交于 2019-12-12 02:03:51

问题


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>();

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) {

    }


}

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

Thanks, Oli


回答1:


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.

Edit:

I will show some examples.

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

public interface FragmentBMethodsCaller{
    void callTheMethodInFragmentB();
}

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();

Good luck.



来源:https://stackoverflow.com/questions/14202662/calling-method-inbetween-fragments-which-are-in-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!