Ricardo
Ricardo

Reputation: 2321

Android: GridView with shelves with scroll

I want to create a GridView with shelves. I have created a class for this:

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);
}...

but when I scroll the GridView, shelves not move. I guess that I have to overwrite some method in this class to move the Bitmap background while I do scroll. Any suggestion?

Upvotes: 0

Views: 745

Answers (1)

Aleks G
Aleks G

Reputation: 57346

You're not approaching this correctly. Setting a background in this way will not make it scrollable. There are several ways to make the background scroll:

  1. Set one shelf as a background for a list item
  2. Set the horizontal shelf as the divider image and set the back of the shelf as the image for list item.
  3. Set the horizontal shelf as the divider image and set the back of the shelf as the background of the listview (as you do now with the whole shelf image).

Method 3 will produce the closest results and this is how apple's iBooks works.

Upvotes: 2

Related Questions