1

グリッドビューのコードは次のとおりです。同じ画像が繰り返し表示され、サンプル画像 2 と 3 が何度も表示されます。どうすればよいですか?

public class GridViewImageAdapter extends 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
};

}

4

1 に答える 1

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 パターンを調べる必要があります

于 2013-07-24T14:20:50.707 に答える