1

所以我试图找出这是否可能。我需要将两个从一个被选中fragments的容器中加载到同一个容器中。activitytab

我发现了这个Android - 一个选项卡中的多个片段

但从来没有人回答过这个问题。

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

听众

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

现在我试着像这样listenerinflate第二个添加第二个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);

    }

两者都不起作用,我要么得到 aNPE (null pointer)要么完全没有任何反应,但我没有得到任何错误。

这似乎是应该经常做的事情,但我似乎无法弄清楚。

4

2 回答 2

2

您需要使用一个空片段作为两个片段的容器,我将发布一些代码向您展示我是如何编辑它的。

那是片段容器:

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;
        }
于 2013-05-07T21:53:43.213 回答
1

我突然意识到我的做法是错误的。我想出了一种方法来做我正在寻找的东西,而且相当简单。如果其他人有这个问题,这里就是解决方案。

public void onTabSelected(Tab tab, FragmentTransaction ft) {
        //get the name of the tab currently selected
        String name = (String) tab.getText().toString();

        // compare it to the tab you  would like to do something with        
        if (name == "Assets") {
            //load the 1st fragment
            fragment = new Store_Fragment();
            getFragmentManager().beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in,
                            android.R.animator.fade_out)
                    .replace(R.id.store_fragment_container, fragment).commit();

            //load the 2nd fragment
            fragment2 = new Load_Fragment();
            getFragmentManager().beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in,
                            android.R.animator.fade_out)
                    .replace(R.id.header_fragment_container, fragment2).commit();
        }

……就是这样。我希望这对其他人有帮助。

于 2013-05-07T22:56:58.190 回答