android tab layout with swipeable views

19
13/8/2015 Android Tab Layout with Swipeable Views http://www.androidhive.info/2013/10/androidtablayoutwithswipeableviews1/ 1/19 Advertise Here London Accommodations Unique Rentals in London. Book accommodations from $49/night. Android Tab Layout with Swipeable Views 463 Comments . By Ravi Tamada . on October 6, 2013 My previous article explains about Android Tab Layout and it got very good ranking in search engines. But unfortunately TabHost is deprecated by android in favor of fragments. So it is suggested that use fragment to achieve tab layout. This article shows you how to create tab layout using fragments and viewpager. Also you can swipe between tab view as it is the functionality of viewpager which is not possible when using TabHost. DOWNLOAD CODE VIDEO DEMO

Upload: tiennq93

Post on 17-Aug-2015

228 views

Category:

Documents


0 download

DESCRIPTION

fragment

TRANSCRIPT

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.