Android: Actualizar imagen en GridView en evento táctil en Adaptador
tengo unGridView
y adaptador adjunto a la misma. El adaptador llena las imágenes en la cuadrícula. Yo he puestosetOnTouchLister
aGridView
en miActivity
e implementado solo en el adaptador. En el adaptador tengo unInteger[] imageIDs
que contiene identificadores de recursos de todas las imágenes que se agregan y unArrayList<ImageSourceObject> imgObjsArr
eso se extiendeImageView
& tiene otras propiedades establecidas.
AhoraonTouch()
, Quiero cambiar la imagen de la imagen seleccionada a otra. Aquí está mi código: SEtting adaptador de rejilla enActivity
enonCreate
:
// Set Objects in Game View
gameView = (GridView) this.findViewById(R.id.game_gridView);
gameObjAdapter = new GameObjectsAdapter(this, R.id.game_gridView, null);
gameView.setAdapter(gameObjAdapter);
ADAPTADOR
public class GameObjectsAdapter extends BaseAdapter implements OnTouchListener {
super();
mContext = c;
this.layoutResourceId = layoutResourceId;
objects = data;
gridViewResAdap = (GridView) mContext.findViewById(this.layoutResourceId);
createObjectsArray();
Log.d("GOA", "Created Objects Array");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageSourceObject imgView;
if (convertView == null) {
imgView = new ImageSourceObject(mContext);
imgView.setLayoutParams(new GridView.LayoutParams(20, 20));
imgView.getScaleType();
imgView.setScaleType(ScaleType.FIT_XY);
imgView.setPadding(0, 0, 0, 0);
} else {
imgView = (ImageSourceObject) convertView;
}
// Chk status of touched of imgView & set image accordingly
if (imgView.isTouched())
imgView.setImage(R.drawable.droid_touched2);
else
imgView.setImage(imageIds[position]);
return imgView;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getActionMasked();
float currentXPos = event.getX();
float currentYPos = event.getY();
int position = gridViewResAdap.pointToPosition((int)currentXPos, (int) currentYPos);
// Key was Pressed Here
if (action == MotionEvent.ACTION_UP) {
if (position > 0) {
// Get the object which is clicked
ImageSourceObject isb = this.imgObjsArr.get(position);
Log.d("GA", " Postion ID " + isb.getId() + " [] ID : " + imageIds[position]);
// Change the status of touched & set image
isb.setTouched(true);
isb.setImage(R.drawable.droid_touched2);
// Update the ArrayList & Integer[] with this updated obj
this.imgObjsArr.set(position, isb);
imageIds[position] = R.drawable.droid_touched2;
Log.d("ISB", "++++ Object ID : " + isb.getId() + " [] ID : " + imageIds[position] + " ISB Touched :" + isb.isTouched());
Toast.makeText(mContext, "Postion Pressed : " + (position+1),
Toast.LENGTH_SHORT).show();
//this.gridViewResAdap.invalidate();
}
return true;
}
return false;
}
Registros:
02-19 15:19:11.615: D/GA(2046): Postion ID 2130837556 [] ID : 2130837556
02-19 15:19:11.617: D/ISB(2046): ++++ Object ID : 2130837559 [] ID : 2130837559 ISB Touched :true
Los registros dicen que el objeto enInteger[]
& ArrayList
ambos están actualizados y tienen valores correctos. Después de todo esto también la imagen no se actualiza en la pantalla. losgridViewResAdap
también es el objeto de la cuadrícula que se pasa de la actividad. Traté de llamarinvalidate()
en él también, pero aún no hay resultados. Como en migetView()
, Estoy usandoimageIDs
, así que he mantenido eso también actualizado.
ImageSourceObject:
public class ImageSourceObject extends ImageView {
public void setImage(int resourceId) {
super.setImageResource(resourceId);
this.setId(resourceId);
}
También,onTouch()
da error al llamaronPerformClick()
, No estoy seguro de dónde llamar y por qué llamar. No tengo implementadoonClickListener
en el adaptador ¿Qué se puede hacer en este caso y qué escribir?onClickListener
cuando las cosas se manejanonTouch()
.
¿Me pueden ayudar a saber por qué el objeto de imagen no se actualiza y dónde me estoy equivocando? Pensé que tenía que actualizar la cuadrícula, así que también llamé invalidate (), pero no hubo resultados.
Cualquier ayuda es muy apreciada.
Gracias