我想创建一个带架子的 GridView。我为此创建了一个类:
public class ShelveGridView extends GridView{
private Bitmap background;
public ShelveGridView(Context context, AttributeSet attrs) {
super(context, attrs);
background = BitmapFactory.decodeResource(getResources(), R.drawable.bibliotca_image_shelf);
}
@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 x = 0; x < width; x += backgroundWidth)
for (int y = top; y < height; y += backgroundHeight)
canvas.drawBitmap(background, x, y, null);
super.dispatchDraw(canvas);
}...
但是当我滚动 GridView 时,货架不会移动。我想我必须在这个类中覆盖一些方法来在我滚动时移动位图背景。有什么建议吗?