首先布局文件添加ViewPager控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
</LinearLayout>
然后在Activity得到控件 添加需要展示的view 设置adapter
重写两个方法destroyItem(//滑动切换的时候销毁当前的组件)
instantiateItem(//每次滑动的时候生成的组件)
package com.example.gs_pc.zgs;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Gs-pc on 2016/7/30.
*/
public class GuideAcitivty extends Activity{
private ViewPager vp;
private int Image[] = {R.drawable.q,R.drawable.w,R.drawable.e};
private List<ImageView>list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide_activity);
vp = (ViewPager) findViewById(R.id.vp);
list = new ArrayList<ImageView>();
for (int i = 0;i<Image.length;i++){
ImageView iv = new ImageView(this);
iv.setBackgroundResource(Image[i]);
list.add(iv);
}
vp.setAdapter(new MyAdapter());
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv = list.get(position);
container.addView(iv);
return iv;
}
}
}