通过活动调用片段功能 [英] Call fragment function over activity

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

问题描述

我有一个带有3个标签的TabLayout.它们是一个片段.

I have a TabLayout with 3 tab.They are a fragment.

Test.java-> PagerAdapter.java-> TabFragment1 + TabFragment2 + TabFragment3

Test.java->PagerAdapter.java->TabFragment1+TabFragment2+TabFragment3

我想调用TabFragment1.helloworld()函数.我怎么称呼这个功能?谢谢.

I want to call TabFragment1.helloworld() function. How can i call this function? Thanks.

Test.java在下面创建了3个标签

Test.java created 3 tabs in below

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

    tabLayout.addTab(tabLayout.newTab().setText("tab1"));

    tabLayout.addTab(tabLayout.newTab().setText("tab2"));                        

    tabLayout.addTab(tabLayout.newTab().setText("tab3"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    viewPager.setOffscreenPageLimit(3);


final PagerAdapter adapter = new PagerAdapter
                                    (getSupportFragmentManager(), 
tabLayout.getTabCount());

viewPager.setAdapter(adapter);

PagerAdapter.java

PagerAdapter.java

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TabFragment1 tab1 = new TabFragment1();
                return tab1;
            case 1:
                TabFragment2 tab2 = new TabFragment2();
                return tab2;
            case 2:
                TabFragment3 tab3 = new TabFragment3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

推荐答案

对于ViewPager来说,它比通常要棘手一些,因为您不能使用首选的findFragmentByTag()方法.您可以尝试以下操作-未经测试,但应该可以:

It's a bit trickier than usual with ViewPagers, because you can't use the preferred findFragmentByTag() method. Here's something you could try - it's untested, but should be ok:

  1. 创建一个接口,您的ViewPager中的所有Fragment都将实现该接口:

  1. Create an interface, which all Fragments in your ViewPager implement:

public interface ViewPagerFragmentBase {

    void callCommonMethod(int code);

}

  • 在您所有的ViewPager Fragment s中实现它:

  • Implement it in all your ViewPager Fragments:

    public FragmentOne implements ViewPagerFragmentBase {
    
        @Override
        public void callCommonMethod(int code) {
    
            if (code == 1) {
                //Run code in this Fragment
            }
    
        }
    
    public FragmentTwo implements ViewPagerFragmentBase {
    
        @Override
        public void callCommonMethod(int code) {
    
            if (code == 2) {
                //Run code in this Fragment
            }
    
        }
    

  • 在您的Activity类中,获取当前在Adapter中的所有Fragment,然后循环浏览每个Fragment,调用callCommonMethod()以及指示您正在尝试的Fragment的代码达到:

  • Inside your Activity class, grab all Fragments currently in your Adapter, then cycle through each one, calling the callCommonMethod() along with a code which indicates the Fragment you are trying to reach:

    public class MainActivity extends AppCompatActivity {
    
        private void callCommonMethodInFragments(int code) {
    
            for (int i = 0; i < viewPagerAdapter.getCount(); i++) {
                ((ViewPagerFragmentBase)viewPagerAdapter.getItem(i)).callCommonMethod(code);
            }
    
        }
    
        //To call it...
        callCommonMethodInFragments(1); //This will run the method in FragmentOne only
    
    }
    

  • 如果代码与您尝试访问的Fragment中的代码匹配,则该方法将运行.

    If the code matches the one in the Fragment you are trying to reach, then the method will run.

    您还可以尝试使用EventBus-这将使您可以更直接地实现目标,而无需输入太多内容: https://github.com/greenrobot/EventBus

    Something else you could also try though is an EventBus - this would allow you to achieve your goal more directly, and with less typing: https://github.com/greenrobot/EventBus

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

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