2

我正在为我的应用程序使用标准的 gridview 源。我面临的问题是,当我在 gridview 中滚动图像时,图像每次都会动态变化,在位置上有错误的图像。以下是我使用的代码供参考......

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(this));




    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), ImageSelection.class);
            // passing array index
            i.putExtra("imageIndex_from_Activity", position);
            startActivity(i);

        }
    });

> 图像适配器类

 private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(5, 5, 5, 5);
            picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

附上截图, 1.滚动之前 2.向下滚动并再次向上滚动。

滚动前

再次向上滚动时

为什么顺序变了??

4

2 回答 2

5

您只是在新视图上设置图像。

请执行下列操作:

if (convertView == null) {
    picturesView = new ImageView(context);
    picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    picturesView.setPadding(5, 5, 5, 5);
    picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));    
} else {
    picturesView = (ImageView)convertView;
}
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
于 2012-10-05T09:11:35.167 回答
2

您的问题是,当您使用 convertView 时,它会从第一条记录中存储旧数据。转换视图用于避免资源的布局膨胀,这会花费您的时间和内存。您应该使用旧的膨胀视图,但设置新数据。

public View getView(int position, View convertView, ViewGroup parent) {  
    ImageView picturesView;  
    if (convertView == null) {  
        picturesView = new ImageView(context);  
    }  
    else {  
        picturesView = (ImageView)convertView;  
    } 
       cursor.moveToPosition(position);  
        // Get the current value for the requested column  
        int imageID = cursor.getInt(columnIndex);  
        // Set the content of the image based on the provided URI  
        picturesView.setImageURI(Uri.withAppendedPath(  
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));  
        picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
        picturesView.setPadding(5, 5, 5, 5);  
        picturesView.setLayoutParams(new GridView.LayoutParams(80, 80));  
    return picturesView;  
}
于 2012-10-05T09:07:47.583 回答