Viewpager of Android UI

Source: Internet
Author: User
Tags object object

Viewpager needs Android-support-v4.jar support for this package, which is an add-on package provided by Google.
Viewpager is primarily used to organize a set of interfaces and to display them in a way that slides left and right. Like a ListView, Viewpager also needs an adapter, Pageradapter, to populate this set of interfaces into Viewpager.

Viewpager Steps to use:
1. Add the Viewpager component to the layout file
2. Load the interface layout to be displayed
3. Instantiate the Viewpager component in activity and set its adapter and listener events

Instance:
Operating effect:

Code Listing:
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:layout_width=" Wrap_content    " android:layout_height= "Wrap_content" >     <android.support.v4.view.viewpager        android:id= "@+id/ Viewpager "        android:layout_width=" fill_parent "        android:layout_height=" Fill_parent "/></ Relativelayout>

Layout file: Viewpager_page1.xml
<?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:gravity=" center "    >    <textview        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"        android:text= "Page1"    /></linearlayout>

Java source code file: Mainactivity.java
Package Com.rainsong.viewpagerdemo;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle ; Import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.layoutinflater;import Android.view.view;import Android.widget.toast;public class MainActivity extends    Activity implements Onpagechangelistener {Viewpager Mviewpager;    Viewpageradapter Mviewpageradapter;     Arraylist<view> mviews; /** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        The view to be paginated is loaded into the array layoutinflater inflater = Getlayoutinflater ();        Mviews = new arraylist<view> ();        Mviews.add (inflater.inflate (r.layout.viewpager_page1, null));        Mviews.add (inflater.inflate (r.layout.viewpager_page2, null)); Mviews.add (Inflater.inflate (r.layout.viewpageR_page3, null));        Mviews.add (inflater.inflate (r.layout.viewpager_page4, null));        Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager);        Mviewpageradapter = new Viewpageradapter (mviews);        Set Viewpager adapter and listen for event mviewpager.setadapter (Mviewpageradapter);    Mviewpager.setonpagechangelistener (this); } @Override public void onpagescrollstatechanged (int. arg0) {} @Override public void onpagescrolled (int arg 0, float arg1, int arg2) {} @Override public void onpageselected (int position) {int pageId = position +        1;    Toast.maketext (mainactivity.this, "page" + pageId + "Selected", Toast.length_short). Show (); }}

Java source code file: Viewpageradapter.java
Package Com.rainsong.viewpagerdemo;import Java.util.arraylist;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.view.view;import Android.view.viewgroup;public Class    Viewpageradapter extends Pageradapter {private arraylist<view> views;    Public Viewpageradapter (arraylist<view> views) {this.views = views;         } @Override public int getcount () {if (views! = null) {return views.size ();    } return 0; } @Override Public Object instantiateitem (viewgroup view, int position) {(Viewpager) view. AddView (views.ge        T (position), 0);    return Views.get (position); } @Override public void Destroyitem (viewgroup view, int position, Object arg2) {(Viewpager) view. Removevie    W (views.get (position));    } @Override Public Boolean isviewfromobject (view view, Object arg1) {return (view = = Arg1); }}

API Knowledge Points
public class
Viewpager
Extends ViewGroup

Class Overview
Layout Manager that allows the user-to-flip left and right through pages of data. You supply an implementation of a pageradapter to generate the pages of the view shows.

void Setadapter (Pageradapter adapter)
Set a pageradapter that would supply views for this pager as needed.

void Setonpagechangelistener (Viewpager.onpagechangelistener Listener)
Set a listener that'll be invoked whenever the page changes or is incrementally scrolled.

Public abstract class
Pageradapter
Extends Object

Known Direct subclasses
Fragmentpageradapter, Fragmentstatepageradapter

Class Overview
Base class providing the adapter to populate pages inside of a viewpager.

When you implement a pageradapter, you must override the following methods at minimum:
Instantiateitem (ViewGroup, int.)
Destroyitem (viewgroup, int, Object)
GetCount ()
Isviewfromobject (View, Object)

Object Instantiateitem (ViewGroup container, int position)
Create the page for the given position.

void Destroyitem (ViewGroup container, int position, object object)
Remove a page for the given position.

abstract int GetCount ()
Return the number of views available.

Abstract Boolean Isviewfromobject (View view, Object object)
Determines whether a page View is associated with a specific key object as returned by Instantiateitem (viewgroup, int).

public static interface
Viewpager.onpagechangelistener

Class Overview
Callback interface for responding to changing state of the selected page.

abstract void onpagescrollstatechanged (int state)
Called when the scroll state changes.

abstract void onpagescrolled (int position, float positionoffset, int positionoffsetpixels)
This method was invoked when the current page was scrolled, either as part of a programmatically initiated smooth Scrol L or a user initiated touch scroll.

abstract void onpageselected (int position)
This method is invoked when a new page becomes selected.

Viewpager of Android UI

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.