Android's Viewpager

Source: Internet
Author: User

API Documentation:

The function of the Viewpager is to slide the view (unlike the Viewflipper's auto-slide, which is to be manually slid), just as the lanucher slides around.

So we can use it to make an app boot interface.

As follows:

Implementation steps:

1. Create Viewpager in the main interface layout.

2. Load the view of each interface.

3, set Pageradapter.

Attention:

When you implement a pageradapter, you must overwrite at least the following methods:

    • instantiateItem(ViewGroup, int)
    • destroyItem(ViewGroup, int, Object)
    • getCount()
    • isViewFromObject(View, Object)

Implementation code:

Mainactivity:

public class Mainactivity extends Activity {private Viewpager mviewpager;private View view1, View2, View3;private list< View> viewlist; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.viewpager); Initview ();  Pageradapter mpageradapter = new Pageradapter () {@Override public boolean isviewfromobject (View arg0,          Object arg1) {return arg0 = = Arg1;          } @Override public int getcount () {return viewlist.size ();               } @Override public void Destroyitem (ViewGroup container, int position, object object) {          Container.removeview (Viewlist.get (position)); } @Override public int getitemposition (Object object) {return Super.getitemposition (object          );         } @Override Public Object instantiateitem (viewgroup container, int position) {     Container.addview (Viewlist.get (position));          return Viewlist.get (position);  }      }; Mviewpager.setadapter (Mpageradapter);} void Initview () {Mviewpager = (Viewpager) Findviewbyid (r.id.viewpager); viewlist = new arraylist<view> (); Layoutinflater layoutinflate = Getlayoutinflater (). from (this); view1 = Layoutinflate.inflate (R.LAYOUT.LAYOUT1, NULL); View2 = layoutinflate.inflate (r.layout.layout2, null); VIEW3 = Layoutinflate.inflate (R.LAYOUT.LAYOUT3, NULL); Viewlist.add (View1); Viewlist.add (VIEW2); Viewlist.add (VIEW3);p ageradapter);}


2. Main layout: Main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical ">    < Android.support.v4.view.ViewPager        android:id= "@+id/viewpager"        android:layout_width= "Match_parent        " android:layout_height= "Match_parent"        android:layout_gravity= "center" >                  </ Android.support.v4.view.viewpager>  </LinearLayout>


3, each loaded sub-layout, here only one, the other two layout code just like the name is different.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/layout1 "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation=" vertical ">    <imageview        android:id=" @+id/iamge "        Android : layout_width= "match_parent"        android:layout_height= "match_parent"        android:src= "@drawable/A"/></ Linearlayout>


The above is an entry-level example, followed by a few more in-depth technical examples.

Android's Viewpager

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.