0

大家好,我有一个问题。我想用自定义 Loyout 做一个 GridView,所以我使用了 layoutInflater,我这样做了:

private ImageView prima;
private ImageView seconda;

public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {

            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);

            Display display = getWindowManager().getDefaultDisplay();
            int width = display.getWidth();

            prima = (ImageView) v.findViewById(R.id.imageView1);
            prima.getLayoutParams().height = width / 3;
            prima.getLayoutParams().width = width / 3;
            seconda = (ImageView) v.findViewById(R.id.imageView2);
            seconda.getLayoutParams().height = width / 3;
            seconda.getLayoutParams().width = width / 3;

            v.setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
            v.setPadding(0, 0, 0, 0);
        } else {
            v = convertView;
        }

        prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
        return v;

    };

当我运行我的应用程序时,图像是随机排列的,并且有很多没有图像的黑色空间。

我做错了什么?

4

2 回答 2

2

你的ViewconvertView没有链接。试试ViewHolder concept这样:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder v;
        if (convertView == null) {

            LayoutInflater li = getLayoutInflater();
            convertView  = li.inflate(R.layout.icon, null);

            Display display = getWindowManager().getDefaultDisplay();
            int width = display.getWidth();

            v = new ViewHolder();
            v.prima = (ImageView) convertView .findViewById(R.id.imageView1);
            v.prima.getLayoutParams().height = width / 3;
            v.prima.getLayoutParams().width = width / 3;
            v.seconda = (ImageView) convertView .findViewById(R.id.imageView2);
            v.seconda.getLayoutParams().height = width / 3;
            v.seconda.getLayoutParams().width = width / 3;

            convertView .setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
            convertView .setPadding(0, 0, 0, 0);
            convertView.setTag(v);
        } else {
            v = (ViewHolder)convertView.getTag();
        }

        v.prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....

         //v.seconda
        return convertView;

    };

class ViewHolder{
    ImageView prima;
    ImageView seconda;
}
于 2012-06-28T11:32:40.213 回答
0

您忘记将图像应用于

    seconda = (ImageView) v.findViewById(R.id.imageView2);

    prima.setImageResource(....);
于 2012-06-28T11:25:24.117 回答