-3
我已經嘗試了在這個網站上有的建議,他們不爲我工作。我會盡力做到非常準確。我有一個自定義畫廊,它在GridView中顯示圖像。用戶可以從gridview中選擇多個圖像。點擊Gridview位置後,會在該位置顯示「Tick」圖標以通知用戶圖像已被選中。問題是當我選擇一個圖像,並滾動選定圖像上的「Ticked」圖標出現在其他一些圖像上。這裏是代碼。滾動時檢查Gridview位置變化
public class CustomPhotoGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
private Cursor imagecursor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages = (GridView) findViewById(R.id.grdImages);
btnSelect = (Button) findViewById(R.id.btnSelect);
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
Log.e("arrPath", "" + arrPath[i]);
}
imageAdapter = new ImageAdapter(arrPath);
grdImages.setAdapter(imageAdapter);
btnSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] arrPath;
public ImageAdapter(String[] arrPath) {
this.arrPath = arrPath;
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getViewTypeCount() {
return arrPath.length;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View rootView = convertView;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
rootView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) rootView.findViewById(R.id.imgThumb);
holder.chkImage = (ImageView) rootView.findViewById(R.id.chkImage);
rootView.setTag(holder);
} else {
holder = (ViewHolder) rootView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.imgThumb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId()
if (thumbnailsselection[id]) {
holder.chkImage.setVisibility(View.GONE);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setVisibility(View.VISIBLE);
thumbnailsselection[id] = true;
}
}
});
Picasso.with(CustomPhotoGalleryActivity.this)
.load(new File(arrPath[position]))
.resize(150, 150)
.centerCrop()
.into(holder.imgThumb);
return rootView;
}
}
class ViewHolder {
ImageView imgThumb;
ImageView chkImage;
int id;
}
}
我嘗試這樣做。它不工作:( – AmeyaG
你面臨什麼問題?? –
假設我選擇了gridview的第0個位置的圖像,它會得到「Ticked」。當我自動向下滾動時,下面的幾張圖片也會得到勾號和第0個位置圖像打勾較早得到unticked – AmeyaG