Android:GridView,带滚动的书架

Android:GridView,带滚动的书架,android,gridview,scroll,Android,Gridview,Scroll,我想创建一个带有工具架的GridView。我已为此创建了一个类: public class ShelveGridView extends GridView{ private Bitmap background; public ShelveGridView(Context context, AttributeSet attrs) { super(context, attrs); background = BitmapFactory.decodeResource(getResou

我想创建一个带有工具架的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);
}...
public类ShelveGridView扩展了GridView{
私有位图背景;
公共ShelveGridView(上下文、属性集属性){
超级(上下文,attrs);
background=BitmapFactory.decodeResource(getResources(),R.drawable.bibliotca\u image\u shelf);
}
@凌驾
受保护的void dispatchDraw(画布){
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();
对于(int x=0;x

但是,当我滚动GridView时,工具架不会移动。我想我必须重写此类中的某些方法,以便在滚动时移动位图背景。有什么建议吗?

您没有正确处理此问题。以这种方式设置背景不会使其可滚动。有几种方法可以使背景滚动:

  • 将一个工具架设置为列表项的背景
  • 将水平搁板设置为分隔器图像,并将搁板背面设置为列表项的图像
  • 将水平搁板设置为分隔器图像,并将搁板的背面设置为listview的背景(就像您现在对整个搁板图像所做的那样)

  • 方法3将产生最接近的结果,这就是苹果的iBooWork如何工作。

    谢谢你,我将尝试选项2。@ RunChar选项2将产生期望的结果,只有当你有足够的项目来填充列表的整个区域时(我松散地考虑网格是行列表)。.如果底部有空行,看起来会很有趣:)@Rincha找到答案了。。。?