SlidingTabLayout: replace with fragment

喜欢而已 提交于 2019-12-11 02:28:52

问题


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)
      1. SlidingTabLayout
      2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!