Java Android,碎片层次结构问题

Java Android,碎片层次结构问题,java,android,android-fragments,Java,Android,Android Fragments,我现在有一个片段,里面有一个视图分页器,它的三个孩子中的一个有一个可选择项的回收器视图,当选择一个项时,会显示另一个带有回收器视图的片段(它是独立的片段,不会显示在同一个视图分页器中) 我想做的是,用F1,1中的F2替换F1。调用F2.onCreateView()时,我得到以下错误: “java.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。” 为了向FragmentPagerAdapter提供FragmentMa

我现在有一个片段,里面有一个视图分页器,它的三个孩子中的一个有一个可选择项的回收器视图,当选择一个项时,会显示另一个带有回收器视图的片段(它是独立的片段,不会显示在同一个视图分页器中)

我想做的是,用F1,1中的F2替换F1。调用F2.onCreateView()时,我得到以下错误:

“java.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。”

为了向FragmentPagerAdapter提供FragmentManager,F1启动时会调用F1.getChildFragmentManger()

“当从F1,1中选择项目时”,我将遵循以下步骤:

1) 获取父片段

2) 从父片段获取FragmentManager

3) 用F2替换父片段(F1)

我怀疑问题在于我试图更改片段的方式,我也尝试过,总是从F1,1直接从活动中获取FragmentManager。我知道,也许这是一个非常糟糕的主意,但我不得不尝试一些东西

这是从“回收器”视图中选择项目时调用的F1,1方法

@Override
public void navigateToProductListView(Vendor vendor, Category category) {
    Bundle argument = new Bundle();
    argument.putParcelable(Vendor.TAG, vendor);
    argument.putParcelable(Category.TAG, category);

    FragmentManager fragmentManager = getParentFragment().getFragmentManager();

    ProductListFragment productListFragment = (ProductListFragment) fragmentManager.findFragmentByTag(ProductListFragment.TAG);

    if (productListFragment == null) {
        productListFragment = new ProductListFragment();
        Log.i("ProductListFragment", "was null");
    }

    productListFragment.setArguments(argument);
    Transitions.replaceFragment(fragmentManager, productListFragment, ProductListFragment.TAG, true);
}
以及引发异常的F2的onCreateView()

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

    mUnbinder = ButterKnife.bind(this, root);

    ListHelper.init(getActivity(), mRecyclerView);

    Log.i("ProductListFragment","onCreateView()");

    return root;
}
这是我的Transitions类的代码,我只使用一个活动,因此用于保存片段的FrameLayout始终是相同的

public static void replaceFragment(FragmentManager fragmentManager, Fragment fragment, String tag, boolean addToBackStack) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        Slide enterTransition = new Slide(Gravity.BOTTOM);
        enterTransition.setDuration(500);
        fragment.setEnterTransition(enterTransition);
        fragment.setAllowEnterTransitionOverlap(false);
    }

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment, tag);
    if (addToBackStack) {
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();
}

FragmentManager FragmentManager=getParentFragment().getFragmentManager()此语句为您提供父级私有FragmentManager,用于在此片段中放置和管理片段。

试试这个
FragmentManager FragmentManager=getParentFragment().getActivity().getFragmentManager()

通过回调方法,您可以从子片段切换父片段。不幸的是,它不起作用。您可以共享代码吗?我刚刚更新了我的问题是您的自定义类吗?您的replaceFragment方法中有什么?谢谢您的回答,很遗憾,我已经尝试直接从活动中获取片段管理器,但它不起作用。您确定所有这些片段都属于同一个活动吗?绝对是的,活动只有一个,而且总是相同的。F1和F2直接连接到活动,F1,1、F1,2、F1,3在F1中声明的视图寻呼机中,由F1的子片段管理器处理。我是个白痴,我解决了问题。问题不是由碎片引起的,所有邪恶的原因都是F2中的RecycleServiceAdapter。我忘了在onCreateViewHolder()方法中添加attachToRoot标志…抱歉,感谢您的耐心等待!!!