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
Setting up the ViewPager
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.
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"?>In this sample, we are using an extra TextView which would show the current page you are on.
<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>
Setting up the ViewPager
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.ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);MyPagerAdapter adapter = new MyPagerAdapter(this);viewPager.setAdapter(adapter);
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.
And you are done!!! Sweet.... You can find the whole source code here.@Overridepublic void destroyItem(View view, int arg1, Object object) {((ViewPager) view).removeView((View)object);}@Overridepublic int getCount() {
return views.size();}@Overridepublic Object instantiateItem(View view, int position) {
View view = views.get(position);
((ViewPager) view).addView(view);
return view;}@Overridepublic boolean isViewFromObject(View view, Object object) {}
return view == object;
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@Felix: Are you sure you have overridden the destroyItem method? Are you getting IllegalStateException? Or something else?
ReplyDeletethank you, i had a useless cast in the destroyItem method...
ReplyDeleteis it possible on change view page, load activity or class?
ReplyDeleteI know that you could change it to Fragments. But, activities, I don't think so.
ReplyDeletethank you,your post was very helpful.
ReplyDeleteAny help on how to replace active fragment with other one?
ReplyDeletegood work.... the post was helpful :)
ReplyDeleteGreat one.keep posting such useful things.
DeleteGreat work. Thank you :)
ReplyDeletesomeone got to be du.mb for calling your sample a simple.
ReplyDeleteIt 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/
Deleteopps nevermind :) thanks
ReplyDeleteGreat work!!!!!!!!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat work :) But can we have Vertical ViewPager as such? If yes please give hints how to implement it
ReplyDeleteThank you.
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.
ReplyDeleteAnother 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 :)
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.
ReplyDeleteAnother 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 :)
Thank U! Really good job !
ReplyDeletethank you very much!
ReplyDeleteHI
ReplyDeletewhen 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
thank you,your post was very helpful.
ReplyDelete