Android 在一个选项卡中替换片段

Android 在一个选项卡中替换片段,android,android-fragments,android-tabs,Android,Android Fragments,Android Tabs,我有两个标签。在其中一个选项卡中,有一个片段(比如fragment1),带有项目的网格视图。如果用户单击网格项,则需要显示新片段(比如fragment2),将旧片段添加到后堆栈中。但我在替换碎片上绊倒了。单击后,相同的片段1再次出现 activity_home.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="ht

我有两个标签。在其中一个选项卡中,有一个片段(比如fragment1),带有项目的网格视图。如果用户单击网格项,则需要显示新片段(比如fragment2),将旧片段添加到后堆栈中。但我在替换碎片上绊倒了。单击后,相同的片段1再次出现

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="in.brewtv.activities.HomeActivity">

    <android.support.design.widget.AppBarLayout
        ...>
            ...
    </android.support.design.widget.AppBarLayout>


    <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" />

</android.support.design.widget.CoordinatorLayout>
CategoriesGridDataAdapter.class:

CategoriesItemsRowHolder.view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Fragment newFragment = SingleCategoryFragment.newInstance(category);
        FragmentTransaction transaction = ((Activity) mContext).getFragmentManager().beginTransaction();
        transaction.replace(R.id.categories_recycler_view_layout, newFragment ); // give your fragment container id in first parameter
        transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
        transaction.commit();
    }
});
我做错了什么?我如何纠正这个问题

CategoriesItemsRowHolder.view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Fragment newFragment = SingleCategoryFragment.newInstance(category);
        FragmentTransaction transaction = ((Activity) mContext).getFragmentManager().beginTransaction();
        transaction.replace(R.id.categories_recycler_view_layout, newFragment ); // give your fragment container id in first parameter
        transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
        transaction.commit();
    }
});