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));
我看到那裏的圖像
謝謝,我沒有包括getCount()只是爲了簡化問題,是的,它是在代碼中,否則它不會編譯。我曾嘗試設置寬度和高度像這樣\t \t this.addView(image,200,200); 但它沒有幫助 –
你有沒有試過用這樣的LayoutParams:'addView(image,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT))'? – SDJMcHattie
當您從適配器中的getView()'返回它時,還要在LinearLayout本身上設置LayoutParams – SDJMcHattie