Your SlideShare is downloading. ×
  • Like
A Deep Dive Into ViewPager
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

A Deep Dive Into ViewPager


from the AnDevCon Boston 2013 conference

from the AnDevCon Boston 2013 conference

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Copyright © 2013 CommonsWare, LLCA Deep Dive IntoViewPagerAnDevCon Boston 2013
  • 2. Copyright © 2013 CommonsWare, LLCGetting Horizontal● Classic Android UX– Focused on vertical scrolling in conventionalapps● Now Presenting: Matias Duarte– Hired by Google away from Palm/WebOS– Re-emphasized horizontal swiping as a patternfor moving between peer content
  • 3. Copyright © 2013 CommonsWare, LLCEnter the Protagonist, Stage Left● ViewPager: Leading Horizontal SwipeSolution– Ships with Android Support package● Not a backport!– Works a Bit Like a ListView● You supply an adapter, which supplies “pages”● ViewPager shows current page● User horizontal swipes to move back and forthbetween pages
  • 4. Copyright © 2013 CommonsWare, LLCTheViewPager Recipe● Add a ViewPager to your activity layout● Create a PagerAdapter– Typical: FragmentPagerAdapter orFragmentStatePagerAdapter– Extend, override getCount() andgetItem()● Attach the PagerAdapter to theViewPager via setAdapter()
  • 5. Copyright © 2013 CommonsWare, LLCWhere We At?● Indicators: PagerTitleStrip, PagerTabStrip– Supplied with Android Support package– Add as child ofViewPager– Set android:layout_gravity to be top
  • 6. Copyright © 2013 CommonsWare, LLCWhere We At: Wharton Style● ViewPagerIndicator– Library of indicators– Add as siblings ofViewPager● E.g., stacked in vertical LinearLayout– Connect toViewPager in Java code
  • 7. Copyright © 2013 CommonsWare, LLCViewPager: Not Perfect● Cannot Easily Be Used In a Fragment– Nested fragments possible but a bit tricky– Requires Android 4.2 or Android Supportpackage● MinimumThree Pages Loaded– Make sure your pages are fast to be created– More likely to be trouble since larger thanListView rows or other AdapterViewscenarios
  • 8. Copyright © 2013 CommonsWare, LLCLights Bar! Camera Bar! Action Bar!● Fragment Pages and the Action Bar– Standard Fragment/Action Bar Recipe● setHasOptionsMenu(true)● onCreateOptionsMenu()● onOptionsItemSelected()– As pages are swiped, action bar items areadded/removed
  • 9. Copyright © 2013 CommonsWare, LLCIts Scrolling, AllThe Way Down● Handling Scrollable Page Content– SubclassViewPager– Override canScroll()● Return true if the designated content could bescrolled, false otherwise● Touch point and scroll delta supplied, in case youneed it (e.g., bezel swiping)
  • 10. Copyright © 2013 CommonsWare, LLCN-Up Printing (Without the Printing Part)● Showing Multiple Pages at Once– Implement getPageWidth() on yourPagerAdapter● Returns float indicating amount of pager space todevote to this page (e.g., 0.5f)– May wish to callsetOffscreenPageLimit() onViewPagerto raise number of cached pages
  • 11. Copyright © 2013 CommonsWare, LLCPlume: A Column of Smoke● Pages on Phones, Columns onTablets– Devise proper layouts for the screen sizes– If you have aViewPager, give it the adapter– If you do not have aViewPager:● Use static fragments, or● Use the adapter to give you fragments for slots– Limitation: same basic structure in allconfigurations (cannot deal with change)
  • 12. Copyright © 2013 CommonsWare, LLCI Can Haz Better Adapter?● FragmentPagerAdapter andFragmentStatePagerAdapter are nice but notperfect● Limitations– UsingViewPager in some configurations, but notall– Adding/removing/reordering pages
  • 13. Copyright © 2013 CommonsWare, LLCSpeaker ContactInformationSource Code