简单的viewpager指示器

private void setCircleTab() {
       /**
        * 初始化小圆点
        */
       LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(28, 28);
       margin.setMargins(4, 0, 0, 0);
       layout_group.removeAllViews();
       imageViews = new ImageView[locations.length];
       for (int i = 0; i < locations.length; i++) {
           ImageView imageView = new ImageView(MainActivity.this);
           imageView.setLayoutParams(new LinearLayout.LayoutParams(16, 16));

           imageViews[i] = imageView;
           if (i == 0) {
               imageViews[i].setImageResource(R.mipmap.circle_white_32);
           } else {
               imageViews[i].setImageResource(R.mipmap.circle_gray_32);
           }
           layout_group.addView(imageViews[i], margin);
       }
   }

下面的代码在Viewpager的OnPageChangeListener中的onPageSelecter方法中使用,实现白色小点跟随Viewpager滑动

/**
    * 选项卡切换时,切换小圆点
    *
    * @param postion
    */
   private void selectCircle(int postion) {
       for (int i = 0; i < imageViews.length; i++) {
           imageViews[postion].setImageResource(R.mipmap.circle_white_32);
           if (postion != i) {
               imageViews[i].setImageResource(R.mipmap.circle_gray_32);
           }
       }
   }

LinearLayout的代码,居中显示效果较好


你可能感兴趣的:(简单的viewpager指示器)