2012-10-05 37 views
2

我爲我的應用程序使用標準的gridview源代碼。我面對的問題是,當我在GridView中滾動圖像時,圖像每次都會動態變化,圖像位置錯誤。對於低於參考爲i使用的代碼......Android 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.Before滾動2.Scrolled向下滾動和再起。

before scrolling

when scrolled up again

爲什麼順序的變化?

+0

無關的問題,但使用一個CursorAdapter,它包含了很多的東西,你在做自己 – njzk2

回答

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

你只設置在舊形象查看... – njzk2

+0

是的,我只是注意到我粘在支架前。 –

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; 
} 
+0

非常感謝:) – jxgn

相關問題