Pagecontrolandroid中的实现

Android中没有PageControl控件 自己实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class PageControl extends LinearLayout {
  private int count = 0;
  private int selected = 0;
  private int nomal_icon_id = R.drawable.pagecontro_icon_normal;
  private int foucsed_icon_id = R.drawable.pagecontro_icon_foucsed;
  private Context context;
  public PageControl(Context context) {
      super(context);
      this.context = context;
  }
  
  public PageControl(Context context, AttributeSet attrs){
      super(context, attrs);
      this.context = context;
      generateView();
  }
  
  public PageControl(Context context, int count) {
      super(context);
      this.count = count;
      this.context = context;
      generateView();
  }

  private void generateView(){
      removeAllViews();
      LayoutParams params =  new LayoutParams(context.getResources().getDimensionPixelSize(R.dimen.home_banner_pagecontrol_shape),
              context.getResources().getDimensionPixelSize(R.dimen.home_banner_pagecontrol_shape));
      params.setMargins(10, 0, 0, 10);
      for(int i = 0; i < count ; i++){
          ImageView indicatorV = new ImageView(context);
          if (i == selected) {
              indicatorV.setImageResource(foucsed_icon_id);
          }else{
              indicatorV.setImageResource(nomal_icon_id);
          }
          addView(indicatorV, params);
      }
  }
  
  public void setCount(int count){
      this.count = count;
      generateView();
  }
  
  public void setSelected(int index){
      this.selected = index;
      generateView();
  }
  
  public void setIconId(int nomalId, int foucsedId){
      this.nomal_icon_id = nomalId;
      this.foucsed_icon_id = foucsedId;
  }
}

直接在布局中定义就可以使用了。