0

我已经在我的项目中成功实现了actionsherlock。所以这是我的代码:

public class ActiontabActivity extends SherlockFragmentActivity   {


    ActionMode mMode;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
        setContentView(R.layout.main);
         ActionBar bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

            bar.setDisplayShowHomeEnabled(false);


            bar.setDisplayShowTitleEnabled(false);
            bar.setTitle("Currency Exchange");
            bar.addTab(bar
                    .newTab()
                    .setText("Converter")
                    .setTabListener(
                            new TabListener<Fragment>(this, "Converter",
                                    ConverterTab.class, null)));

            bar.addTab(bar
                    .newTab()
                    .setText("Favorites")
                    .setTabListener(
                            new TabListener1<Fragment>(this, "Favorites",
                                    FavoritesTab.class, null)));
            if (savedInstanceState != null) {
                bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
            }


    }


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

    public class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private final FragmentActivity mActivity;
        private final String mTag;
        private final Class<ConverterTab> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;




        public TabListener(FragmentActivity activity, String tag, Class<ConverterTab> clz, Bundle args) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();


            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                ft.detach(mFragment);
            }
        }




        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
             ft = mActivity.getSupportFragmentManager()
                        .beginTransaction();

                if (mFragment == null) {
                    mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                            mArgs);
                    ft.add(android.R.id.content, mFragment, mTag);
                    ft.commit();
                } else {
                    ft.attach(mFragment);
                    ft.commit();
                }


        }




        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
               if (mFragment != null) {
                    //this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
                    FragmentManager man = mFragment.getFragmentManager();
                    if(man.getBackStackEntryCount()>0) //this check is required to prevent null point exceptions when clicking off of a tab with no history
                        man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); //this pops the stack back to index 0 so you can then detach and then later attach your initial fragment
                    //also it should be noted that if you do popbackstackimmediate here instead of just popbackstack you will see a flash as the gui changes back to the first fragment when the code executes
                    //end
                    ft.detach(mFragment);
                }



        }




        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }



    }
    public class TabListener1<T extends Fragment> implements ActionBar.TabListener {
        private final FragmentActivity mActivity;
        private final String mTag;
        private final Class<FavoritesTab> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;




        public TabListener1(FragmentActivity activity, String tag, Class<FavoritesTab> clz, Bundle args) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();


            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                ft.detach(mFragment);
            }
        }




        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
             ft = mActivity.getSupportFragmentManager()
                        .beginTransaction();

                if (mFragment == null) {
                    mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                            mArgs);
                    ft.add(android.R.id.content, mFragment, mTag);
                    ft.commit();
                } else {
                    ft.attach(mFragment);
                    ft.commit();
                }


        }




        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
             if (mFragment != null) {
                    //this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
                    FragmentManager man = mFragment.getFragmentManager();
                    if(man.getBackStackEntryCount()>0) //this check is required to prevent null point exceptions when clicking off of a tab with no history
                        man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); //this pops the stack back to index 0 so you can then detach and then later attach your initial fragment
                    //also it should be noted that if you do popbackstackimmediate here instead of just popbackstack you will see a flash as the gui changes back to the first fragment when the code executes
                    //end
                    ft.detach(mFragment);
                }


        }




        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }



    }
}

我在转换器选项卡中放了一个按钮,在收藏夹选项卡中放了一个复选框。但是现在我想当我运行我的应用程序时,它最初没有选择任何选项卡。只显示主屏幕,当我按下任何选项卡时,它会显示它的内容。因为最初我想在未选择选项卡时显示其他内容。请告诉我我该怎么做?请告诉我如何显示最初的主屏幕,然后当我选择任何选项卡时它会显示其内容。我附上了我的图片项目:

在此处输入图像描述 在此处输入图像描述

4

3 回答 3

2

您可以通过以下方式执行此操作:

bar.addTab(yourTab,false);

像这样,选项卡仍然是可视选择的,但侦听器中 onTabSelected 方法中的操作未执行。

于 2012-07-22T09:30:10.047 回答
0

不幸的是,当您创建标签时,Android 应该将任何标签标记为选中。但我有几个想法。

首先。您可以替换内容。只需制作一个布尔标志并在选择选项卡时更改其值。因此,当标志为假时(例如),您可以在主屏幕上显示您的内容。

或者第二个想法。只需使用选项卡和您的主屏幕内容进行另一个主要活动。And when any tab is selected just run your activity with real tabs.

对不起我的英语。有些事情可能不清楚。所以问我,我会尝试更清楚地解释它。

于 2012-07-11T20:31:55.330 回答
0

像这样尝试...
actionbar.addTab(converterTab,0,false);
actionbar.addTab(favouriteTab,1,false);

于 2013-05-10T08:34:24.523 回答