SlideShare a Scribd company logo
Copyright © 2013 CommonsWare, LLC
A Deep Dive Into
ViewPager
AnDevCon Boston 2013
Copyright © 2013 CommonsWare, LLC
Getting Horizontal
● Classic Android UX
– Focused on vertical scrolling in conventional
apps
● Now Presenting: Matias Duarte
– Hired by Google away from Palm/WebOS
– Re-emphasized horizontal swiping as a pattern
for moving between peer content
Copyright © 2013 CommonsWare, LLC
Enter the Protagonist, Stage Left
● ViewPager: Leading Horizontal Swipe
Solution
– 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 forth
between pages
Copyright © 2013 CommonsWare, LLC
TheViewPager Recipe
● Add a ViewPager to your activity layout
● Create a PagerAdapter
– Typical: FragmentPagerAdapter or
FragmentStatePagerAdapter
– Extend, override getCount() and
getItem()
● Attach the PagerAdapter to the
ViewPager via setAdapter()
Copyright © 2013 CommonsWare, LLC
Where We At?
● Indicators: PagerTitleStrip, PagerTabStrip
– Supplied with Android Support package
– Add as child ofViewPager
– Set android:layout_gravity to be top
Copyright © 2013 CommonsWare, LLC
Where We At: Wharton Style
● ViewPagerIndicator
– Library of indicators
– Add as siblings ofViewPager
● E.g., stacked in vertical LinearLayout
– Connect toViewPager in Java code
Copyright © 2013 CommonsWare, LLC
ViewPager: Not Perfect
● Cannot Easily Be Used In a Fragment
– Nested fragments possible but a bit tricky
– Requires Android 4.2 or Android Support
package
● MinimumThree Pages Loaded
– Make sure your pages are fast to be created
– More likely to be trouble since larger than
ListView rows or other AdapterView
scenarios
Copyright © 2013 CommonsWare, LLC
Lights 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 are
added/removed
Copyright © 2013 CommonsWare, LLC
It's Scrolling, AllThe Way Down
● Handling Scrollable Page Content
– SubclassViewPager
– Override canScroll()
● Return true if the designated content could be
scrolled, false otherwise
● Touch point and scroll delta supplied, in case you
need it (e.g., bezel swiping)
Copyright © 2013 CommonsWare, LLC
N-Up Printing (Without the Printing Part)
● Showing Multiple Pages at Once
– Implement getPageWidth() on your
PagerAdapter
● Returns float indicating amount of pager space to
devote to this page (e.g., 0.5f)
– May wish to call
setOffscreenPageLimit() onViewPager
to raise number of cached pages
Copyright © 2013 CommonsWare, LLC
Plume: 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 all
configurations (cannot deal with change)
Copyright © 2013 CommonsWare, LLC
I Can Haz Better Adapter?
● FragmentPagerAdapter and
FragmentStatePagerAdapter are nice but not
perfect
● Limitations
– UsingViewPager in some configurations, but not
all
– Adding/removing/reordering pages
Copyright © 2013 CommonsWare, LLC
Speaker Contact
Information
Source Code

More Related Content

PDF
Android Training - View Pager
PDF
Android Training - Card Style
ODP
PDF
How to Enable PWA for Multi Company in Odoo ?
PDF
Website Project Kanbanview
PDF
eLearning Local Video
PDF
Mobile Web Rock
DOCX
JineshPatel_Portfolio
Android Training - View Pager
Android Training - Card Style
How to Enable PWA for Multi Company in Odoo ?
Website Project Kanbanview
eLearning Local Video
Mobile Web Rock
JineshPatel_Portfolio

Similar to A Deep Dive Into ViewPager (20)

PPTX
User experience and interactions design
PDF
Android design patterns
PDF
Mastering the Master Detail Pattern
PDF
Android Best Practices
PDF
How Android API's evolved: Viewflipper vs ViewPager
PPT
Getting the Magic on Android Tablets
PPT
Hello Android
PPTX
Advance ui development and design
PDF
The Action Bar: Front to Back
PDF
Improving android experience for both users and developers
PDF
Droidcon2013 android experience lahoda
PPTX
Android 3
PDF
The Good, the Bad and the Ugly things to do with android
PPTX
Treatment, Architecture and Threads
PPTX
Performance #3 layout&animation
PDF
Android Support Libraries
PDF
Introducing Honeycomb
PDF
Android TV: Building apps with Google’s Leanback Library
PDF
The Action Bar: Front to Back
PDF
Georgiy Shur: Bring onboarding to life
User experience and interactions design
Android design patterns
Mastering the Master Detail Pattern
Android Best Practices
How Android API's evolved: Viewflipper vs ViewPager
Getting the Magic on Android Tablets
Hello Android
Advance ui development and design
The Action Bar: Front to Back
Improving android experience for both users and developers
Droidcon2013 android experience lahoda
Android 3
The Good, the Bad and the Ugly things to do with android
Treatment, Architecture and Threads
Performance #3 layout&animation
Android Support Libraries
Introducing Honeycomb
Android TV: Building apps with Google’s Leanback Library
The Action Bar: Front to Back
Georgiy Shur: Bring onboarding to life
Ad

