带小圆点的viewpager

public class GuideActivityextends AppCompatActivityimplements ViewPager.OnPageChangeListener{

ViewPagervp;

int []imageIdArray;//图片资源数组

    ListviewList;//图片资源集合

    ViewGroupvg;//放置小圆点

//    实例化小圆点

    ImageViewiv_point;

ImageView []ivPointArray;

//    最后一页按钮

    ImageButtonib_start;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.guide);

ib_start = (ImageButton) findViewById(R.id.guide_button);

ib_start.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View view) {

Intent intent =new Intent("Z.l.f");

startActivity(intent);

finish();

}

});

//        加载viewPager

        initViewPager();

//        加载底部圆点

        initPoint();

}

//    加载底部圆点

    private void initPoint(){

vg = (ViewGroup) findViewById(R.id.guide_point);

//        根据Item实例化数组

        ivPointArray =new ImageView[viewList.size()];

//        循环底部圆点,将生成的Image保存到数组

        int size =viewList.size();

for (int i =0; i < size; i++) {

iv_point =new ImageView(this);

iv_point.setLayoutParams(new ViewGroup.LayoutParams(20,20));

iv_point.setPadding(30,0,30,0);//left,top,right,bottom

            ivPointArray[i] =iv_point;

//            第一个页面被设为选中状态,所以放两张不同的图片

            if (i ==0) {

iv_point.setBackgroundResource(R.drawable.point_press);

}else{

iv_point.setBackgroundResource(R.drawable.point_normal);

}

//            将数组中的Image加入到ViewGroup

            vg.addView(ivPointArray[i]);

}

}

//    加载图片

    private void initViewPager(){

mViewList =new ArrayList();

LayoutInflater lf = getLayoutInflater().from(MainActivity.this);

View view1 = lf.inflate(R.layout.we_indicator1,null);

View view2 = lf.inflate(R.layout.we_indicator2,null);

View view3 = lf.inflate(R.layout.we_indicator3,null);

mViewList.add(view1);

mViewList.add(view2);

mViewList.add(view3);

//        获取Layout参数,设为全局

        LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(

LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

//        循环创建View加入到集合中

        int len =imageIdArray.length;

for (int i =0; i < len; i++) {

//new Imageview 并设置全屏和图片资源

            ImageView imageview =new ImageView(this);

imageview.setLayoutParams(params);

imageview.setBackgroundResource(imageIdArray[i]);

viewList.add(imageview);

}

vp.setAdapter(new GuideAdapter(viewList));

vp.addOnPageChangeListener(this);

}

@Override

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

}

//滑动后监听

    @Override

    public void onPageSelected(int position) {

//循环设置当前页的标记图

        int length =imageIdArray.length;

for (int i =0; i < length; i++) {

ivPointArray[position].setBackgroundResource(R.drawable.point_press);

if (position != i){

ivPointArray[i].setBackgroundResource(R.drawable.point_normal);

}

}

//        判断是否是最后一页

        if (position ==imageIdArray.length -1) {

ib_start.setVisibility(View.VISIBLE);

}else{

ib_start.setVisibility(View.GONE);

}

}

@Override

    public void onPageScrollStateChanged(int state) {

}

}

你可能感兴趣的:(带小圆点的viewpager)