2011-07-14 21 views
0

大家:我如何能在GridView畫只是一個項目

gridView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
//    v.invalidate(0, 0, 5, 5); 
       v.invalidate(); //v is a ImageView here 
      } 
     }); 

我想只繪製V,但事實是低於V的身高每一個項目得到了重繪,我該怎麼辦。

回答

0
public class MyGridView extends GridView 
{ 
    private Bitmap background; 

    public MyGridView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     background = BitmapFactory.decodeResource(getResources(), R.drawable.bg); 
    } 
} 

@Override 
protected void dispatchDraw(Canvas canvas) 
{ 
    int count = getChildCount(); 
    int top = count > 0 ? getChildAt(0).getTop() : 0; 
    int backgroundWidth = background.getWidth(); 
    int backgroundHeight = background.getHeight(); 
    int width = getWidth(); 
    int height = getHeight(); 

    for (int y = top; y < height; y += backgroundHeight) 
    { 
     for (int x = 0; x < width; x += backgroundWidth) 
     { 
      canvas.drawBitmap(background, x, y, null); 
     } 
    } 

    super.dispatchDraw(canvas); 
} 

XML

<your.package.name.MyGridView 
    android:id="@+id/mygridview" 
    <!-- other GridView configuration parameters --> 
/> 
+0

??感謝ü的答覆,但可以dispatchDraw解決我的親?或者我不知道爲什麼無效(0,0,5,5)這麼想的工作,因爲它應該是 – pain

相關問題