如何设置最初选择的选项卡ActionBarSherlock

时间:2012-08-01 01:01:57

标签: android tabs actionbarsherlock

我正在尝试使用ActionBarShelock将我的Tab2设置为最初选择的选项卡,但我无法开始工作。

选项卡工作正常但我无法在应用程序启动时选择第二个选项卡而不是第一个选项卡。 这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppThemeLightDarkActionBar);
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayShowTitleEnabled(true);

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

    setContentView(mViewPager);

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

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(bar.newTab().setText("Tab1"), Tab1.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Tab2"),
            Tab2.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Tab3"), Tab3.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText("Tab4"), Tab4.class, null);


}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

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

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

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

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

我怎样才能让它发挥作用? 谢谢。

1 个答案:

答案 0 :(得分:2)

onCreate方法的末尾使用此内容:

mViewPager.setCurrentItem(1);

above method应该从0开始,所以1是第二页。