Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为我的三个选项卡式布局动态创建片段_Android_Android Layout_Android Fragments - Fatal编程技术网

Android 如何为我的三个选项卡式布局动态创建片段

Android 如何为我的三个选项卡式布局动态创建片段,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我有一个三个选项卡的选项卡式布局。我想用相应按钮上的新片段替换其中的片段,单击每个旧片段。我的想法是,为了做到这一点,我必须包含一个容器(最好是framelayout),我可以替换它来显示新的片段。我是这样做的: 活动xml <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:lay

我有一个三个选项卡的选项卡式布局。我想用相应按钮上的新片段替换其中的片段,单击每个旧片段。我的想法是,为了做到这一点,我必须包含一个容器(最好是framelayout),我可以替换它来显示新的片段。我是这样做的: 活动xml

<android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment"
        android:layout_below="@id/appbar"
        >
        <fragment
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/tabA"
            android:name="com.moodoff.Moods"
            tools:layout="@layout/fragment_moods" />
    </FrameLayout>
现在我要做的是从我的原始片段按钮单击我在同一个容器中加载新片段,它工作正常。但问题是在第二个和第三个选项卡中,显示了相同的片段,而我在其他两个片段中没有看到其他两个选项卡。
如何解决此问题?

您只是一个布局和一个片段,您想替换所有三个布局,然后制作3个布局,以替换其他两个选项卡,您如何加载这些片段?我没有了解你的代码的全部结构(例如,单击以加载片段)。youzking:忘了这一点,你能建议我如何将三个片段放在三个选项卡中吗?我可以随时用新的碎片替换它们。
 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_all_tabs, container, false);

            Log.e("ALLTABS","I am called again..");

            /*mViewPager.setCurrentItem(Start.switchToTab);
            mViewPager.getAdapter().notifyDataSetChanged();*/

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            Fragment newFragment = Moods.newInstance("replacedA","b");
            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack if needed
            //transaction.replace(R.id.allmoods, newFragment);
            transaction.replace(R.id.fragment, newFragment);
            transaction.addToBackStack("mainA");
            //transaction.commitAllowingStateLoss();

            return rootView;
        }