Back to project page Android-ActionBarSherlock-ViewPager-Tabs-Tutorial.
The source code is released under:
Apache License
If you think the Android project Android-ActionBarSherlock-ViewPager-Tabs-Tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.androidbegin.absviewpagertutorial; /*from w ww. j a va 2 s . co m*/ import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class MainActivity extends SherlockFragmentActivity { // Declare Variables ActionBar mActionBar; ViewPager mPager; Tab tab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from activity_main.xml setContentView(R.layout.activity_main); // Activate Navigation Mode Tabs mActionBar = getSupportActionBar(); mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Locate ViewPager in activity_main.xml mPager = (ViewPager) findViewById(R.id.pager); // Activate Fragment Manager FragmentManager fm = getSupportFragmentManager(); // Capture ViewPager page swipes ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { super.onPageSelected(position); // Find the ViewPager Position mActionBar.setSelectedNavigationItem(position); } }; mPager.setOnPageChangeListener(ViewPagerListener); // Locate the adapter class called ViewPagerAdapter.java ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm); // Set the View Pager Adapter into ViewPager mPager.setAdapter(viewpageradapter); // Capture tab button clicks ActionBar.TabListener tabListener = new ActionBar.TabListener() { @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // Pass the position on tab click to ViewPager mPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } }; // Create first Tab tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener); mActionBar.addTab(tab); // Create second Tab tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener); mActionBar.addTab(tab); // Create third Tab tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener); mActionBar.addTab(tab); } }