Android - 使用ViewPager和FragmentStatePagerAdapter为ActionBar创建多个选项卡级别

时间:2014-07-03 15:11:49

标签: android tabs android-actionbar android-viewpager fragmentmanager

如何在ActionBar中处理Android选项卡,以便向用户显示多个选项卡和相关片段的上下文级别(换句话说,我希望ActionBar选项卡根据选项中的选项从一个级别更改为另一个级别更高级别 - 有点像分级菜单)。 (理想情况下,解决方案会支持任意数量的“级别”,但至少有三个“级别”会很好)

注意:为了正确排序答案,请按“最旧”排序 - 这会将部件放在HomeActivity的最后位置。

3 个答案:

答案 0 :(得分:0)

这是我的一个名为ABTabsAdapter类的制表符类的示例,它使用FragmentStatePagerAdapter,ViewPager和ActionBar显示和管理最多三个虚拟级别的制表符及相关片段,所有这些都与Tabs和Fragments的集合一起使用"胶合& #34;与TabInfo结构一起使用。此解决方案与StackOverflow和Android支持文献中发布的其他解决方案拼凑在一起。关键是使用FragmentStatePagerAdapter(不是像Android中所述的FragmentPagerAdapter和默认的Android项目模板中所示)。另外,请注意使用bIsSet布尔变量,该变量通过addTab函数显示在插入时选择的选项卡。最后,请注意getItemPosition函数也可以返回片段/视图/选项卡的实际位置,但这对FragmentStatePagerAdapter的性能几乎没有影响,当它只是为所有人返回POSITION_NONE时更容易阅读/使用请求的项目(事实上,这是当前标签更改位置时更新ViewPager的建议方法 - 在此实现中,标签不仅会更改位置,还会根据上下文"删除和重新添加标签。选项卡菜单的级别"。(第1部分)

public class ABTabsAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener
{
    ActionBar.Tab tabLevelHome = null;
    ActionBar.Tab tabLevel1_2 = null;
    ActionBar.Tab tabLevel1_3 = null;
    ActionBar.Tab tabLevel1_4 = null;
    ActionBar.Tab tabLevel1_2_1 = null;
    ActionBar.Tab tabLevel1_2_2 = null;
    ActionBar.Tab tabLevel1_2_3 = null;
    ActionBar.Tab tabLevel1_3_1 = null;
    ActionBar.Tab tabLevel1_3_1_1 = null;
    ActionBar.Tab tabLevel1_3_2 = null;
    ActionBar.Tab tabLevel1_3_2_1 = null;
    ActionBar.Tab tabLevel1_3_3 = null;
    ActionBar.Tab tabLevel1_3_3_1 = null;
    ActionBar.Tab tabLevel1_4_1 = null;
    ActionBar.Tab tabLevel1_4_2 = null;

    TabInfo infoLevelHome = null;
    TabInfo infoLevel1_2 = null;
    TabInfo infoLevel1_3 = null;
    TabInfo infoLevel1_4 = null;
    TabInfo infoLevel1_2_1 = null;
    TabInfo infoLevel1_2_2 = null;
    TabInfo infoLevel1_2_3 = null;
    TabInfo infoLevel1_3_1 = null;
    TabInfo infoLevel1_3_1_1 = null;
    TabInfo infoLevel1_3_2 = null;
    TabInfo infoLevel1_3_2_1 = null;
    TabInfo infoLevel1_3_3 = null;
    TabInfo infoLevel1_3_3_1 = null;
    TabInfo infoLevel1_4_1 = null;
    TabInfo infoLevel1_4_2 = null;

    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    Boolean bIsSet = false;

    static final class TabInfo
    {
        private final Class<?> clss;

        TabInfo(Class<?> _class)
        {
            clss = _class;
        }
    }

