因此,我试图找出是否可行。我需要从选定的单个fragments加载两个activity到同一tab的两个容器中。

我发现了这个Android - Multiple fragments in ONE tab

但没人回答这个问题。

因此,这是代码示例以及我尝试过的内容。

听众

surveyTabListener = new TabListener<Load_Fragment>(this,
            R.id.header_fragment_container, Load_Fragment.class);


onTabSelected

public void onTabSelected(Tab tab, FragmentTransaction ft) {

        if (fragment == null) {
            String fragmentName = fragmentClass.getName();
            fragment = Fragment.instantiate(activity, fragmentName);
            ft.add(fragmentContainer, fragment, fragmentName);
        } else
            ft.attach(fragment);
}


现在,我尝试像这样在第二个listener的第二个inflate中添加第二个fragment

surveyTabListener2 = new TabListener<Store_Fragment>(this,
            R.id.store_fragment_container, Store_Fragment.class);


而且我也试图在if else中使用第二个onTabSelected语句,像这样

public void onTabSelected(Tab tab, FragmentTransaction ft) {

        if (fragment == null) {
            String fragmentName = fragmentClass.getName();
            fragment = Fragment.instantiate(activity, fragmentName);
            ft.add(fragmentContainer, fragment, fragmentName);
        } else
            ft.attach(fragment);

        if (fragment2 == null) {
            String fragmentName2 = fragmentClass2.getName();
            fragment2 = Fragment.instantiate(activity, fragmentName2);
            ft.add(fragmentContainer2, fragment2, fragmentName2);
        } else
            ft.attach(fragment2);

    }


都不起作用,我要么得到NPE (null pointer)要么什么都没有发生,但是我没有得到任何错误。

这似乎应该经常进行,但我似乎无法弄清楚。

最佳答案

您需要使用一个空片段作为两个片段的容器,我将发布一些代码向您展示如何对其进行处理。

那是片段容器:

public static class FragmentInsideContainer extends Fragment {

        ListView mainListView;
        ArrayAdapter<Producer> listAdapter;
        TextView inputSearch;
        static FragmentInsideContainer containerReference;
        String idProd;

        public FragmentInsideContainer() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.layoutcontaineprod, container, false);

            FragmentInsideContainer.containerReference = this;

            FragmentDetailProd fragmentDetailProd = new FragmentDetailProd();
            FragmentDetailProd.id = this.idProd;

            DummySectionFragment2.currentDetailProdFragment = fragmentDetailProd;

            FragmentListWine fragmentListWine = new FragmentListWine();
            fragmentListWine.id = this.idProd;

            DummySectionFragment2.currentListWineFragment = fragmentListWine;

            FragmentInsideContainer.containerReference.getChildFragmentManager().beginTransaction().replace(R.id.containerDetail, fragmentDetailProd).commit();
            FragmentInsideContainer.containerReference.getChildFragmentManager().executePendingTransactions();
            FragmentInsideContainer.containerReference.getChildFragmentManager().beginTransaction().add(R.id.containerDetail, fragmentListWine).commit();
            FragmentInsideContainer.containerReference.getChildFragmentManager().executePendingTransactions();

            return rootView;
        }

关于android - 一个ActionTab的多个 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16429134/

10-10 03:48