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

Posted

技术标签:

【中文标题】Android - 使用 ViewPager 和 FragmentStatePagerAdapter 为 ActionBar 创建多级选项卡【英文标题】:Android - Creating Multiple Levels of Tabs for ActionBar Using ViewPager and FragmentStatePagerAdapter 【发布时间】:2014-08-24 19:08:00 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

第 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】:

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

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;
        
    

【讨论】:

【参考方案3】:

这是我的一个名为 ABTabsAdapter 类的选项卡类的示例,它使用 FragmentStatePagerAdapter、ViewPager 和 ActionBar 呈现和管理多达三个虚拟级别的选项卡以及关联的片段,所有这些都与“粘合”在一起的选项卡和片段的集合一起工作一个 TabInfo 结构。该解决方案是从 *** 上发布的其他解决方案和 Android 支持文献拼凑而成的。关键是使用 FragmentStatePagerAdapter(不是 Android lit 中所述的 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;
    

【讨论】:

以上是关于Android - 使用 ViewPager 和 FragmentStatePagerAdapter 为 ActionBar 创建多级选项卡的主要内容,如果未能解决你的问题,请参考以下文章

Android ------ ViewPager1和ViewPager2的使用

Android ------ ViewPager1和ViewPager2的使用

如何在 Android 中使用 TabLayout 和 ViewPager2

如何使用导航抽屉和viewpager/viewpager2在java中制作android应用程序

如何在Android中使用viewPagerindicator和ViewPager?

Android PullToRefreshListView和ViewPager的结合使用