<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="center" >
</android.support.v4.view.ViewPager> </LinearLayout>
添加视图
List<View> viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
for(int i=0;i<;i++){
ImageView image=new ImageView(this);
image.setImageResource(R.drawable.abd);
viewList.add(image);
} MyAdapter pageAdapter = new MyAdapter(viewList);
ViewPager viewpage=(ViewPager)findViewById(R.id.viewpager);
viewpage.setAdapter(pageAdapter);
添加适配器
package sdf.ess; import java.util.List; import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup; public class MyAdapter extends PagerAdapter{
List<View> viewList; public MyAdapter(List<View> viewList2) {
this.viewList=viewList2;
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView(viewList.get(position));
} @Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager)container).addView(viewList.get(position));
return viewList.get(position);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return viewList.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
//添加viewpager标题用 不需要标题就不用重写
// @Override
/* public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titles.get(position); }*/
}