1

複数のコンテキストレベルのタブと関連するフラグメントをユーザーに表示するように、ActionBar で Android タブを処理するにはどうすればよいですか (つまり、より高いレベルでの選択に基づいて、ActionBar タブをあるレベルから別のレベルに変更したい -階層メニューのようなものです)。(理想的には、ソリューションは任意の数の「レベル」をサポートしますが、少なくとも 3 つの「レベル」があればよいでしょう)

注: 回答を適切な順序で並べ替えるには、「最も古い」順に並べ替えます。これにより、パーツが HomeActivity を最後にして適切な場所に配置されます。

4

3 に答える 3

0

これは、FragmentStatePagerAdapter、ViewPager、および ActionBar を使用して、関連付けられたフラグメントを持つ最大 3 つの仮想レベルのタブを表示および管理する ABTabsAdapter クラスと呼ばれるタブ クラスの例です。 . このソリューションは、StackOverflow および Android サポート資料に投稿された他のソリューションからまとめられています。重要なのは、FragmentStatePagerAdapter を使用することです (Android lit に記載されている FragmentPagerAdapter ではなく、デフォルトの Android プロジェクト テンプレートに示されているものです)。また、addTab 関数を介して挿入時にタブが選択されないようにする bIsSet ブール変数の使用にも注意してください。最後に、getItemPosition 関数はフラグメント/ビュー/タブの実際の位置も返すことができることに注意してください。

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;
    }
于 2014-07-03T15:11:49.237 に答える
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;
        }
    }
}
于 2014-07-03T15:19:50.443 に答える
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());
    }
}

}

于 2014-07-03T15:15:38.553 に答える