13/8/2015
AndroidTabLayoutwithSwipeableViewshttp://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/
1/19AdvertiseHereLondonAccommodationsUniqueRentalsinLondon.Bookaccommodationsfrom$49/night.Android
Tab Layout with Swipeable Views463 Comments . By Ravi Tamada . on
October 6,
2013MypreviousarticleexplainsaboutAndroidTabLayoutanditgotverygoodrankinginsearchengines.ButunfortunatelyTabHostisdeprecatedbyandroidinfavor
of fragments. So it is suggested that use fragmentto achieve tab
layout.Thisarticleshowsyouhowtocreatetablayoutusingfragments and
viewpager. Also you can swipe
betweentabviewasitisthefunctionalityofviewpagerwhichisnot possible
when using TabHost.DOWNLOAD CODEVIDEO DEMO13/8/2015
AndroidTabLayoutwithSwipeableViewshttp://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/
2/19Android Tab Layout with Swipeable ViewsViewPager and
FragmentsBeforegettingintothistutorialitissuggestedtohaveknowledgeonFragmentsandViewPagerasthesetwoaremainconceptsusedhere.UnfortunatelyIhaventcoveredaboutfragementsandviewpager
on androidhive Layout
OverviewCheckoutthefollowingpicwhichexplainsthecompleteoverviewoflayoutarchitecture.Basicallyweare
using
ViewPagerasmainlayoutandforindividualpagerviewsweuseFragments.Thetabsarepart
of Action Bar.13/8/2015
AndroidTabLayoutwithSwipeableViewshttp://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/
3/19Creating new ProjectEven though you are not familiar with
ViewPager or Fragments, dont worry. You will get an idea aboutwhat
those are and how to use them once you are done through this
article. So lets start by creating anew project.1. Create a new
project in Eclipse from File New Android Application Project. While
creatingthe project select the app theme which has Action Bar as
shown in the below image.13/8/2015
AndroidTabLayoutwithSwipeableViewshttp://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/
4/192.AswearegoingtouseFragments,extendyourmainactivityfromFragmentActi
vi ty .Alsoimplement this class fromActi onBar. TabLi steneras we
are adding Tabs too.3.OpenmainactivitylayoutfileandaddVi ewPager
element.(Mylayoutfileformainactivityisactivity_main.xml)publicclassMainActivityextendsFragmentActivityimplementsActionBar.TabListener{ACTIVITY_MAIN.XML13/8/2015
AndroidTabLayoutwithSwipeableViewshttp://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/
5/194. I normally prefer to create a separate package for adapter
classes just to separate them from
activityclasses.Socreateanewpackagenamedyour_package_name.adapter.Inamedmynewpackageasinfo.androidhive.tabsswipe.adapter5.
I am creating aFragmentPagerAdapterclass to provide views to tab
fragments. Create a classcalled TabsPagerAdapter.java under adapter
package. This adapter provides fragment views to tabswhich we are
going to create them later in this tutorial.