    public ABTabsAdapter(HomeActivity homeActivity, FragmentManager fragmentManager, ViewPager viewPager, ActionBar actionBar)
    {
        super(fragmentManager);
        mContext = homeActivity;
        mActionBar = actionBar;
        mViewPager = viewPager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
        initializeTabs();
        bIsSet = true;
        selectLayout(0);
    }

    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int nPos)
    {
        return Fragment.instantiate(mContext, getSelectedTab(nPos).clss.getName(), null);
    }

    @Override
    public int getItemPosition(Object objGet)
    {
        return POSITION_NONE;
    }

    @Override
    public void onPageScrolled(int nPos, float fOffset, int nOffsetPixels)
    {
    }

    @Override
    public void onPageSelected(int nPos)
    {
        mActionBar.setSelectedNavigationItem(nPos);
    }

    @Override
    public void onPageScrollStateChanged(int nState)
    {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tabSelected, FragmentTransaction fragmentTransaction)
    {
        TabInfo infoGet = (TabInfo)tabSelected.getTag();

        selectLayout(tabSelected.getPosition());

        for (int nCount=0; nCount < mTabs.size(); nCount++)
        {
            if (mTabs.get(nCount).clss.getName() == infoGet.clss.getName())
            {
                mViewPager.setCurrentItem(nCount);
            }
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
    {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
    {
    }

    public void addTab(ActionBar.Tab tabAdd, int nPos)
    {
        try
        {
            if(nPos < 0)
            {
                mTabs.add((TabInfo)tabAdd.getTag());
                mActionBar.addTab(tabAdd);
                notifyDataSetChanged();
            }
            else
            {
                mTabs.add(nPos,(TabInfo)tabAdd.getTag());
                mActionBar.addTab(tabAdd, nPos);
                notifyDataSetChanged();
            }
        }
        catch(Exception e)
        {
            appContext.showMessage("Error","[addTab]: "+e.toString());
        }
    }

    public void removeTab(ActionBar.Tab tabRemove)
    {
        try
        {
            Object tag = tabRemove.getTag();

            for (int nCount=0; nCount < mTabs.size(); nCount++)
            {
                if (mTabs.get(nCount) == tag)
                {
                    mTabs.remove(nCount);
                    mActionBar.removeTab(tabRemove);
                    notifyDataSetChanged();
                    break;
                }
            }
        }
        catch(Exception e)
        {
            appContext.showMessage("Error","[removeTab]: "+e.toString());
        }
    }

    public void initializeTabs()
    {
        try
        {
            tabLevelHome = mActionBar.newTab();
            if(tabLevelHome != null)
            {
                tabLevelHome.setText(" Home");
                tabLevelHome.setIcon(R.drawable.levelhome_44x29);
                tabLevelHome.setTabListener(this);

                infoLevelHome = new TabInfo(HomeActivity.HomeFragment.class);
                tabLevelHome.setTag(infoLevelHome);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Home tab is not set.");

            tabLevel1_2 = mActionBar.newTab();
            if(tabLevel1_2 != null)
            {
                tabLevel1_2.setText(" Level1_2");
                tabLevel1_2.setIcon(R.drawable.level1_2_44x29);
                tabLevel1_2.setTabListener(this);

                infoLevel1_2 = new TabInfo(Level1_2Fragment.class);
                tabLevel1_2.setTag(infoLevel1_2);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_2 tab is not set.");

            tabLevel1_3 = mActionBar.newTab();
            if(tabLevel1_3 != null)
            {
                tabLevel1_3.setText(" Level1_3");
                tabLevel1_3.setIcon(R.drawable.level1_3_44x29);
                tabLevel1_3.setTabListener(this);

                infoLevel1_3 = new TabInfo(Level1_3Fragment.class);
                tabLevel1_3.setTag(infoLevel1_3);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3 tab is not set.");

            tabLevel1_4 = mActionBar.newTab();
            if(tabLevel1_4 != null)
            {
                tabLevel1_4.setText(" Level1_4");
                tabLevel1_4.setIcon(R.drawable.level1_4_44x29);
                tabLevel1_4.setTabListener(this);

                infoLevel1_4 = new TabInfo(Level1_4Fragment.class);
                tabLevel1_4.setTag(infoLevel1_4);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_4 tab is not set.");

            tabLevel1_2_1 = mActionBar.newTab();
            if(tabLevel1_2_1 != null)
            {
                tabLevel1_2_1.setText(" Level1_2_1");
                tabLevel1_2_1.setIcon(R.drawable.level1_2_1_44x29);
                tabLevel1_2_1.setTabListener(this);

                infoLevel1_2_1 = new TabInfo(Level1_2_1Fragment.class);
                tabLevel1_2_1.setTag(infoLevel1_2_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_2_1 tab is not set.");

            tabLevel1_2_2 = mActionBar.newTab();
            if(tabLevel1_2_2 != null)
            {
                tabLevel1_2_2.setText(" Level1_2_2");
                tabLevel1_2_2.setIcon(R.drawable.level1_2_2_44x29);
                tabLevel1_2_2.setTabListener(this);

                infoLevel1_2_2 = new TabInfo(Level1_2_2Fragment.class);
                tabLevel1_2_2.setTag(infoLevel1_2_2);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_2_2 tab is not set.");

            tabLevel1_2_3 = mActionBar.newTab();
            if(tabLevel1_2_3 != null)
            {
                tabLevel1_2_3.setText(" Level1_2_3");
                tabLevel1_2_3.setIcon(R.drawable.level1_2_3_44x29);
                tabLevel1_2_3.setTabListener(this);

                infoLevel1_2_3 = new TabInfo(Level1_2_3Fragment.class);
                tabLevel1_2_3.setTag(infoLevel1_2_3);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_2_3 tab is not set.");

            tabLevel1_3_1 = mActionBar.newTab();
            if(tabLevel1_3_1 != null)
            {
                tabLevel1_3_1.setText(" Level1_3_1");
                tabLevel1_3_1.setIcon(R.drawable.level1_3_1_44x29);
                tabLevel1_3_1.setTabListener(this);

                infoLevel1_3_1 = new TabInfo(Level1_3_1Fragment.class);
                tabLevel1_3_1.setTag(infoLevel1_3_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_1 tab is not set.");

            tabLevel1_3_1_1 = mActionBar.newTab();
            if(tabLevel1_3_1_1 != null)
            {
                tabLevel1_3_1_1.setText(" Level1_3_1_1");
                tabLevel1_3_1_1.setIcon(R.drawable.level1_3_1_1_44x29);
                tabLevel1_3_1_1.setTabListener(this);

                infoLevel1_3_1_1 = new TabInfo(Level1_3_1_1Fragment.class);
                tabLevel1_3_1_1.setTag(infoLevel1_3_1_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_1_1 tab is not set.");

            tabLevel1_3_2 = mActionBar.newTab();
            if(tabLevel1_3_2 != null)
            {
                tabLevel1_3_2.setText(" Level1_3_2");
                tabLevel1_3_2.setIcon(R.drawable.level1_3_2_44x29);
                tabLevel1_3_2.setTabListener(this);

                infoLevel1_3_2 = new TabInfo(Level1_3_2Fragment.class);
                tabLevel1_3_2.setTag(infoLevel1_3_2);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_2 tab is not set.");

            tabLevel1_3_2_1 = mActionBar.newTab();
            if(tabLevel1_3_2_1 != null)
            {
                tabLevel1_3_2_1.setText(" Level1_3_2_1");
                tabLevel1_3_2_1.setIcon(R.drawable.level1_3_2_1_44x29);
                tabLevel1_3_2_1.setTabListener(this);

                infoLevel1_3_2_1 = new TabInfo(Level1_3_2_1Fragment.class);
                tabLevel1_3_2_1.setTag(infoLevel1_3_2_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_2_1 tab is not set.");

            tabLevel1_3_3 = mActionBar.newTab();
            if(tabLevel1_3_3 != null)
            {
                tabLevel1_3_3.setText(" Level1_3_3");
                tabLevel1_3_3.setIcon(R.drawable.level1_3_3_44x29);
                tabLevel1_3_3.setTabListener(this);

                infoLevel1_3_3 = new TabInfo(Level1_3_3Fragment.class);
                tabLevel1_3_3.setTag(infoLevel1_3_3);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_3 tab is not set.");

            tabLevel1_3_3_1 = mActionBar.newTab();
            if(tabLevel1_3_3_1 != null)
            {
                tabLevel1_3_3_1.setText(" Level1_3_3_1");
                tabLevel1_3_3_1.setIcon(R.drawable.level1_3_3_1_44x29);
                tabLevel1_3_3_1.setTabListener(this);

                infoLevel1_3_3_1 = new TabInfo(Level1_3_3_1Fragment.class);
                tabLevel1_3_3_1.setTag(infoLevel1_3_3_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_3_3_1 tab is not set.");

            tabLevel1_4_1 = mActionBar.newTab();
            if(tabLevel1_4_1 != null)
            {
                tabLevel1_4_1.setText(" Level1_4_1");
                tabLevel1_4_1.setIcon(R.drawable.level1_4_1_44x29);
                tabLevel1_4_1.setTabListener(this);

                infoLevel1_4_1 = new TabInfo(Level1_4_1Fragment.class);
                tabLevel1_4_1.setTag(infoLevel1_4_1);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_4_1 tab is not set.");

            tabLevel1_4_2 = mActionBar.newTab();
            if(tabLevel1_4_2 != null)
            {
                tabLevel1_4_2.setText(" Level1_4_2");
                tabLevel1_4_2.setIcon(R.drawable.level1_4_2_44x29);
                tabLevel1_4_2.setTabListener(this);

                infoLevel1_4_2 = new TabInfo(Level1_4_2Fragment.class);
                tabLevel1_4_2.setTag(infoLevel1_4_2);
            }
            else
                appContext.showMessage("Error","[initializeTabs] Level1_4_2 tab is not set.");
        }
        catch(Exception e)
        {
            appContext.showMessage("Error","[initializeTabs]: "+e.toString());
        }
    }

    public void selectLayout(int nPos)
    {
        try
        {
            if(bIsSet)
            {
                bIsSet = false;

                if(appContext.nCurrentLayoutID == 0)
                    showLayoutHome();
                else if(appContext.nCurrentLayoutID == R.layout.fragment_levelhome)
                {
                    if(nPos == 0)
                        appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                    else if(nPos == 1)
                        showLayoutLevel1_2();
                    else if(nPos == 2)
                        showLayoutLevel1_3();
                    else if(nPos == 3)
                        showLayoutLevel1_4();
                }
                else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2)
                {
                    if(nPos == 0)
                        showLayoutHome();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_2;
                    else if(nPos == 2)
                        showLayoutLevel1_2_1();
                    else if(nPos == 3)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_4_2;
                }
                else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3)
                {
                    if (nPos == 0)
                        showLayoutHome();
                    else if (nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3;
                    else if (nPos == 2)
                        showLayoutLevel1_3_1();
                    else if (nPos == 3)
                        showLayoutLevel1_3_2();
                    else if (nPos == 4)
                        showLayoutLevel1_3_3();
                }
                else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_4) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4_1))
                {
                    if (nPos == 0)
                        showLayoutHome();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_4;
                    else if(nPos == 2)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_4_1;
                    else if(nPos == 3)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_4_2;
                }
                else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1_1))
                {
                    if (nPos == 0)
                        showLayoutLevel1_3();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_1;
                    else if(nPos == 2)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_1_1;
                }
                else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2_1))
                {
                    if (nPos == 0)
                        showLayoutLevel1_3();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_2;
                    else if(nPos == 2)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_2_1;
                }
                else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3_1))
                {
                    if (nPos == 0)
                        showLayoutLevel1_3();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_3;
                    else if(nPos == 2)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_3_3_1;
                }
                else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_2_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_2_2) ||(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_3))
                {
                    if (nPos == 0)
                        showLayoutLevel1_2();
                    else if(nPos == 1)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_2_1;
                    else if(nPos == 2)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_2_2;
                    else if(nPos == 3)
                        appContext.nCurrentLayoutID = R.layout.fragment_level1_2_3;
                }
                else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4_2)
                {
                    if(mActionBar.getTabAt(1).getText() == " Level1_2")
                    {
                        if(nPos == 0)
                            showLayoutHome();
                        else if(nPos == 1)
                            appContext.nCurrentLayoutID = R.layout.fragment_level1_2;
                        else if(nPos == 2)
                            showLayoutLevel1_2_1();
                        else if(nPos == 3)
                            appContext.nCurrentLayoutID = R.layout.fragment_level1_4_2;
                    }
                    else
                    {
                        if (nPos == 0)
                            showLayoutHome();
                        else if(nPos == 1)
                            appContext.nCurrentLayoutID = R.layout.fragment_level1_4;
                        else if(nPos == 2)
                            appContext.nCurrentLayoutID = R.layout.fragment_level1_4_1;
                        else if(nPos == 3)
                            appContext.nCurrentLayoutID = R.layout.fragment_level1_4_2;
                    }
                }
                updateActionBar();

                bIsSet = true;
            }
        }
        catch(Exception e)
        {
            appContext.showMessage("Error","[selectLayout]: "+e.toString());
        }
    }

    public TabInfo getSelectedTab(int nPos)
    {
        TabInfo infoGet = null;

        try
        {
            if(appContext.nCurrentLayoutID == 0)
                infoGet = (TabInfo)tabLevelHome.getTag();
            else if(appContext.nCurrentLayoutID == R.layout.fragment_levelhome)
            {
                if(nPos == 0)
                    infoGet = (TabInfo)tabLevelHome.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_2.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_3.getTag();
                else if(nPos == 3)
                    infoGet = (TabInfo)tabLevel1_4.getTag();
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2)
            {
                if(nPos == 0)
                    infoGet = (TabInfo)tabLevelHome.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_2.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_2_1.getTag();
                else if(nPos == 3)
                    infoGet = (TabInfo)tabLevel1_4_2.getTag();
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3)
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevelHome.getTag();
                else if (nPos == 1)
                    infoGet = (TabInfo)tabLevel1_3.getTag();
                else if (nPos == 2)
                    infoGet = (TabInfo)tabLevel1_3_1.getTag();
                else if (nPos == 3)
                    infoGet = (TabInfo)tabLevel1_3_2.getTag();
                else if (nPos == 4)
                    infoGet = (TabInfo)tabLevel1_3_3.getTag();
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_4) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4_1))
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevelHome.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_4.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_4_1.getTag();
                else if(nPos == 3)
                    infoGet = (TabInfo)tabLevel1_4_2.getTag();
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1_1))
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevel1_3.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_3_1.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_3_1_1.getTag();
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2_1))
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevel1_3.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_3_2.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_3_2_1.getTag();
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3_1))
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevel1_3.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_3_3.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_3_3_1.getTag();
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_2_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_2_2) ||(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_3))
            {
                if (nPos == 0)
                    infoGet = (TabInfo)tabLevel1_2.getTag();
                else if(nPos == 1)
                    infoGet = (TabInfo)tabLevel1_2_1.getTag();
                else if(nPos == 2)
                    infoGet = (TabInfo)tabLevel1_2_2.getTag();
                else if(nPos == 3)
                    infoGet = (TabInfo)tabLevel1_2_3.getTag();
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4_2)
            {
                if(mActionBar.getTabAt(1).getText() == " Level1_2")
                {
                    if(nPos == 0)
                        infoGet = (TabInfo)tabLevelHome.getTag();
                    else if(nPos == 1)
                        infoGet = (TabInfo)tabLevel1_2.getTag();
                    else if(nPos == 2)
                        infoGet = (TabInfo)tabLevel1_2_1.getTag();
                    else if(nPos == 3)
                        infoGet = (TabInfo)tabLevel1_4_2.getTag();
                }
                else
                {
                    if (nPos == 0)
                        infoGet = (TabInfo)tabLevelHome.getTag();
                    else if(nPos == 1)
                        infoGet = (TabInfo)tabLevel1_4.getTag();
                    else if(nPos == 2)
                        infoGet = (TabInfo)tabLevel1_4_1.getTag();
                    else if(nPos == 3)
                        infoGet = (TabInfo)tabLevel1_4_2.getTag();
                }
            }
        }
        catch(Exception e)
        {
            appContext.showMessage("Error","[getSelectedTab]: "+e.toString());
        }

        return infoGet;
    }

