【问题标题】:Set onClick listener for dynamically added custom views为动态添加的自定义视图设置 onClick 监听器
【发布时间】:2015-02-10 09:16:58
【问题描述】:

我需要滚动自定义视图。为了实现这一点,我在 XML 布局中创建了一个滚动视图,并添加了 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() +"");
 }

【问题讨论】:

  • 显示你的监听代码
  • 发布监听函数!

标签: android onclicklistener android-custom-view


【解决方案1】:
 view[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

【讨论】:

  • 通过为CustomView创建不同的对象来检查它是否有效
  • 它可能会起作用。在这里,您拥有相同的对象,并且每次都返回相同对象的 id
  • 我正在创建不同的视图对象,因为我正在维护视图数组,并且每个视图在循环内的注册方式都不同。!
【解决方案2】:

将你的 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]);
       }

【讨论】:

  • 这给了我相同的结果,即最后添加的视图的标签。
  • 嗨sohaib,我编辑了我的答案。使用 setTag() 而不是 setId() 来保存 id 并在 onclick 中获取。尝试这个。如果您有任何疑问,请告诉我。
  • 您好,我尝试了您在上面发布的解决方案,但没有成功。返回我添加的最后一个自定义视图的 id。这真的很奇怪。
【解决方案3】:
 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多