【问题标题】:gridview showing the same image in repetitiongridview 重复显示相同的图像
【发布时间】:2013-07-24 14:13:35
【问题描述】:

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

公共类 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
};

}

【问题讨论】:

    标签: java android android-gridview


    【解决方案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 模式

    【讨论】:

    • 非常感谢您的即时答复,但 Eclipse 说无法解决行,我该怎么办?
    • 抱歉错过了,我已经编辑了答案,只是将行更改为 convertView
    • 好的,刚刚解决了,再次感谢大佬,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多