At first, the first two screens that were exposed to the left and right were switched in
Innovation stems from imitation 3: switching between the two screens left and right. In this article, I would like to thank the author for providing us with such a good demo.
What is viewpager?
It is a class that enables smooth switching between the left and right screens. It is provided by Google and supplemented by Android. First, let everyone have an intuitive understanding.
When we search for related topics online, we will see many questions about this: how to smooth? Horizontal movement? And so on. Now, we can not do this laborious task by ourselves. We only need to use a class called viewpager. Viewpager is the latest in Compatibility
The package is released and supports Android 1.6 +. If you want to use it, update compatibility package through SDK manager, specific Use method in the <sdk_doc_path>/SDK/compatibility-library.html (sdk_doc_path is the location of the built-in Android documentation, need to be updated to the latest documentation ).
Innovation comes from imitation 3: switching between the left and right screens. This provides a workspace for switching between the left and right screens. Now, this workspace class has been released on Google I/O, it is easy for us to use. I will release it together with the source code of this demo later. What is the difference between viewpager and workspace? Take a look at the API
Doc statement:
Layout manager that allows the user to Flip left and right through pages of data. You supply
An implementation ofPagerAdapter
To
Generate the pages that the view shows.
As mentioned above, viewpager provides an adapter for us to control various views, while workspace needs to be manually added, which is equivalent to a "static switch ". Next I will introduce how to use it:
1. Download the android SDK & AVD Manager Update: available
Packages-> Android repository-> Android compatibility package, Revision 3. It will be installed under the \ extras \ Android \ Compatibility \ V4 File
2. Create an android project, add the android-support-v4.jar to the project under the above installation directory, if it is the latest ADT, just right click Project-> android
Tools-> Add compatibility Library
3. Use viewpager in Activity
4. If viewpager is defined in XML, write the global path, for example:
<android.support.v4.view.ViewPagerandroid:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/awesomepager"/>
5. Create a pageradapter and implement the following methods:
Getcount (), instantiateitem (), destroyitem (), isviewfromobject ().
As mentioned above, we have pasted the code and found that switching between the left and right screens can be implemented very quickly and easily.
Public class extends activity {private viewpager awesomepager; private context cxt; private extends awesomeadapter; private layoutinflater minflater; private list <View> mlistviews;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); cxt = This; awesomeadapter = new awesomepageradapter (); awesomepager = (viewpager) findviewbyid (R. id. awesomepager); awesomepager. setadapter (awesomeadapter); mlistviews = new arraylist <View> (); minflater = getlayoutinflater (); mlistviews. add (minflater. inflate (R. layout. layout1, null); mlistviews. add (minflater. inflate (R. layout. layout2, null); mlistviews. add (minflater. inflate (R. layout. layout3, null);} private class awesomepageradapter extends pageradapter {@ overridepublic int getcount () {return mlistviews. size ();}/*** create a page from the specified position ** @ Param container viewpager container * @ Param position the page position to be instantiated. * @ return returns the page with the specified position. It does not need to be a view or other view containers. * // @ overridepublic object instantiateitem (View Collection, int position) {(viewpager) collection ). addview (mlistviews. get (position), 0); Return mlistviews. get (position);}/*** destroy page from the specified position *** parameter same as above */@ overridepublic void destroyitem (View Collection, int position, object view) {(viewpager) collection ). removeview (mlistviews. get (position) ;}@ overridepublic Boolean isviewfromobject (view, object) {return view == (object) ;}@ overridepublic void finishupdate (view arg0) {}@ overridepublic void restorestate (parcelable arg0, classloader arg1) {}@ overridepublic parcelable savestate () {return NULL ;}@ overridepublic void startupdate (view arg0 ){}}}
In this way, we can easily switch between the two screens. The content on the specific page depends on everyone!
Code: http://download.csdn.net/source/3488073
All rights reserved. For more information, see the source!