2014-02-11 43 views
1

我需要将GridView中的项目作为单独的对象进行重绘,例如在下载所代表的游戏时显示进度条。所以我为View创建了一个包装器。 问题是网格项目根本不显示。 为简单起见,我只在LibraryAlbumView中添加了一张图片。
如果我setBackgroundColor(0xFF00FF00);我在网格单元中看到这个绿色背景,但没有其他的东西。 在我的包装:Android GridView Adapter显示空白视图

public class LibraryAlbumView extends LinearLayout { 
private Context context; 
public LibraryAlbumView(Context context) { 
    super(context); 
    this.context = context; 
    ImageView image; 
    image = new ImageView(context); 
    image.setImageDrawable(context.getResources().getDrawable(
      R.drawable.delete)); 
    this.addView(image); 

} 

和我的适配器代码:

public class LibraryAlbumAdapter extends BaseAdapter { 
private Context context; 

public LibraryAlbumAdapter(Context context) { 
    this.context = context; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     gridItem = new LibraryAlbumView(context); 
     return gridItem; 
      } else { 
      return (LibraryAlbumView) convertView; 
    } 
然而

,如果我的包装改为LibraryAlbumView extends ImageView 并在其构造我更改为:

  super(context); 
    this.context = context; 
    setImageDrawable(context.getResources().getDrawable(
      R.drawable.delete)); 

我看到那里的图像

回答

0

它看起来这是因为你没有覆盖你的适配器中的其他重要方法,如getCount()。由于Adapter的默认实现返回零,因此GridView认为您没有要显示的项目。

除了上述信息,它看起来像你不是为ImageView设置LayoutParams等等LinearLayout不懂得如何表达它,并选择给它零的宽度和高度,这可以解释为什么你看到绿色的背景,没有别的。

+0

谢谢,我没有包括getCount()只是为了简化问题,是的,它是在代码中,否则它不会编译。我曾尝试设置宽度和高度像这样\t \t this.addView(image,200,200); 但它没有帮助 –

+0

你有没有试过用这样的LayoutParams:'addView(image,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT))'? – SDJMcHattie

+0

当您从适配器中的getView()'返回它时,还要在LinearLayout本身上设置LayoutParams – SDJMcHattie