I have a Custom Layout with a standard setOnClickListener() call.
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// some click related code
}
});
In this Layout is a view that should drag horizontally.
I wrote some code and it does more or less what I want it to do:
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = true;
final int action = event.getAction();
final float x = event.getRawX();
final float y = event.getRawY();
switch (action) {
case MotionEvent.ACTION_DOWN: {
mInitialTouchX = x;
mInitialTouchY = y;
mStartX = getX();
mStartY = getY();
break;
}
case MotionEvent.ACTION_MOVE: {
Logger.d("MOVE");
if (direction == -1) {
}
final float dx = x - mInitialTouchX;
final float dy = y - mInitialTouchY;
mPosX = mStartX + dx;
mPosY = mStartY + dy;
if (Math.abs(dx) > SWYPE_MIN_TRAVEL) {
if (Math.abs(mPosX) < maximumSwypeDistance) {
setTranslationX(mPosX);
}
}
break;
}
case MotionEvent.ACTION_UP:
Logger.d("UP");
case MotionEvent.ACTION_CANCEL:
Logger.d("CANCEL");
case MotionEvent.ACTION_POINTER_UP:
Logger.d("POINTER_UP");
final float dx = x - mInitialTouchX;
if (Math.abs(dx) < SWYPE_MIN_TRAVEL) {
Logger.d("click");
result = false;
}
break;
}
return result;
}
The question is: How can I have both?
Edit: Well using the answer from Gunnar Karlssons event i could alter the code to perform the click manually in the ACTION_UP
Event like this:
((ViewGroup) this.getParent()).performClick();
I can't help myself but i find this somewhat unsatisfying. I there a more elegant solution?