首页 > 代码库 > Android UI之ViewPager

Android UI之ViewPager

ViewPager需要android-support-v4.jar这个包的支持,它是Google提供的一个附加包。
ViewPager主要用来组织一组界面,并且通过左右滑动的方式来展示。与ListView类似,ViewPager也需要一个适配器,也就是PagerAdapter,来把这组界面填充到ViewPager中。

ViewPager的使用步骤:
1、往布局文件里加入ViewPager组件
2、加载要显示的界面布局
3、在Activity里实例化ViewPager组件,并设置它的适配器和监听事件

实例:
运行效果:

代码清单:
布局文件: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>

布局文件: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源代码文件: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);

        // 将要分页显示的View装入数组中
        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);
        // 设置ViewPager的适配器和监听事件
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, 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源代码文件: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.get(position), 0);

        return views.get(position);
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object arg2) {
        ((ViewPager) view).removeView(views.get(position));
    }

    @Override
    public boolean isViewFromObject(View view, Object arg1) {
        return (view == arg1);
    }
}

API知识点
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 that the view shows.

void setAdapter(PagerAdapter adapter)
Set a PagerAdapter that will supply views for this pager as needed.

void setOnPageChangeListener(ViewPager.OnPageChangeListener listener)
Set a listener that will 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 will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll.

abstract void onPageSelected(int position)
This method will be invoked when a new page becomes selected.

Android UI之ViewPager