View Slide with ViewPage Horizontal

Whether you just started on Android application development or a veteran of the ship, probably will not be long before you need to implement it moves horizontally set of views. Many of the existing Android applications using this model and user interface, such as the new Android Market, Google Docs and Google +. ViewPage standardized implementation.

ViewPage was released as part of the review of the compatibility pack 3 works with Android 1.6 and up. After following the instructions to get the package that you can right click on the Android project in Eclipse, select "Android Tools" and "Adding compatibility library" so that new classes available.

ViewPage is a ViewGroup and works similarly to AdapterViews (such as ListView and Gallery), so you should not feel too weird. Note that if you use a design ViewPage xml, be sure to use the full class reference, for example,

        … />

ViewPagers source of their views PagerAdapters that will give you full control over the reuse and recycling of views. An application called PagerAdapter FragmentPagerAdapter is provided to facilitate the use of fragments in a ViewPage, which is immensely powerful and as simple as applying getCount () and getItem (). There is an example called Fragment Pager Support always shows support to illustrate this.

public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {

public int getCount() {
return NUM_ITEMS;

public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);

FragmentPagerAdapter each fragment is separated by passing through the list, but keep in memory so it can simply be reattached when the user moves again. If you have a larger number of fragments, the FragmentStatePagerAdapter worth considering, as it will be removed, with the disadvantage that need to be rebuilt as the user goes back to them. Therefore, if less, the most complex pieces FragmentPagerAdapter makes sense, but consider FragmentStatePagerAdapter for large sets.

On the plus side I recently wrote a simple example ViewPage / PagerAdapter TextViews serving simple. One thing to note is that if you are carrying out their own PagerAdapter up to you, the developer, to add and remove their points of view and from the ViewGroup. To facilitate this, the ViewPage is passed to the methods PagerAdapter instantiateItem () and destroyItem ().

public Object instantiateItem(View collection, int position) {
View v = layoutInflater.inflate(...);
((ViewPager) collection).addView(v,0);
return tv;

public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((TextView) view);

The source code for ViewPage is also included and is available in / Extras/android/compatibility/v4/src. It's worth checking that can generate the reference documentation it with Javadoc. In the reference docs / source will find other useful methods, such setOnPageChangeListener (), allowing its application to the court that Vista is currently visible.

If you are launching an application in Android Market using ViewPage please ping me in Google + or Twitter, I love to see how widely it is used and the stages of innovation in which it appears.
Komentar Facebook
1 Komentar untuk "View Slide with ViewPage Horizontal"

why the tabs also scrolled when you scroll the view?And the tab always center the width of the view?

Back To Top