我怎么能在gridview中只绘制一个项目

时间:2011-07-14 10:11:14

标签: android user-interface

大家:

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的高度以下的每个项目都被重新绘制,我该怎么做。

1 个答案:

答案 0 :(得分: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 -->
/>