Android 滑动布局:替换为片段

Android 滑动布局:替换为片段,android,android-fragments,material-design,pagerslidingtabstrip,Android,Android Fragments,Material Design,Pagerslidingtabstrip,我有以下情况: 线性布局 工具栏 滑动布局 寻呼机 框架布局 最初,我只向FrameLayout添加了一个片段,所有的工作都完成了。接下来,我删除这个片段,创建新的适配器,并将适配器绑定到SlidingTableOut。所有这些都再次起作用,但现在我需要将选项卡可见性设置为false,并使用 slidingTabLayout.setVisibility(View.GONE); 但我还需要用一个新片段替换当前视图。 通常情况下,我会使用以下方法: getFragmentManager()

我有以下情况:

  • 线性布局
    • 工具栏
    • 滑动布局
    • 寻呼机
    • 框架布局
最初,我只向FrameLayout添加了一个片段,所有的工作都完成了。接下来,我删除这个片段,创建新的适配器,并将适配器绑定到SlidingTableOut。所有这些都再次起作用,但现在我需要将选项卡可见性设置为false,并使用

slidingTabLayout.setVisibility(View.GONE);
但我还需要用一个新片段替换当前视图。 通常情况下,我会使用以下方法:

getFragmentManager().replace(parent, newFragment);
但现在我不能这么做,因为 getFragmentManager().replace(ViewPager,newFragment);
不起作用。那我该怎么做呢?如果可能的话,我可以在后台添加ViewPager吗?

我找到了一个解决方案。我对布局进行了以下更新:

  • 线性布局
    • 工具栏
    • 线性布局(id:parentScroll)
    • 滑动布局
    • 寻呼机
    • 框架布局(id:父级)
因此,当我需要隐藏所有选项卡和ViewPager时,为了在框架布局中膨胀新片段,我会执行以下操作:

 parentScroll.setVisibility(View.GONE);
 MyFragment myFragment= new MyFragment ();
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 transaction.add(R.id.parent, myFragment, "MYTAG");
 transaction.commit();
在这个新片段的反压中:

 MyFragment myFragment= (MyFragment) getSupportFragmentManager.findFragmentByTag("MYTAG");
 if (myFragment!= null && myFragment.isVisible()) {
     parentScroll.setVisibility(View.VISIBLE);
     getSupportFragmentManager().beginTransaction().remove(myFragment).commit();
 }