ViewPager introduction and usage

Source: Internet
Author: User

1. functions implemented by ViewPager and running instructions

The ViewPager class provides new effects of multi-interface switching. The new effect has the following features:

[1] One of the interfaces in A group is currently displayed.

[2] when a user slides between the left and right, the current screen displays the current interface and a part of the next interface.

[3] After sliding, the page automatically jumps to the selected page

 

The following is the implementation


 

2. How to Use the ViewPager Control

 

2.1 reference the ViewPager Control

ViewPager comes from google's complementary component android-support-v13.jar, which is located in the androidSDK folder

Android-sdk-windows \ extras \ android \ compatibility \ v13

 

Reference android-support-v13.jar to a project

 

2.2 configuration page file

<Android. support. v4.view. ViewPager

Android: id = "@ + id/viewPager1"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: layout_alignLeft = "@ + id/textView1"

Android: layout_above = "@ id/adLayout"

Android: layout_below = "@ id/topText"

Android: layout_centerVertical = "true"/>

 

2.3 set the adapter of the ViewPager Control

The ViewPager adapter inherits from the PagerAdapter base class and implements the following four methods:

// Obtain the number of interfaces of the current form

Public int getCount ()

 

// Initialize the position page

Public Object instantiateItem (View collection, int position)

 

// Destroy the position page

Public void destroyItem (View collection, int position, Object view)

 

// Determine whether the interface is generated by the object

Public boolean isViewFromObject (View arg0, Object arg1)

 

3. initialize the ViewPager control.

Initialize the adapter of the ViewPager Control

 

ViewPager1 = (ViewPager) findViewById (R. id. viewPager1 );

ViewPager1.setAdapter (new ImgPagerAdapter (this, lists ));

 

Configure page change events for the adapter

ViewPager1

. SetOnPageChangeListener (new OnPageChangeListener (){

// Select on the page

@ Override

Public void onPageSelected (int position ){

TopText. setText (String. valueOf (position + 1) + "/" + String. valueOf (lists. length ));

}

 

@ Override

Public void onPageScrollStateChanged (int state ){

}

 

@ Override

Public void onPageScrolled (int position,

Float positionOffset, int positionOffsetPixels ){

}

});

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.