2013-07-24 33 views
1

這裏相同的圖像是爲GridView的代碼,它示出了在重複相同的圖像,樣本圖像2和3中示出了一遍又一遍,我應該怎麼辦?gridview的表示重複

公共類GridViewImageAdapter延伸BaseAdapter { 私人語境mContext;

public GridViewImageAdapter(Context c) { 
    mContext = c; 
} 

public int getCount() { 
    return mThumbIds.length; 
} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

// create a new ImageView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
    //ImageView imageView; 
    ImageView icon; 

    if (convertView == null) { // if it's not recycled, initialize some attributes 


     icon = new ImageView(mContext); 

     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row=inflater.inflate(R.layout.grid_icon, parent, false); 

     icon=(ImageView)row.findViewById(R.id.album_image); 
     icon.setScaleType(ImageView.ScaleType.CENTER_CROP); 

     icon.setImageResource(mThumbIds[position]); 


     //overlay 
     View overlay = (View) row.findViewById(R.id.overlay); 
     int opacity = 100; // from 0 to 255 
     overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha 
     FrameLayout.LayoutParams params = 
      new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 600); 
     params.gravity = Gravity.CENTER; 
     overlay.setLayoutParams(params); 
     overlay.invalidate(); 

     return row; 
     } 
    return convertView; 

} 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 

    // TODO Auto-generated method stub 

} 

// references to our images 
private Integer[] mThumbIds = { 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1 
}; 

}

回答

1

這是監守你重複返回相同的看法。

convertView是重用視圖,因此一旦它被使用,並不再爲空,你不改變它返回它。

更改這個

if (convertView == null) { // if it's not recycled, initialize some attributes 


     icon = new ImageView(mContext); 

     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = inflater.inflate(R.layout.grid_icon, parent, false); 

    } 


    icon=(ImageView)row.findViewById(R.id.album_image); 
    icon.setScaleType(ImageView.ScaleType.CENTER_CROP); 

    icon.setImageResource(mThumbIds[position]); 


    //overlay 
    View overlay = (View) convertView.findViewById(R.id.overlay); 
    int opacity = 100; // from 0 to 255 
    overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha 
    FrameLayout.LayoutParams params = 
     new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 600); 
    params.gravity = Gravity.CENTER; 
    overlay.setLayoutParams(params); 
    overlay.invalidate(); 

    return convertView; 

以上應該工作,這是低效的,雖然,你應該看看了ViewHolder模式

+0

非常感謝你爲眼前的答案,但日食說行不能得到解決,有什麼我應該怎麼做? –

+0

對不起,錯過了那一個,我已經編輯了我的答案,只是改變了行convertView – triggs

+0

好吧剛剛解決它,再次感謝隊友,真的很感激它! –