Viewpager usage and implementation process

Source: Internet
Author: User

See the figure first:


The filling content on the page is the random keyword flying in and out animation effect, which will be updated later. Please ignore it now.


First, import the jar package:Android-support-v4.jar



Add viewpager layout to the layout File

        <android.support.v4.view.ViewPager            android:id="@+id/search_viewpager"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center" >        </android.support.v4.view.ViewPager>


Create two item la s for filling in viewpager


Then there is the activity, which mainly writes the sliding switching page between left and right, and the animation effect of another small image as the page switches the displacement.

Public class searchallcityactivity extends activity {private keywordsflow; private viewpager; private imageview; private list <View> lists = new arraylist <View> (); Private viewpageradapter; private bitmap cursor; private int offset; private int currentitem; private matrix = new matrix (); Private int bmwidth; private animation; private button shuaxin_sq, shuaxin_fl; public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. search_allcity); // slide the image imageview = (imageview) findviewbyid (R. id. viewpaget_img); // Add the pages of popular business districts and popular categories to the lists set of viewpager. add (getlayoutinflater (). inflate (R. layout. search_hot_shangqu, null); lists. add (getlayoutinflater (). inflate (R. layout. search_hot_fenlei, null); // initialize the initecursor (); adapter = new viewpageradapter (lists); viewpager = (viewpager) findviewbyid (R. id. search_viewpager); viewpager. setadapter (adapter); // viewpager sliding listener viewpager. setonpagechangelistener (New viewpager. onpagechangelistener () {@ overridepublic void onpageselected (INT arg0) {// todo auto-generated method stub // when sliding, The imageview on the top is slowly sliding through the animation switch (arg0) {Case 0: If (currentitem = 1) {animation = new translateanimation (offset * 2 + bmwidth, 0, 0, 0);} else if (currentitem = 2) {animation = new translateanimation (offset * 4 + 2 * bmwidth, 0, 0);} break; Case 1: If (currentitem = 0) {animation = new translateanimation (0, offset * 2 + bmwidth, 0, 0);} else if (currentitem = 2) {animation = new translateanimation (4 * Offset + 2 * bmwidth, offset * 2 + bmwidth, 0, 0);} break;} currentitem = arg0; animation. setduration (500); animation. setfillafter (true); imageview. startanimation (animation);} @ overridepublic void onpagescrolled (INT arg0, float arg1, int arg2) {// todo auto-generated method stub} @ overridepublic void merge (INT arg0) {// todo auto-generated method stub});}/*** calculates the position of the sliding image */private void initecursor () {cursor = bitmapfactory. decoderesource (getresources (), R. drawable. viewpager_img); bmwidth = cursor. getwidth (); displaymetrics DM; DM = getresources (). getdisplaymetrics (); offset = (DM. widthpixels-2 * bmwidth)/4; matrix. settranslate (offset, 0); imageview. setimagematrix (matrix); // The scaletype required for iamgeview is matrixcurrentitem = 0 ;}}


Finally, you cannot forget the adapter of viewpager.


Public class viewpageradapter extends pageradapter {list <View> viewlists; Public viewpageradapter (list <View> lists) {viewlists = lists;} @ override public int getcount () {// get size // todo auto-generated method stub return viewlists. size () ;}@ override public Boolean isviewfromobject (view arg0, object arg1) {// todo auto-generated method stub return arg0 = arg1 ;} @ override public void destroyitem (view, int position, object) // destroy item {(viewpager) view ). removeview (viewlists. get (position) ;}@ override public object instantiateitem (view, int position) // instantiate item {(viewpager) view ). addview (viewlists. get (position), 0); Return viewlists. get (position );}}



Viewpager usage and implementation process

Related Article

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.