先上代码 package ui; import java.util.ArrayList; import com.example.play.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.ImageView; import android.widget.Toast; public class Welcome extends Activity { @SuppressWarnings("unused") private final int[] images = new int[] { R.drawable.diyizhang, R.drawable.dierzhang, R.drawable.disanzhang, R.drawable.disizhang }; private ImageView image; private ArrayList<ImageView> imageviews; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_wlecome); // 实例化组建 initview(); vp.setAdapter(new GuideAdaptet()); vp.setOnPageChangeListener(new GuideChangeListener()); } private void initview() { vp = (ViewPager) findViewById(R.id.vp_guide); imageviews = new ArrayList<ImageView>(); for (int i = 0; i < images.length; i++) { image = new ImageView(this); // 将图片添加到image组建中 image.setBackgroundResource(images[i]); imageviews.add(image); } } /** * 设置viewpager的适配器 * * @author Administrator * */ class GuideAdaptet extends PagerAdapter { @Override public int getCount() { return images.length; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageviews.get(position)); return imageviews.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } /** * 设置viewpager的事件 */ class GuideChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } /** * 当选中的posion */ @Override public void onPageSelected(int arg0) { if (arg0 == 0) { Toast.makeText(Welcome.this, "wo shi di yi zhang ", 0).show(); } if (arg0 == 1) { Toast.makeText(Welcome.this, "wo shi di er zhang ", 0).show(); } if (arg0 == 2) { Toast.makeText(Welcome.this, "wo shi di san zhang ", 0).show(); } if (arg0 == 3) { Toast.makeText(Welcome.this, "wo shi di si zhang ", 0).show(); Intent intent = new Intent(); intent.setClass(Welcome.this, Splash.class); startActivity(intent); } } } }
布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/vp_guide" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>