SlidingTabLayout:替换片段 [英] SlidingTabLayout: replace with fragment

查看:160
本文介绍了SlidingTabLayout:替换片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:


  • 线性布局结果

    • 工具栏

    • SlidingTabLayout

    • ViewPager

    • 的FrameLayout


起初,我只添加一个片段到的FrameLayout,和所有的作品。接下来,我删除此片段,创建新的适配器,并绑定适配器的SlidingTabLayout。所有再次工作,但现在我需要标签的可见性设置为false,而我这样做与

  slidingTabLayout.setVisibility(View.GONE);

但我也需要更换一个新的片段当前视图。
通常我这样做有:

  getFragmentManager()取代(父母,newFragment);

但现在我不能这样做,因为
    getFragmentManager()取代(ViewPager,newFragment)。
没有作品。那么,如何做到这一点?如果有可能,我可以添加ViewPager在backstack更换?


解决方案

我找到一个解决方案。我做了以下更新到我的布局:


  • 线性布局

    • 工具栏

    • 线性布局(ID:parentScroll)

      1. SlidingTabLayout

      2. ViewPager


    • 框架布局(ID:父)


所以,当我需要隐藏所有的标签和ViewPager,以膨胀的框架布局新的片段,我执行以下操作:

  parentScroll.setVisibility(View.GONE);
 MyFragment myFragment =新MyFragment();
 FragmentTransaction交易= getSupportFragmentManager()调用BeginTransaction()。
 transaction.add(R.id.parent,myFragmentMyTag的);
 器transaction.commit();

onBack pressed 这个新的片段:

  MyFragment myFragment =(MyFragment)getSupportFragmentManager.findFragmentByTag(MyTag的);
 如果(myFragment =空&放大器;!&放大器; myFragment.isVisible()){
     parentScroll.setVisibility(View.VISIBLE);
     。getSupportFragmentManager()调用BeginTransaction()删除(myFragment).commit()。
 }

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?

解决方案

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();
 }

这篇关于SlidingTabLayout:替换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