Programmatically switch tabs in Android using ActionBarSherlock

后端 未结 2 478
暖寄归人
暖寄归人 2021-01-05 14:05

I couldn\'t find any info about this but, how can i programmatically switch tabs in ActionBarSherlock?

Normally when i want to switch views i\'d use something like:<

2条回答
  •  执笔经年
    2021-01-05 14:26

    In my app I have one tab fragment that has an album of pictures. When the user clicks on one of the pictures it causes that picture to be displayed in a ViewPager on the other tab fragment and automatically switches to that tab with setCurrentTabByTag().

    public class EditAlbumTabs extends SherlockFragmentActivity {
       TabHost mTabHost;
       TabsAdapter mTabsAdapter;
    
       public void onPagerPositionSet(int pagerPosition, String[] imageUrls) {
            FragmentFlash fragmentFlash = (FragmentFlash)mTabsAdapter.getFragment("flash");
            if (fragmentFlash != null) {
               fragmentFlash.pagerPositionSet(pagerPosition, imageUrls);
               **mTabHost.setCurrentTabByTag("flash");**
            }
       }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.fragment_tabs_pager);
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
    
        mViewPager = (ViewPager)findViewById(R.id.pager);
        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
        mTabsAdapter.addTab(mTabHost.newTabSpec("album").setIndicator("Album"),
                FragmentAlbumFlashum.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("flash").setIndicator("Flash"),
                FragmentFlash.class, null);
    }
    

提交回复
热议问题