ViewFlipper实现带圆点的banner轮播图

1 布局文件定义ViewFlipper以及小圆点

         

                

                
                

                

            

2 activity中先定义几个变量

@BindView(R.id.ll_banner_dot)
LinearLayout ll_banner_dot;//小圆点的父布局

@BindView(R.id.vf_banner)
ViewFlipper vf_banner;//viewflipper
private List dotlist;//圆点列表,将自定义的圆点imageview放到里边去
private int bannerCount;//轮播图数量
private int bannerIndex;//轮播图起始索引

 

3 根据服务器获取到的banner数据动态生成banner图的imageview和小圆点的imageview

for(BannerData bannerData:bannerlist){
           ImageView imageView = new ImageView(getContext());
           LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
           imageView.setLayoutParams(params);
           imageView.setScaleType(ImageView.ScaleType.FIT_XY);
           vf_banner.addView(imageView);
           Glide.with(this).load(bannerData.getImagePath()).into(imageView);

           //动态生成小圆点
           ImageView iv_banner = new ImageView(this.getContext());
           LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(35,35);
           params1.gravity= Gravity.CENTER;
           params1.leftMargin=10;
           iv_banner.setLayoutParams(params1);
           iv_banner.setBackgroundResource(R.drawable.dian);
           ll_banner_dot.addView(iv_banner);
           dotlist.add(iv_banner);
           

       }

4 通过实现viewflipper的getInAnimation().setAnimationListener()监听来同步改变小圆点的背景

vf_banner.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                if(bannerIndex>=dotlist.size()){
                    bannerIndex=0;//重置索引
                }
                
                for(int index=0;index

 

你可能感兴趣的:(Android)