In September 2011 I began a series on Styling Android about ViewPager and Jake Wharton’s ViewPagerIndicator library. This proved to be a very popular series, but the comments on those articles show that there has been some changes in Jake’s library which means that the code in the original articles no longer works with the latest version of the library. In this series we’ll revisit ViewPager and ViewPagerIndicator, get the old example code working with the latest version of the library, and also look at some alternatives.
The methods for obtaining the ViewPagerIndicator library have not changed since the original article on ViewPager, so I won’t bother repeating them here.
Once we have checked out the latest version (2.4.1 at the time of writing) of ViewPagerIndicator/library, we need to configure our existing ViewPager code to use this library by right clicking on it in Package Explorer, then selecting “Properties|Android”. Then in the “Library” panel, remove any existing library settings, and adding the ViewPagerIndicator-library that we’ve just checked out.
The first problem that we’ll see is that there is an error in res/values/styles.xml. This is because the style specifies a parent of
Widget.TitlePageIndicator which was included in the earlier version of the library, but is no longer there. We can see how to resolve this by looking at Jake’s sample application code, and see how the styles are organised there. If we look here we can see that
CustomTitlePageIndicator style no longer inherits from a style defined in the library itself, so all we need to do to fix our code is remove the parent specification. If we compare the style in Jake’s sample to our style we can see that they’re pretty similar, so we’ll just go with the one we have, just without the parent definition.
The second major thing that has changed is the package name of the library. Previously it was
com.jakewharton.android.viewpagerindicator, but now it is
com.viewpagerindicator, so we must change the imports in ViewPagerActivity. We must also update the package name in res/layout/main.xml as this also refers to the incorrect package.
Thirdly, if we look at ViewPagerAdapter we can see that we have to implement the TitleProvider interface. If we change the package name as before, we still see an error because this interface is no longer a part of the library. The reason for this is because this version of ViewPagerIndicator is based upon a newer version of the support library, and this has an expanded API which has a title provider built in. We must remove the TitleProvider import, and implements directive from our ViewPagerAdapter class definition, and change the method name from
If we now run this we can see that we have the behaviour that we did previously:
Getting the existing code to work with the current version of the library wasn’t too difficult, but there we a few changes that were worthy of some explanation.
In the next part of this series we’ll look at an alternative to Jake’s ViewPagerIndicator library.
The source code for this article can be found here.
© 2012, Mark Allison. All rights reserved.
ViewPager Redux – Part 1 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.