----------------ViewPager的功能就是可以使视图滑动
大概步骤 1所需要的对象 List<View> 用于装载ImageView 对象 根据图片数量把 图片依次添加设置到ImageView中 然后将ImageView 添加到List<View>中
2.设置ViewPager的apapter重写PagerAdapter方法
3.设置adapter
注意 需要导包:
android.support.v4
效果图
直接上代码:
public class MainActivity extends Activity { private List<View> views; private ViewPager viewpager; private ViewPageAdapter adapter; private Integer images[]={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LayoutInflater inflater=getLayoutInflater(); views=new ArrayList<View>(); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); for(int i=0;i<images.length;i++){ ImageView iv=new ImageView(this); iv.setLayoutParams(params); //设置布局大小和LinearLayout一样 iv.setScaleType(ScaleType.FIT_XY); iv.setImageResource(images[i]); views.add(iv); } viewpager=(ViewPager) findViewById(R.id.viewpager); adapter=new ViewPageAdapter(views); viewpager.setAdapter(adapter); //绑定回调函数 viewpager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } }
adapter:
package com.example.test_viewpager; import java.util.List; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; public class ViewPageAdapter extends PagerAdapter { private List<View> views; public ViewPageAdapter(List<View> views) { this.views=views; } @Override public void destroyItem(View view, int arg1, Object arg2) { // TODO Auto-generated method stub ((ViewPager)view).removeView(views.get(arg1)); //移除 } //完成更新 @Override public void finishUpdate(View view) { } //获得总数 @Override public int getCount() { if(views!=null){ return views.size(); } return 0; } //实例化Item @Override public Object instantiateItem(View view, int arg1) { ((ViewPager)view).addView(views.get(arg1),0); return views.get(arg1); } @Override public boolean isViewFromObject(View view, Object arg1) { // TODO Auto-generated method stub return view==arg1; } //恢复状态 @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { // TODO Auto-generated method stub } //保存状态 @Override public Parcelable saveState() { // TODO Auto-generated method stub return null; } //启动更新 @Override public void startUpdate(View arg0) { // TODO Auto-generated method stub } }
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>