Free Android app covering all aspects of addiction, including prevention and treatment

Swipe View Tutorial

Using Swipe Views to swop pages

Android Swipe Views icon

Swipe views allow you to change pages by swiping your finger across the screen.

You create Swipe views by using a page viewer and an adapter. The adapter supplies the pages and the page viewer takes care of moving the pages on and off the screen.

We’re using a ViewPager. It’s a layout manager that takes care of the horizontal scrolling. Usually it’s used with fragments, scrolling them on and off screen.

We’re also going to need a page adapter to supply the pages to the ViewPager.

As we’re using fragments, we have two adapter options:

  • FragmentPagerAdapter – represents each page as a fragment. The fragment is kept in the fragment manager while it’s still possible that it may be used. This adapter is best used for a small number of fragments. It can use lot of memory as fragments are kept in memory while not displayed
  • FragmentStatePagerAdapter – uses fragments to manage the pages. It also saves and restores the fragment’s state. It’s useful for a large number of pages as it uses less memory. Only the state of the fragment is kept in memory and not the whole fragment. It may be a bit slow switching between pages due to it first having to restore their state

We’re going to use the FragmentStatePagerAdapter.

Read more