给引导页添加pageControl

2017-03-10  本文已影响45人  csp

导航点在Android里面其实就是一个个图片,在ios里面是PageControl;
需要在xml里面的ViewPager布局里面添加ImageView,使用的时候实现接口:
implements ViewPager.OnPageChangeListener {
在其中的onPageSelected(int position)里面实现图片内容的切换:
public class Guide extends Activity implements ViewPager.OnPageChangeListener {

private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View>views;
private ImageView[] dots;
private int[] ids = {R.id.iv1,R.id.iv2,R.id.iv3};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.guide);
    initViews();
    initDots();
}

private void initViews(){

    LayoutInflater inflater = LayoutInflater.from(this);

    views = new ArrayList<View>();
    views.add(inflater.inflate(R.layout.one,null));
    views.add(inflater.inflate(R.layout.two,null));
    views.add(inflater.inflate(R.layout.three,null));

    vpAdapter = new ViewPagerAdapter(views,this);
    vp = (ViewPager)findViewById(R.id.viewPager);
    vp.setAdapter(vpAdapter);
    vp.addOnPageChangeListener(this);

}

private void initDots(){
    dots = new ImageView[views.size()];
    for (int i=0;i<views.size();i++){
        dots[i] = (ImageView)findViewById(ids[i]);
    }
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    for (int i=0;i<views.size();i++){
        if (position == i){
            dots[i].setImageResource(R.mipmap.ic_launcher);
        }else{
            dots[i].setImageResource(R.mipmap.ic_launcher_round);
        }
    }

}

@Override
public void onPageScrollStateChanged(int state) {

}

}

上一篇下一篇

猜你喜欢

热点阅读