【问题标题】:Images in Android GridView changes. How?Android GridView 中的图像发生变化。如何?
【发布时间】:2012-09-26 09:33:24
【问题描述】:

我正在为我的应用程序使用标准的 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);

        }
    });

> ImageAdapter 类

 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.向下滚动再向上滚动。

为什么顺序会改变??

【问题讨论】:

  • 与问题无关,但使用游标适配器,它包含很多你正在做的事情

标签: android image gridview dynamic


【解决方案1】:

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

执行以下操作:

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));

【讨论】:

  • 是的,我刚刚注意到我粘贴在括号之前。
【解决方案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;  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多