从子片段访问父片段方法

Access Parent Fragment method from Child Fragment

我正在尝试从 Map 片段访问位于父片段 Tabs 中的 myfunction()

我尝试了以下行 Tabs parentFragment = (Tabs) getParentFragment(); 但是 parentFragment 为空。

我阅读了有关该主题的所有主题,但其中 none 对我有所帮助...(也许我误解了它们)。

我有以下文件:

我要从中调用 Parent 方法的 Fragment。

public class Map extends Fragment {

View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.map_fragment, container, false);
    Tabs parentFragment = (Tabs) getParentFragment();
    parentFragment.myfunction();
    // it crash because parentFragment is null...
    return rootView;
    }
}

包含 myfunction() 方法的父片段。

public class Tabs extends Fragment {
private View rootview;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    rootview = inflater.inflate(R.layout.tabs, container, false);

    TabLayout tabLayout = (TabLayout) rootview.findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) rootview.findViewById(R.id.pager);
    Context context = (HomeContainer)getContext();
    final TabsAdapter adapter = new TabsAdapter
            (getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(3);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    return rootview;
}

// the function I desire to call
public void myfunction(){
}

}

TabsAdapter(因为Map片段是一个Tab)。

public class TabsAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

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

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            Map tab1 = new Map();
            return tab1;
        case 1:
            Settings tab2 = new Settings();
            return tab2;
        case 2:
            Others tab3 = new Others();
            return tab3;
        default:
            return null;
    }
}

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

我对 Android 比较陌生,所以我有点迷茫! 预先感谢您的帮助。

onCreateView 可能太早了,尝试这样做 onViewCreated,或者在最坏的情况下 onStart

我建议您将 is 放在 onCreateView 之后并检查实例。

 Fragment parentFragment = getParentFragment();

    if(parentFragment instanceof Tab) {
        ((Tab) parentFragment).myfunction();
    }