布局:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

java代码

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<ImageView> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager_main);
        initImageViews();
        MyPagerAdapter adapter = new MyPagerAdapter();
        viewPager.setAdapter(adapter);
    }

    private void initImageViews() {
        list = new ArrayList<ImageView>();
        //设置宽度和高度
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        //设置图片1.
        ImageView image1 = new ImageView(this);
        image1.setLayoutParams(params);
        image1.setImageResource(R.mipmap.guide_1);
        //设置图片2.
        ImageView image2 = new ImageView(this);
        image2.setLayoutParams(params);
        image2.setImageResource(R.mipmap.guide_2);
        //设置图片3.
        ImageView image3 = new ImageView(this);
        image3.setLayoutParams(params);
        image3.setImageResource(R.mipmap.guide_3);
        //添加到集合中
        list.add(image1);
        list.add(image2);
        list.add(image3);
    }

    //自己创建一个PageAdapter的子类。
    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//判断是否是同一个对象,如果是,则不创建,否则就创建。
        }

        //需要添加对象到容器中。

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        //销毁
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
            // container.removeView((View) object);
        }
    }
}