2015-02-10 50 views
-1

我需要滾動自定義視圖。爲了達到這個目的,我在XML佈局中做了一個scrollview,並添加了Relativelayout作爲其唯一的孩子。然後在運行時,我在RelativeLayout中添加自定義視圖。
CustomView類代碼如下:爲onClick監聽器設置動態添加的自定義視圖

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() +""); 
} 
+0

顯示你的聽衆代碼 – 2015-02-10 09:22:22

+0

發佈監聽功能! – 2015-02-10 10:23:20

回答

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


     } 
    }); 
+0

在for循環中添加此內容。它可能工作 – Nivedh 2015-02-10 09:35:41

+0

不工作已經試過這個! – 2015-02-10 10:18:05

+0

檢查它是否可以通過爲CustomView – Nivedh 2015-02-10 11:31:22

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]); 
     } 
+0

這給了我相同的結果,即最後添加的視圖的標籤。 – 2015-02-10 10:10:56

+0

嗨sohaib,我編輯了我的答案。使用setTag()而不是setId()來保存id並在onclick中獲取。嘗試這個。如果您有任何疑問,請告訴我。 – droidd 2015-02-10 10:18:14

+0

嗨,我試過你在上面發佈的解決方案,但它沒有奏效。返回上次添加的自定義視圖的ID。這真的很奇怪。 – 2015-02-10 10:28:16

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); 
+0

沒有幫助。 :( – 2015-02-10 12:45:09

相關問題