ViewPager主要应用于Activity中的图片的切换,使用的也比较广泛,在这样写下当做学习的笔记。
其实ViewPager的使用与GridView、ListView的方法差不多,都需要适配器,只是适配器不一样。
1、先声明
private ViewPager viewPager;
2、获取ViewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
3、使用适配器适配
viewPager.setAdapter(adapter);
4、写一个适配器,一般重写四个方法也就可以了。
PagerAdapter adapter = new PagerAdapter() {
};
实例:
package com.example.viewpagerdemo; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerTitleStrip; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; public class MainActivity extends Activity { /** Called when the activity is first created. */ private ViewPager mViewPager; private PagerTitleStrip mPagerTitleStrip; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager)findViewById(R.id.viewpager); mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle); //将要分页显示的View装入数组中 LayoutInflater mLi = LayoutInflater.from(this); View view1 = mLi.inflate(R.layout.view1, null); View view2 = mLi.inflate(R.layout.view2, null); View view3 = mLi.inflate(R.layout.view3, null); //每个页面的Title数据 final ArrayList<View> views = new ArrayList<View>(); views.add(view1); views.add(view2); views.add(view3); final ArrayList<String> titles = new ArrayList<String>(); titles.add("tab1"); titles.add("tab2"); titles.add("tab3"); //填充ViewPager的数据适配器 // PagerAdapter mPagerAdapter = new PagerAdapter() { // // @Override // public boolean isViewFromObject(View arg0, Object arg1) { // return arg0 == arg1; // } // // @Override // public int getCount() { // return views.size(); // } // // @Override // public void destroyItem(View container, int position, Object object) { // ((ViewPager)container).removeView(views.get(position)); // } // // @Override // public CharSequence getPageTitle(int position) { // return titles.get(position); // } // // @Override // public Object instantiateItem(View container, int position) { // ((ViewPager)container).addView(views.get(position)); // return views.get(position); // } // }; PagerAdapter dAdapter = new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager)container).removeView(views.get(position)); //super.destroyItem(container, position, object); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub ((ViewPager)container).addView(views.get(position)); return views.get(position); } }; mViewPager.setAdapter(dAdapter); //mViewPager.setAdapter(mPagerAdapter); } }