TabHost(从片段隐藏Tabhost)

时间:2015-12-18 09:44:51

标签: android android-fragments android-studio tabs

enter image description here我有一个tabhost,它位于我的主要活动(导航抽屉)内 在每个标签中我都有ViewPager ViewPager由不同的碎片组成 在那些碎片中,我必须隐藏/显示我的TabHost 但我不知道如何将TabHost从我的Activity传递给片段。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    tabHost = (TabHost) findViewById(R.id.tabHost);
    tabHost.setup();
    tabHost.setOnTabChangedListener(this);
    setNewTab(this, tabHost, "tab1", R.string.textTabTitle1, R.drawable.icon_search, R.id.tab1);
    setNewTab(this, tabHost, "tab2", R.string.textTabTitle1, R.drawable.icon_comment, R.id.tab2);
    setNewTab(this, tabHost, "tab3", R.string.textTabTitle1, R.drawable.icon_car, R.id.tab3);


    final ViewPager pagerComment = (ViewPager) findViewById(R.id.pagerComment);
    pagerComment.setAdapter(new AdapterTabComment(getSupportFragmentManager()));

    final ViewPager pagerSearch = (ViewPager) findViewById(R.id.pagerSearch);
    pagerSearch.setAdapter(new AdapterTabSearch(getSupportFragmentManager()));

    tabsComment = (PagerSlidingTabStrip) findViewById(R.id.tabsComment);
    tabsComment.setViewPager(pagerComment);
    setTextColor(0, tabsComment);

    tabsSearch = (PagerSlidingTabStrip) findViewById(R.id.tabsSearch);
    tabsSearch.setViewPager(pagerSearch);
    setTextColor(0, tabsSearch);

    pagerComment.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            setTextColor(position, tabsComment);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });


    pagerSearch.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            setTextColor(position,tabsSearch);
        }

        @Override
        public void onPageScrollStateChanged(int state) {}});
    pagerSearch.setCurrentItem(0);
}

这甚至可能吗?
谢谢..

2 个答案:

答案 0 :(得分:2)

您可以声明TabHost静态,您可以在片段中访问。我在我的项目中使用了它。

答案 1 :(得分:0)

我正确地提出你的问题,但我会按照我的理解回答这个问题

当你调用Fragment faragment = new Fragment(); 将一些键传递给该fragmet作为参数,以便在此基础上隐藏或显示片段中的tabhosts。如果我错了抱歉..