gridview在重复中显示相同的图像

时间:2013-07-24 14:13:35

标签: java android android-gridview

这是gridview的代码,它在重复中显示相同的图像,样本图像2和3一遍又一遍地显示,我该怎么办?

公共类GridViewImageAdapter扩展BaseAdapter {     private Context 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 个答案:

答案 0 :(得分:1)

这是因为你反复回到同一个观点。

convertView是一个重用的视图,所以一旦它被使用并且不再为null,你将返回它而不更改它。

更改为

    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模式