答案 1 :(得分:0)

第2部分......

public void selectUpLayout()
{
    try
    {
        if((appContext.nCurrentLayoutID == R.layout.fragment_levelhome) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_2) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4_2))
            showLayoutHome();
        else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3_1))
            showLayoutLevel1_3();
        else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_2_1) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_2_2) ||(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_3))
            showLayoutLevel1_2();
        updateActionBar();
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[selectUpLayout]: "+e.toString());
    }
}

private void updateActionBar()
{
    try
    {
        if(mActionBar != null)
        {
            if(appContext.nCurrentLayoutID == R.layout.fragment_levelhome)
            {
                mActionBar.setTitle("levelhome");
                mActionBar.setSubtitle("levelhome");
                mActionBar.setHomeButtonEnabled(false);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2)
            {
                mActionBar.setTitle("fragment_level1_2");
                mActionBar.setSubtitle("fragment_level1_2");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3)
            {
                mActionBar.setTitle("fragment_level1_3");
                mActionBar.setSubtitle("fragment_level1_3");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4)
            {
                mActionBar.setTitle("fragment_level1_4");
                mActionBar.setSubtitle("fragment_level1_4");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_1)
            {
                mActionBar.setTitle("Level1_2_1");
                mActionBar.setSubtitle("Level1_2_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_2)
            {
                mActionBar.setTitle("Level1_2_2");
                mActionBar.setSubtitle("Level1_2_2");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2_3)
            {
                mActionBar.setTitle("Level1_2_3");
                mActionBar.setSubtitle("Level1_2_3");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1)
            {
                mActionBar.setTitle("Level1_3_1");
                mActionBar.setSubtitle("Level1_3_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_1_1)
            {
                mActionBar.setTitle("Level1_3_1_1");
                mActionBar.setSubtitle("Level1_3_1_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2)
            {
                mActionBar.setTitle("Level1_3_2");
                mActionBar.setSubtitle("Level1_3_2");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_2_1)
            {
                mActionBar.setTitle("Level1_3_2_1");
                mActionBar.setSubtitle("Level1_3_2_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3)
            {
                mActionBar.setTitle("Level1_3_3");
                mActionBar.setSubtitle("Level1_3_3");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3_3_1)
            {
                mActionBar.setTitle("Level1_3_3_1");
                mActionBar.setSubtitle("Level1_3_3_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4_1)
            {
                mActionBar.setTitle("Level1_4_1");
                mActionBar.setSubtitle("Level1_4_1");
                mActionBar.setHomeButtonEnabled(true);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4_2)
            {
                mActionBar.setTitle("Level1_4_2");
                mActionBar.setSubtitle("Level1_4_2");
                mActionBar.setHomeButtonEnabled(true);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[AddTab]: "+e.toString());
    }
}

private void showLayoutHome()
{
    try
    {
        if(mActionBar != null)
        {
            if(appContext.nCurrentLayoutID == 0)
            {
                appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                addTab(tabLevelHome,-1);
                addTab(tabLevel1_2, -1);
                addTab(tabLevel1_3,-1);
                addTab(tabLevel1_4, -1);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_2)
            {
                appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(2));

                addTab(tabLevel1_3,-1);
                addTab(tabLevel1_4, -1);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_3)
            {
                appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                removeTab(mActionBar.getTabAt(4));
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(2));
                removeTab(mActionBar.getTabAt(1));

                addTab(tabLevel1_2, -1);
                addTab(tabLevel1_3,-1);
                addTab(tabLevel1_4, -1);
            }
            else if((appContext.nCurrentLayoutID == R.layout.fragment_level1_4) || (appContext.nCurrentLayoutID == R.layout.fragment_level1_4_1))
            {
                appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(2));

                addTab(tabLevel1_2,1);
                addTab(tabLevel1_3, 2);
            }
            else if(appContext.nCurrentLayoutID == R.layout.fragment_level1_4_2)
            {
                if(mActionBar.getTabAt(1).getText() == " Level1_2")
                {
                    appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                    removeTab(mActionBar.getTabAt(3));
                    removeTab(mActionBar.getTabAt(2));

                    addTab(tabLevel1_3,-1);
                    addTab(tabLevel1_4, -1);
                }
                else
                {
                    appContext.nCurrentLayoutID = R.layout.fragment_levelhome;
                    removeTab(mActionBar.getTabAt(3));
                    removeTab(mActionBar.getTabAt(2));

                    addTab(tabLevel1_2,1);
                    addTab(tabLevel1_3, 2);
                }
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutHome]: "+e.toString());
    }
}

private void showLayoutLevel1_2()
{
    try
    {
        if(mActionBar != null)
        {
            if(appContext.nCurrentLayoutID == R.layout.fragment_levelhome)
            {
                appContext.nCurrentLayoutID = R.layout.fragment_level1_2;
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(2));

                addTab(tabLevel1_2_1,-1);
                addTab(tabLevel1_4_2, -1);
            }
            else
            {
                appContext.nCurrentLayoutID = R.layout.fragment_level1_2;
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(2));

                addTab(tabLevelHome,0);
                addTab(tabLevel1_4_2, -1);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_2]: "+e.toString());
    }
}

private void showLayoutLevel1_3()
{
    try
    {
        if(mActionBar != null)
        {
            if(appContext.nCurrentLayoutID == R.layout.fragment_levelhome)
            {
                appContext.nCurrentLayoutID = R.layout.fragment_level1_3;
                removeTab(mActionBar.getTabAt(3));
                removeTab(mActionBar.getTabAt(1));

                addTab(tabLevel1_3_1, -1);
                addTab(tabLevel1_3_2,-1);
                addTab(tabLevel1_3_3, -1);
            }
            else
            {
                appContext.nCurrentLayoutID = R.layout.fragment_level1_3;
                removeTab(mActionBar.getTabAt(2));
                removeTab(mActionBar.getTabAt(1));

                addTab(tabLevelHome,0);
                addTab(tabLevel1_3_1, -1);
                addTab(tabLevel1_3_2,-1);
                addTab(tabLevel1_3_3, -1);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_3]: "+e.toString());
    }
}

private void showLayoutLevel1_4()
{
    try
    {
        if(mActionBar != null)
        {
            appContext.nCurrentLayoutID = R.layout.fragment_level1_4;
            removeTab(mActionBar.getTabAt(2));
            removeTab(mActionBar.getTabAt(1));

            addTab(tabLevel1_4_1,-1);
            addTab(tabLevel1_4_2, -1);
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_4]: "+e.toString());
    }
}

private void showLayoutLevel1_2_1()
{
    try
    {
        if(mActionBar != null)
        {
            appContext.nCurrentLayoutID = R.layout.fragment_level1_2_1;
            removeTab(mActionBar.getTabAt(3));
            removeTab(mActionBar.getTabAt(0));

            addTab(tabLevel1_2_2,-1);
            addTab(tabLevel1_2_3, -1);
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_2_1]: "+e.toString());
    }
}

private void showLayoutLevel1_3_1()
{
    try
    {
        if(mActionBar != null)
        {
            appContext.nCurrentLayoutID = R.layout.fragment_level1_3_1;
            removeTab(mActionBar.getTabAt(4));
            removeTab(mActionBar.getTabAt(3));
            removeTab(mActionBar.getTabAt(0));

            addTab(tabLevel1_3_1_1, -1);
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_3_1]: "+e.toString());
    }
}

private void showLayoutLevel1_3_2()
{
    try
    {
        if(mActionBar != null)
        {
            appContext.nCurrentLayoutID = R.layout.fragment_level1_3_2;
            removeTab(mActionBar.getTabAt(4));
            removeTab(mActionBar.getTabAt(2));
            removeTab(mActionBar.getTabAt(0));

            addTab(tabLevel1_3_2_1, -1);
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_3_2]: "+e.toString());
    }
}

private void showLayoutLevel1_3_3()
{
    try
    {
        if(mActionBar != null)
        {
            appContext.nCurrentLayoutID = R.layout.fragment_level1_3_3;
            removeTab(mActionBar.getTabAt(3));
            removeTab(mActionBar.getTabAt(2));
            removeTab(mActionBar.getTabAt(0));

            addTab(tabLevel1_3_3_1,-1);
        }
    }
    catch(Exception e)
    {
        appContext.showMessage("Error","[showLayoutLevel1_3_3]: "+e.toString());
    }
}

}

答案 2 :(得分:0)

最后,这是带有HomeFragment代码的HomeActivity,它初始化并显示它...所有你需要做的就是创建appContext(在StackOverflow上找到这个简单的解决方案)和其余的片段,你应该将它们重命名为有意义的你的解决方案...

public class HomeActivity extends ActionBarActivity implements
        Level1_2Fragment.OnFragmentInteractionListener,
        Level1_3Fragment.OnFragmentInteractionListener,
        Level1_4Fragment.OnFragmentInteractionListener,
        Level1_2_1Fragment.OnFragmentInteractionListener,
        Level1_2_2Fragment.OnFragmentInteractionListener,
        Level1_2_3Fragment.OnFragmentInteractionListener,
        Level1_3_1Fragment.OnFragmentInteractionListener,
        Level1_3_1_1Fragment.OnFragmentInteractionListener,
        Level1_3_2Fragment.OnFragmentInteractionListener,
        Level1_3_2_1Fragment.OnFragmentInteractionListener,
        Level1_3_3Fragment.OnFragmentInteractionListener,
        Level1_3_3_1Fragment.OnFragmentInteractionListener,
        Level1_4_1Fragment.OnFragmentInteractionListener,
        Level1_4_2Fragment.OnFragmentInteractionListener
{
    ABTabsAdapter mABTabsAdapter = null;
    ActionBar mActionBar = null;
    ViewPager mViewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager_home);
        setContentView(mViewPager);

        mABTabsAdapter = new ABTabsAdapter(this, getSupportFragmentManager(), mViewPager, mActionBar);

        if (savedInstanceState != null)
        {
            //mActionBar.setSelectedNavigationItem(savedInstanceState.getInt("currentTab", 0));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt("currentTab", mActionBar.getSelectedNavigationIndex());
    }

    public void onFragmentInteraction()
    {
    }

    public static class HomeFragment extends Fragment
    {
        public static HomeFragment newInstance()
        {
            HomeFragment fragment = new HomeFragment();
            return fragment;
        }

        public HomeFragment()
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
            return rootView;
        }
    }
}