Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ActionBar.TabListener tabListener = new ActionBar.TabListener() {
- public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
- // When the tab is selected, switch to the
- // corresponding page in the ViewPager.
- mViewPager.setCurrentItem(tab.getPosition());
- }
- ...
- };
- /////////////////////////////////////////////////
- final ActionBar actionBar = getActionBar();
- ...
- // Specify that tabs should be displayed in the action bar.
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- // Create a tab listener that is called when the user changes tabs.
- ActionBar.TabListener tabListener = new ActionBar.TabListener() {
- public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
- // show the given tab
- }
- public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
- // hide the given tab
- }
- public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
- // probably ignore this event
- }
- };
- // Add 3 tabs, specifying the tab's text and TabListener
- for (int i = 0; i < 3; i++) {
- actionBar.addTab(
- actionBar.newTab()
- .setText("Tab " + (i + 1))
- .setTabListener(tabListener));
- }
- ////////////////////////////////////////////
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Tab 1"
- android:textAppearance="?android:attr/textAppearanceLarge"/>
- ///////////////////////////////////////
- public class Tab1 extends Fragment {
- //Overriden method onCreateView
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //Returning the layout file after inflating
- //Change R.layout.tab1 in you classes
- return inflater.inflate(R.layout.tab1, container, false);
- }
- }
- ////////////////////////////////////////
- public class Pager extends FragmentStatePagerAdapter {
- //integer to count number of tabs
- int tabCount;
- //Constructor to the class
- public Pager(FragmentManager fm, int tabCount) {
- super(fm);
- //Initializing tab count
- this.tabCount= tabCount;
- }
- //Overriding method getItem
- @Override
- public Fragment getItem(int position) {
- //Returning the current tabs
- switch (position) {
- case 0:
- Tab1 tab1 = new Tab1();
- return tab1;
- case 1:
- Tab2 tab2 = new Tab2();
- return tab2;
- case 2:
- Tab3 tab3 = new Tab3();
- return tab3;
- default:
- return null;
- }
- }
- //Overriden method getCount to get the number of tabs
- @Override
- public int getCount() {
- return tabCount;
- }
- }
- /////////////////////////////////////////////main/////
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
- app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
- <!-- our tablayout to display tabs -->
- <android.support.design.widget.TabLayout
- android:id="@+id/tabLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
- <!-- View pager to swipe views -->
- <android.support.v4.view.ViewPager
- android:id="@+id/pager"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"/>
- ////////////////////////////////////////main
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
- app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
- <!-- our tablayout to display tabs -->
- <android.support.design.widget.TabLayout
- android:id="@+id/tabLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="?attr/colorPrimary"
- android:minHeight="?attr/actionBarSize"
- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
- <!-- View pager to swipe views -->
- <android.support.v4.view.ViewPager
- android:id="@+id/pager"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"/>
- ////////////////////////////////////////////////////////
- public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
- //This is our tablayout
- private TabLayout tabLayout;
- //This is our viewPager
- private ViewPager viewPager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Adding toolbar to the activity
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- //Initializing the tablayout
- tabLayout = (TabLayout) findViewById(R.id.tabLayout);
- //Adding the tabs using addTab() method
- tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
- tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
- tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
- tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
- //Initializing viewPager
- viewPager = (ViewPager) findViewById(R.id.pager);
- //Creating our pager adapter
- Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
- //Adding adapter to pager
- viewPager.setAdapter(adapter);
- //Adding onTabSelectedListener to swipe views
- tabLayout.setOnTabSelectedListener(this);
- }
- @Override
- public void onTabSelected(TabLayout.Tab tab) {
- viewPager.setCurrentItem(tab.getPosition());
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement