I have set an OnTouchListener on a imageview in my gridview, so everytime it's clicked ACTION DOWN fires and i change the background of the image of a clicked version of it, so it emulates a click and on ACTION UP i return the old
Everything works fine when I click it but when i slide it slightly out of bounds the UP event doesn't happen and the image stays the same. That seems logical to me since I am not releasing the touch but I am sliding it outside of the imageview. I tried to limit the background change to happen only if the click is inside the bounds of the view, but nothing worked, this is my touch listener event :
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return false;
}
});
and this is another version in which i tried something else :
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("IMAGE", "xC: " + (int)motionEvent.getX() + " yC:" + (int)motionEvent.getX());
if(isViewContains(view, (int)motionEvent.getX(), (int)motionEvent.getY())) {
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
}
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return true;
}
});
this is the function that I am using
private boolean isViewContains(View view, int rx, int ry) {
int[] l = new int[2];
view.getLocationOnScreen(l);
Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
Log.i("IMAGE", "x: " + rx + " y:" + ry);
if(rect.contains(view.getLeft()+ rx, view.getTop() + ry)) {
Log.i("IMAGE", "contains");
}
return rect.contains(view.getLeft()+ rx, view.getTop() + ry);
}
Do you have any hints or suggestions ? What I want to is no matter what I do swipe or click the original image to return.
Try MotionEvent.ACTION_CANCEL in onTouch(View view, MotionEvent motionEvent)