"Android UI design and development" 1. Boot interface (i) Viewpager Introduction and simple implementation

Source: Internet
Author: User

1.ViewPager implementations

2.ViewPager Implementation Capabilities

The Viewpager class provides a new effect for multi-interface switching, with the following characteristics:

<1> currently displays one of the interfaces in a set of interfaces;

<2> when the user slides the interface through the left and right, the current screen displays the current interface and part of the next interface;

<3> after the swipe is finished, the interface automatically jumps to the currently selected interface.

3.Viewpager Detailed Description

Android-support-v4.jar is a software package that Google has provided to us that is compatible with the low-version Android device, which is encapsulated with APIs that are available only on Android 3.0. And Viewpager is one of them. Using it, we can do a lot of things, from the simplest navigation, to the page menu and so on. So how to use it, like a ListView, we also need an adapter, he is pageradapter.

Viewpager corresponding Official document address: http://developer.android.com/reference/android/support/v4/view/ViewPager.html

4, the use of Viewpager

Three steps to use it:

1. Add the component to the layout file

< Android.support.v4.view.ViewPager   
Android:id = "@+id/viewpager"      android:layout_width= "Wrap_content"     android:layout_height= "Wrap_ Content "     android:layout_gravity=" center ">   

Note: This component is used to display the left and right sliding interface , and if the XML layout file is not loaded , He is not displaying the contents of the

2, load the page to be displayed

Layoutinflater lf = Getlayoutinflater (). From (this);           NULL );           NULL );           NULL );             New Arraylist<view> (); // The view that will be paginated is loaded into the   array         Viewlist.add (view1);          Viewlist.add (VIEW2);          Viewlist.add (VIEW3);  

3, in the activity to instantiate the Viewpager component, and set its adapter (that is, Pageradapter, the same method as the ListView), here generally need to rewrite pageradapter.

 Public classMyviewpageradapterextendsPageradapter {PrivateList<view>mlistviews;  PublicMyviewpageradapter (list<view>mlistviews) {             This. mlistviews = Mlistviews;//construction method, parameter is our page card, this is more convenient. } @Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {Container.removeview (Mlistviews.get (position));//Delete a page card} @Override PublicObject Instantiateitem (ViewGroup container,intPosition) {//This method is used to instantiate a page cardContainer.addview (Mlistviews.get (position), 0);//Add a page card        returnMlistviews.get (position); } @Override Public intGetCount () {returnMlistviews.size ();//returns the number of page cards} @Override Public BooleanIsviewfromobject (View arg0, Object arg1) {returnARG0==ARG1;    }}

Viewpager adapter is Pageradapter, it is the base class that provides adapters to populate the page Viewpager inside, and you most likely want to use a more specific implementation, such as Fragmentpageradapter or Fragmentstatepageradapter. Here to explain, in fact, Viewpager should be used with fragment, at least Google's official think so, but under 3.0, we do not need to do so. It is important to note that when you implement a pageradapter, you must overwrite at least the following methods:

intint, object) GetCount () Isviewfromobject (View, Object)

The official document address for the Pageradapter is: http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html

Source code Address: Https://github.com/YeXiaoChao/Yc_ui_viewpager

Source: http://blog.csdn.net/yangyu20121224/article/details/8980917

"Android UI design and development" 1. Boot interface (i) Viewpager Introduction and simple implementation

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.