2013-04-11 55 views
0

我想创建一个带有架子的GridView。我为此创建了一个类:Android:带有滚动架的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时,货架不能移动。我想我必须在这个类中覆盖一些方法来移动位图背景,同时进行滚动。任何建议?

回答

2

你没有正确接近这个。以这种方式设置背景不会使其滚动。有几种方法可以使背景滚动:

  1. 组一个架子作为一个列表项
  2. 设置水平搁架分隔图像的背景和设定大陆架的列表项后面的图像。
  3. 将水平搁板设置为分隔图像,并将搁板的背面设置为列表视图的背景(就像现在使用整个搁板图像一样)。

方法3将产生最接近的结果,这就是苹果的iBooks的工作原理。

+0

谢谢你,我会尝试使用选项2. – Rincha 2013-04-11 11:12:49

+0

@Rincha选项2将产生所需的结果,只有当你有足够的项目来填充整个列表的区域(我宽松地认为一个网格是行列表) 。如果在底部有空行,它会看起来相当有趣:) – 2013-04-11 11:14:24

+0

@Rincha找到答案...? – 2015-10-12 11:35:50