Placeholder Image

Subtitles section Play video

  • IAN LAKE: Welcome to Android Development Patterns, where

  • we help you build better apps.

  • I'm Ian Lake.

  • Let's get to work.

  • Let's think about how you navigate through an app.

  • You navigate to a detailed view from the parent view,

  • or you can navigate between sibling views.

  • That's where tabs and ViewPagers come in.

  • So what is a ViewPager?

  • That's what controls the actual swiping between different pages

  • of content.

  • ViewPager gets its content from a PagerAdapter.

  • This could be a generic view-- one

  • for each page-- or whole fragments,

  • if you use a FragmentPagerAdapter

  • or FragmentStatePagerAdapter.

  • FragmentPagerAdapter keeps each fragment

  • it creates in memory, making it lightning fast to switch

  • between already loaded tabs.

  • However, this can get expensive, memory-wise,

  • if you have a large number of fragments.

  • Something FragmentStatePagerAdapter

  • solves by destroying and recreating fragments as needed,

  • only saving the state.

  • There isn't a lot to a PagerAdapter.

  • You'll need to override getCount to return

  • the number of pages you have.

  • Then, in the case of a FragmentPagerAdapter,

  • you'll need to implement getItem,

  • which returns the fragment associated with each position.

  • Here, we'll just do a simple switch statement,

  • selecting the right fragment for each position.

  • But you have the flexibility to use any approach you want.

  • The other thing we might want to override

  • is getPageTitle, returning a string describing each page.

  • Why would you want a page title?

  • Well, those page titles will be really helpful

  • if you want to have some tabs associated with your ViewPager.

  • Tabs give users a great way to see

  • exactly what those pages contain without having

  • to scroll through each one.

  • They also allow users to jump between pages

  • by tapping on a tab.

  • The TabLayout class, part of the Android Design Support Library,

  • makes it easy to implement tabs, with all the material design

  • styling.

  • And it's backward compatible to Android 2.1.

  • Let's hook everything together.

  • First, we need to hook our ViewPager up

  • to a PagerAdapter using setAdapter.

  • Then, hooking up a TabLayout is just a single method

  • setupWithViewPager.

  • With just that, you get tabs from the PagerAdapter's titles.

  • And swiping through pages will change the tabs,

  • and tapping on tabs will change the pages-- as you'd expect.

  • Get started with the TabLayout and ViewPagers

  • by adding the Design Library.

  • Be sure to check out the design guidelines

  • and the detailed training docs.

  • Thanks for joining me here on Android Development Patterns.

  • Use tabs and ViewPagers to build better apps.

  • [MUSIC PLAYING]

IAN LAKE: Welcome to Android Development Patterns, where

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it