2014-02-11 45 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