我创建了一个简单的图像选择器,在网格视图中我添加了2个视图: imageview和复选框。 所以当它点击时我试图在图像周围设置边框,但是其他图像得到边框两个(即使我没有点击它们),继承人的活动:
public class Media extends ActionBarActivity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] selectedImg;
private ImageAdapter imageAdapter;
ArrayList<String> rtnSelectedImg = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.media);
//ActionBar settings :
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null,null, MediaStore.Images.Media.DATE_TAKEN + " DESC");
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.selectedImg = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
//Adding the images to grid :
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
selectedImg[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.media, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
else if (id == R.id.done) {
Intent main = new Intent(this, Main.class);
main.putStringArrayListExtra("media_lst",rtnSelectedImg);
startActivity(main);
//finish();
}
return super.onOptionsItemSelected(item);
}
//Image Adapter Class
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
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) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.media_item, parent,false);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// holder.checkbox.setId(position);
// holder.imageview.setId(position);
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) { holder.checkbox.setChecked(thumbnailsselection[position]);
// int id = holder.checkbox.getId();
// int ids = holder.imageview.getId();
if (holder.checkbox.isChecked()){
rtnSelectedImg.remove(selectedImg[position]);
holder.checkbox.setChecked(false);
thumbnailsselection[position] = false;
holder.imageview.setBackgroundResource(0);
} else {
rtnSelectedImg.add(selectedImg[position]);
holder.checkbox.setChecked(true);
thumbnailsselection[position] = true;
holder.imageview.setBackgroundResource(R.drawable.media_row_border);
}
if(!rtnSelectedImg.isEmpty()){
getSupportActionBar().setTitle("Selected "+rtnSelectedImg.size());
}else{
getSupportActionBar().setTitle("Media");
}
}
});
// i = new AQuery(getApplicationContext());
holder.imageview.setImageBitmap(thumbnails[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}