View Switching Using viewpager in Android

Source: Internet
Author: User

Added animations for viewflipper view switching and implemented views moving with gestures in Android to implement Views switching with gestures. Now, in Android, compatibility package provides viewpager for easier view switching, the implementation result is as follows:

The effect is the same as that of viewgroup, but the implementation process is simpler. viewpager is used for the new Android Market and Google + versions.

Let's talk about the implementation process:

The project directory is as follows:

The oncreate method of mypageractivity is as follows:

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Initpagecontent ();
Awesomeadapter = new mypageradapter ();
Awesomepager = (viewpager) findviewbyid (R. Id. awesomepager );
Awesomepager. setadapter (awesomeadapter );
}

The main. xml layout file introduces viewpager:

<? 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"
Android: Background = "# a4c639">
<Android. Support. v4.view. viewpager
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Id = "@ + ID/awesomepager"/>
</Linearlayout>

Mypageradapter inherits pageradapter:

Private class mypageradapter extends pageradapter {

@ Override
Public int getcount (){
Return images. length;
}

/**
* Create the page for the given position. The adapter is responsible
* For adding the view to the container given here, although it only
* Must ensure this is done by the time it returns from
* {@ Link # finishupdate ()}.
*
* @ Param container the containing view in which the page will be shown.
* @ Param position the page position to be instantiated.
* @ Return returns an object representing the new page. This does not
* Need To Be a view, but can be some other container of the page.
*/
@ Override
Public object instantiateitem (View Collection, int position ){

View view = getlayoutinflater (). Inflate (R. layout. Page, null );

Imageview = (imageview) view. findviewbyid (R. Id. imageid );
Imageview. setimagedrawable (images [position]);
(Viewpager) Collection). addview (view, 0 );
Return view;
}

/**
* Remove a page for the given position. The adapter is responsible
* For removing the view from its container, although it only must ensure
* This is done by the time it returns from {@ link # finishupdate ()}.
*
* @ Param container the containing view from which the page will be removed.
* @ Param position the page position to be removed.
* @ Param object the same object that was returned
* {@ Link # instantiateitem (view, INT )}.
*/
@ Override
Public void destroyitem (View Collection, int position, object view ){
(Viewpager) Collection). removeview (View) view );
}

@ Override
Public Boolean isviewfromobject (view, object ){
Return view = (View) object );
}

/**
* Called when the change in the shown pages has been completed. At this
* Point you must ensure that all of the pages have actually been added or
* Removed from the container as appropriate.
* @ Param container the containing view which is displaying this adapter's
* Page views.
*/
@ Override
Public void finishupdate (view arg0 ){}

@ Override
Public void restorestate (parcelable arg0, classloader arg1 ){}

@ Override
Public parcelable savestate (){
Return NULL;
}

@ Override
Public void startupdate (view arg0 ){}
}

The red code is responsible for loading layout and filling the view in layout. In this way, the view can be switched with gestures.

Source code see: http://bigcateasymorse.googlecode.com/svn/trunk/android-viewpager0.1/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.