问题
I've the following situation:
- Linear Layout
- ToolBar
- SlidingTabLayout
- ViewPager
- FrameLayout
Initially, I only add a fragment to the FrameLayout, and all works. Next I remove this fragment, create new Adapter, and bind adapter to the SlidingTabLayout. All works again, but now I need to set tab visibility to false, and I do this with
slidingTabLayout.setVisibility(View.GONE);
but I also need to replace the current view with a new fragment. Usually I do this with:
getFragmentManager().replace(parent, newFragment);
but now I can't do this because getFragmentManager().replace(ViewPager, newFragment); doesn't works. So how can I do this? And if is possible, can I add the ViewPager replacing in the backstack?
回答1:
I find a solution. I made the following update to my layout:
- Linear Layout
- ToolBar
- Linear Layout (id: parentScroll)
- SlidingTabLayout
- ViewPager
- Frame Layout (id: parent)
So when I need to hide all the tabs and the ViewPager, in order to inflate a new Fragment in the Frame Layout, I do the following:
parentScroll.setVisibility(View.GONE);
MyFragment myFragment= new MyFragment ();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.parent, myFragment, "MYTAG");
transaction.commit();
In the onBackPressed of this new fragment:
MyFragment myFragment= (MyFragment) getSupportFragmentManager.findFragmentByTag("MYTAG");
if (myFragment!= null && myFragment.isVisible()) {
parentScroll.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().remove(myFragment).commit();
}
来源:https://stackoverflow.com/questions/30518710/slidingtablayout-replace-with-fragment