Android ViewPager: slide the page, androidviewpager

Source: Internet
Author: User

Android ViewPager: slide the page, androidviewpager

Next article: Android ViewPager re-exploration: Adding slide indicator bar

 

The adapter PagerAAdapter is required for ViewPager. The following four functions need to be rewritten:

InstantiateItem (ViewGroup container, int position): Creates a page view at a specified position. The adapter adds the View to the container specified here.
DestroyItem (ViewGroup container, int position, Object object): removes a page at a given position.
GetCount (): returns the number of valid views.
IsViewFromObject (View view, Object object): determines whether the Key returned by the instantiateItem (ViewGroup, int) function is the same View as that of a page view.

 

You only need to add the layout of the main interface

<Android. support. v4.view. ViewPager/>

You can:

Activity_main.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2                 xmlns:tools="http://schemas.android.com/tools" 3                 android:layout_width="match_parent" 4                 android:layout_height="match_parent" 5                 tools:context=".MainActivity"> 6  7     <android.support.v4.view.ViewPager 8         android:id="@+id/viewpager" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_gravity="center"/>12 13 </RelativeLayout>

 

It is determined that ViewPager has three pages, and each page must have a layout:

Take the first first_page.xml as an example:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 <TextView 6 android: layout_width = "match_parent" 7 android: layout_height = "match_parent" 8 android: gravity = "center" 9 android: text = "This is the first page" 10 android: textSize = "20sp"/> 11 12 </RelativeLayout>

 

MainActivity needs to initialize it in onCreate and call the adapter:

1 package com. example. hopecaploud. myapplication; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. support. v4.view. viewPager; 6 import android. view. layoutInflater; 7 import android. view. view; 8 9 import java. util. arrayList; 10 import java. util. list; 11 12 13 public class MainActivity extends Activity {14 private View first, second, third; 15 private ViewPager viewPager; // corresponds to <android. support. v4.view. viewPager/> Control 16 private List <View> viewList; // View array 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. activity_main); 22 23/* initialize */24 viewPager = (ViewPager) findViewById (R. id. viewpager); 25 LayoutInflater inflater = getLayoutInflater (); 26 first = inflater. inflate (R. layout. first_page, null); 27 second = inflater. inflate (R. layout. second_page, null); 28 third = inflater. inflate (R. layout. third_page, null); 29 30 viewList = new ArrayList <View> (); // Mount 31 viewList in the array to be displayed by page. add (first); 32 viewList. add (second); 33 viewList. add (third); 34 35/* adapter part */36 NewPagerAdapter pagerAdapter = new NewPagerAdapter (viewList); 37 viewPager. setAdapter (pagerAdapter); 38} 39 40}

 

The last and most important, inherited from PagerAdapter adapter:

NewPagerAdapter. java:

1 package com. example. hopecaploud. myapplication; 2 3 import android. support. v4.view. pagerAdapter; 4 import android. view. view; 5 import android. view. viewGroup; 6 7 import java. util. list; 8 9/** 10 * Created by LT on 2015/7/27. 11 */12 public class NewPagerAdapter extends PagerAdapter {13 public List <View> viewList; 14 15 public NewPagerAdapter (List <View> viewList) {16 this. viewList = viewList; 17} 18 19/* the following four functions must be overwritten */20 @ Override21 public boolean isViewFromObject (View arg0, Object arg1) {22 // judge whether the Key returned by the instantiateItem (ViewGroup, int) function is the same view as that returned by a page view (judge the key) 23 // TODO Auto-generated method stub24 return arg0 = arg1; 25} 26 27 @ Override28 public int getCount () {// return the number of views to slide 29 // TODO Auto-generated method stub30 return viewList. size (); 31} 32 33 @ Override34 public void destroyItem (ViewGroup container, int position, 35 Object object) {// Delete the View at the specified position from the current container; 36 // TODO Auto-generated method stub37 container. removeView (viewList. get (position); 38} 39 40 @ Override41 public Object instantiateItem (ViewGroup container, int position) {42 // instantiate: add the current view to the container, and return the current View (transfer key) 43 // TODO Auto-generated method stub44 container. addView (viewList. get (position); 45 46 return viewList. get (position); 47} 48}

 

Then, a simple ViewPager is completed.

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.