More from CommonsWare (20)

PDF
Gradle and Your Android Wearable Projects
PDF
Getting Android Developers for Your Wearables
PDF
When Microwatts Are Precious: Battery Tips for Wearable Apps
PDF
Android Security: Defending Your Users
PDF
Secondary Screen Support Using DisplayManager
PDF
Not Quite As Painful Threading
PDF
Android Development: The 20,000-Foot View
PDF
Maps V2... And You!
PDF
Second-Screen Support in Android 4.2
PDF
Integrate Android Apps and Web Apps
PDF
From Android to the Mobile Web
PDF
X Means Y
PDF
The Wonderful World of Wearables
PDF
Securing User Data with SQLCipher
PDF
Beaming Data to Devices with NFC
PDF
What's New in Jelly Bean
PDF
Making Money at Mobile: 60 Business Models
PDF
AppsWorld Keynote
PDF
App Integration (Revised and Updated)
PDF
Rich Text Editing and Beyond
Gradle and Your Android Wearable Projects
Getting Android Developers for Your Wearables
When Microwatts Are Precious: Battery Tips for Wearable Apps
Android Security: Defending Your Users
Secondary Screen Support Using DisplayManager
Not Quite As Painful Threading
Android Development: The 20,000-Foot View
Maps V2... And You!
Second-Screen Support in Android 4.2
Integrate Android Apps and Web Apps
From Android to the Mobile Web
X Means Y
The Wonderful World of Wearables
Securing User Data with SQLCipher
Beaming Data to Devices with NFC
What's New in Jelly Bean
Making Money at Mobile: 60 Business Models
AppsWorld Keynote
App Integration (Revised and Updated)
Rich Text Editing and Beyond
Ad

Recently uploaded (20)

PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
CIFDAQ's Market Insight: SEC Turns Pro Crypto
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectral efficient network and resource selection model in 5G networks
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)

A Deep Dive Into ViewPager

  • 1. Copyright © 2013 CommonsWare, LLC A Deep Dive Into ViewPager AnDevCon Boston 2013
  • 2. Copyright © 2013 CommonsWare, LLC Getting Horizontal ● Classic Android UX – Focused on vertical scrolling in conventional apps ● Now Presenting: Matias Duarte – Hired by Google away from Palm/WebOS – Re-emphasized horizontal swiping as a pattern for moving between peer content
  • 3. Copyright © 2013 CommonsWare, LLC Enter the Protagonist, Stage Left ● ViewPager: Leading Horizontal Swipe Solution – 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 forth between pages
  • 4. Copyright © 2013 CommonsWare, LLC TheViewPager Recipe ● Add a ViewPager to your activity layout ● Create a PagerAdapter – Typical: FragmentPagerAdapter or FragmentStatePagerAdapter – Extend, override getCount() and getItem() ● Attach the PagerAdapter to the ViewPager via setAdapter()
  • 5. Copyright © 2013 CommonsWare, LLC Where 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, LLC Where 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, LLC ViewPager: Not Perfect ● Cannot Easily Be Used In a Fragment – Nested fragments possible but a bit tricky – Requires Android 4.2 or Android Support package ● MinimumThree Pages Loaded – Make sure your pages are fast to be created – More likely to be trouble since larger than ListView rows or other AdapterView scenarios
  • 8. Copyright © 2013 CommonsWare, LLC Lights 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 are added/removed
  • 9. Copyright © 2013 CommonsWare, LLC It's Scrolling, AllThe Way Down ● Handling Scrollable Page Content – SubclassViewPager – Override canScroll() ● Return true if the designated content could be scrolled, false otherwise ● Touch point and scroll delta supplied, in case you need it (e.g., bezel swiping)
  • 10. Copyright © 2013 CommonsWare, LLC N-Up Printing (Without the Printing Part) ● Showing Multiple Pages at Once – Implement getPageWidth() on your PagerAdapter ● Returns float indicating amount of pager space to devote to this page (e.g., 0.5f) – May wish to call setOffscreenPageLimit() onViewPager to raise number of cached pages
  • 11. Copyright © 2013 CommonsWare, LLC Plume: 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 all configurations (cannot deal with change)
  • 12. Copyright © 2013 CommonsWare, LLC I Can Haz Better Adapter? ● FragmentPagerAdapter and FragmentStatePagerAdapter are nice but not perfect ● Limitations – UsingViewPager in some configurations, but not all – Adding/removing/reordering pages
  • 13. Copyright © 2013 CommonsWare, LLC Speaker Contact Information Source Code