Ok, your problem is "unable to trigger for long click, always takes touch listener", but this is not enought. I need more details:
- which view you supposed to handle the long click, parent view or children view?
- which listener you used to handle long click, android.view.View.setOnLongClickListener or android.view.GestureDetector?
actually I done the same job last week. My experiences is: do not using android.view.View.setOnLongClickListener neither android.view.GestureDetector, handle the long click on parent view by yourself. View.java is a good example.
EDIT:
I dont's have compiler on my hand, so I just typing the pseudo-code which will handle the long press by self, for the real-code, the View.java will give you the best answer.
Firstly, you need a runnable to implement your action
class CheckForLongPress implements Runnable {
public void run() {
if (getParent() != null) {
// show toast
}
}
}
Secondly, modify your onTouchEvent to detecting long press
boolean onTouchEvent(...) {
switch (event.getAction()) {
case MotionEvent.ACITON_DOWN:
// post a delayed runnable to detecting long press action.
// here mPendingCheckForLongPress is instance of CheckForLongPress
postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
...
break;
case MotionEvent.ACTION_MOVE:
// cancel long press action
if (distance(event, lastMotionEvent) > mTouchSlop) {
removeCallbacks(mPendingCheckForLongPress);
}
...
break;
case MotionEvent.ACTION_UP:
// cancel long press action
removeCallbacks(mPendingCheckForLongPress);
...
break;
EDIT again:
following is real code, not pseudo one, which is very simple and shows how to handle long press in View.onTouchEvent(), may it would be help.
public class ItemView extends View {
public ItemView(Context context) {
super(context);
}
public ItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ItemView(Context context, AttributeSet attrs) {
super(context, attrs);
}
Runnable mLongPressDetector = new Runnable() {
public void run() {
Toast.makeText(getContext(), "Hello long press", Toast.LENGTH_SHORT).show();
}
};
MotionEvent mLastEvent;
@Override
public boolean onTouchEvent(MotionEvent event) {
mLastEvent = MotionEvent.obtain(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
postDelayed(mLongPressDetector, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
final int slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
if (Math.abs(mLastEvent.getX() - event.getX()) > slop ||
Math.abs(mLastEvent.getY() - event.getY()) > slop) {
removeCallbacks(mLongPressDetector);
}
break;
case MotionEvent.ACTION_UP:
removeCallbacks(mLongPressDetector);
break;
}
return true;
}
}