Wednesday, October 19, 2011

Simple ViewPager for Android

You would have seen many applications recently, which make use of the new and awesome ViewPager that allows views to be horizontally scrollable. The new Android Market app also implements a flavor of the ViewPager, although it's a little more complex that what we will see here. This class is not available directly for you to use. Check this blog for some insight. It would required you to download a compatibility library from Google, add it to your project wherever you would like to use it, and go about paging views.

And yes, it's very simple to implement. I have coded up a little sample where it shows you how to use a ViewPager with a simple PagerAdapter. More or less, it works like the ListViews. Although there is not much documentation for this, it's quite easy to set up everything.

Firstly, you would need to add the ViewPager into your layout file.

Listing of main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Page 1" android:id="@+id/textViewHeader"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center" android:padding="10dip" android:textStyle="bold"></TextView>
    <android.support.v4.view.ViewPager
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/viewPager" />
</LinearLayout>
In this sample, we are using an extra TextView which would show the current page you are on.

Setting up the ViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter adapter = new MyPagerAdapter(this);
viewPager.setAdapter(adapter);
This code is almost identical to what we would do for a ListView or Gallery. The only difference is that the adapter here, extends a PagerAdapter instead.

The PagerAdapter

The PagerAdapter has a few methods that you would implement. For this example, I have four different views, one for each page. 2 are ListViews, a TextView and a Button, not in that order. Here are the methods that you would need to implement. Look at the MyPageAdapter class for more.

@Override
public void destroyItem(View view, int arg1, Object object) {
         ((ViewPager) view).removeView((View)object);
}
@Override
public int getCount() {
          return views.size();
}
@Override
public Object instantiateItem(View view, int position) {
           View view = views.get(position);
           ((ViewPager) view).addView(view);
           return view;
}
@Override
public boolean isViewFromObject(View view, Object object) {
           return view == object;
}
 And you are done!!! Sweet.... You can find the whole source code here.

22 comments:

  1. Nice work but my app crashes everytime when sliding the second time (3rd page or back to first) at ViewRoot.draw No matter if i use your idea or inflating the views. can you say me what to do?

    ReplyDelete
  2. @Felix: Are you sure you have overridden the destroyItem method? Are you getting IllegalStateException? Or something else?

    ReplyDelete
  3. thank you, i had a useless cast in the destroyItem method...

    ReplyDelete
  4. is it possible on change view page, load activity or class?

    ReplyDelete
  5. I know that you could change it to Fragments. But, activities, I don't think so.

    ReplyDelete
  6. thank you,your post was very helpful.

    ReplyDelete
  7. Any help on how to replace active fragment with other one?

    ReplyDelete
  8. good work.... the post was helpful :)

    ReplyDelete
    Replies
    1. Great one.keep posting such useful things.

      Delete
  9. Great work. Thank you :)

    ReplyDelete
  10. someone got to be du.mb for calling your sample a simple.

    ReplyDelete
    Replies
    1. It is very simple actually. Here is the sample which is more complex: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

      Delete
  11. opps nevermind :) thanks

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Great work :) But can we have Vertical ViewPager as such? If yes please give hints how to implement it

    Thank you.

    ReplyDelete
  14. I have implemented a viewpager which contains different views inside it like, imageview then videoview etc. What I am trying to implement is pinch zoom, pan and drag on the viewpager so that irrespective of any specific view inside viewpager, I can perform pinch zoom, pan and drag on it.

    Another challenge is by default viewpager has a event for touch via which it performs switching of views inside it. If anybody went through this or have idea about this case, please advise me.

    Help is appreciated. Thanks :)

    ReplyDelete
  15. I have implemented a viewpager which contains different views inside it like, imageview then videoview etc. What I am trying to implement is pinch zoom, pan and drag on the viewpager so that irrespective of any specific view inside viewpager, I can perform pinch zoom, pan and drag on it.

    Another challenge is by default viewpager has a event for touch via which it performs switching of views inside it. If anybody went through this or have idea about this case, please advise me.

    Help is appreciated. Thanks :)

    ReplyDelete
  16. Thank U! Really good job !

    ReplyDelete
  17. thank you very much!

    ReplyDelete
  18. HI

    when ever i swap to some next image, and on back press i have finished the activity, but it ends only that image but not the all the imageview...

    so it has created a kinda loop for me which i dont want

    ReplyDelete
  19. thank you,your post was very helpful.

    ReplyDelete