为动态添加的自定义视图设置onClick侦听器

时间:2015-02-10 09:16:59

标签: android onclicklistener android-custom-view

我需要滚动自定义视图。为了实现这一点,我在XML布局中创建了一个scrollview,并将Relativelayout添加为其唯一的子项。然后在运行时我在RelativeLayout中添加我的自定义视图 CustomView类代码如下:

public class CustomView extends View {

int bgColor ;
Point x ;
Point y ;
Point z ;
int itemHeight;
String tag;


public CustomView(Context context) {
    super(context);
}

public void  setParameters ( int bgColor , Point a , Point b , Point c, int itemHeight , String text )
{
    this.bgColor = bgColor;
    this.x = a;
    this.y = b;
    this.z = c;
    this.itemHeight = itemHeight;
    this.tag = text;
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setStrokeWidth(4);
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setAntiAlias(true);
    Point a = new Point(x.x, x.y);
    Point b = new Point(y.x, y.y);
    Point c = new Point(z.x, z.y);
    Path path = new Path();
    path.setFillType(Path.FillType.EVEN_ODD);
    path.moveTo(x.x,x.y);
    path.lineTo(b.x, b.y);
    path.lineTo(c.x, c.y);
    path.lineTo(a.x, a.y);
    path.close();

    canvas.drawPath(path, paint);

   /* paint.setTextSize(60);
    canvas.drawText("h o m e",60,76,paint);*/


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       setMeasuredDimension(widthMeasureSpec , resolveSize( itemHeight , heightMeasureSpec ) );
}

 public String getCustomTag(){
     return this.tag;
 }



public String getColor(){
    return bgColor +" ";
}

}

这是在运行时在RelativeLayout中添加自定义视图的方法:

   private void drawViewsTest(int numItems,int screenWidth,int itemHeight,Context ctx)
   {
   Point firstPoint = new Point(0,0) ;
   Point secondPoint = new Point (0,itemHeight);
   Point thirdPoint = new Point (screenWidth,itemHeight/2);

   view = new CustomView[numItems];
   for(int i = 0; i < numItems; i++){
       view[i] = new CustomView(ctx);
       if(i%2 == 0){
           view[i].setParameters(Color.BLACK, firstPoint, secondPoint,    thirdPoint, secondPoint.y,  "men" + " ");
           view[i].startAnimation(an);
       }else{
           view[i].setParameters(Color.RED, firstPoint, secondPoint, thirdPoint, secondPoint.y, "women" + " " );
           view[i].startAnimation(anTwo);
       }
       view[i].setId(i + 1);
       view[i].setOnClickListener(this);
       firstPoint = thirdPoint;
       thirdPoint = secondPoint;
       secondPoint = new Point(firstPoint.x , firstPoint.y+itemHeight);
       rl.addView(view[i]);
   }
   setContentView(v);

}

我的问题是内部循环我为每个视图注册了onClickListener,并为每个单独的视图注册了setId。但是在clicklistener里面,无论我点击哪个视图,它都会返回上次添加视图的ID。 任何建议将不胜感激。!

这是听众代码

 @Override
 public void onClick(View view) {
   Log.e("Tag is " , view.getId() +"");
 }

3 个答案:

答案 0 :(得分:0)

 view[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

答案 1 :(得分:0)

将for循环更改为此。

  for(int i = 0; i < numItems; i++){
           view[i] = new CustomView(ctx);
           if(i%2 == 0){
               view[i].setParameters(Color.BLACK, firstPoint, secondPoint,    thirdPoint, secondPoint.y,  "men" + " ");
               view[i].startAnimation(an);
           }else{
               view[i].setParameters(Color.RED, firstPoint, secondPoint, thirdPoint, secondPoint.y, "women" + " " );
               view[i].startAnimation(anTwo);
           }
           view[i].setTag(i + 1);
          view[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         if(v.getTag()!=null)
         {
           int id=Integer.parseInt(v.getTag().toString())
           {

           }
         }

        }
    });
           firstPoint = thirdPoint;
           thirdPoint = secondPoint;
           secondPoint = new Point(firstPoint.x , firstPoint.y+itemHeight);
           rl.addView(view[i]);
       }

答案 2 :(得分:0)

 Check whether its works for you
private void drawViewsTest(int numItems,int screenWidth,int itemHeight,Context ctx)
       {
       Point firstPoint = new Point(0,0) ;
       Point secondPoint = new Point (0,itemHeight);
       Point thirdPoint = new Point (screenWidth,itemHeight/2);


       for(int i = 0; i < numItems; i++){
           CustomView view = new CustomView(ctx);
           if(i%2 == 0){
              view .setParameters(Color.BLACK, firstPoint, secondPoint,    thirdPoint, secondPoint.y,  "men" + " ");
               view .startAnimation(an);
           }else{
              view .setParameters(Color.RED, firstPoint, secondPoint, thirdPoint, secondPoint.y, "women" + " " );
               view .startAnimation(anTwo);
           }
           view .setTag("viewID"+i);
           view .setOnClickListener(this);
           firstPoint = thirdPoint;
           thirdPoint = secondPoint;
           secondPoint = new Point(firstPoint.x , firstPoint.y+itemHeight);
           rl.addView(view);
       }
       setContentView(v);