0

I have a drag and drop code. If the user touch the item, the OnTouchListener

Code starts:

View.OnTouchListener dragListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        // start move on a touch event
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            // API 24
            // view.startDragAndDrop(data, shadowBuilder, view, View.DRAG_FLAG_GLOBAL); // API 24
            view.setVisibility(View.VISIBLE);
            return true;
        }
        return false;
    }
};

But I would have a OnLongClickListener on my code. If the item was hold (long clicked) by the user, a toast messages was show on the display:

    homebutton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
    Toast.makeText(UserArea.this, "laaaange geklickt", Toast.LENGTH_SHORT).show();
    return true;  
}
});

But it doesn't work :-(

Jonathan Bravetti
  • 2,228
  • 2
  • 15
  • 29
SilverBlue
  • 239
  • 2
  • 13
  • 1
    does your dargListener is also set with homeButton ? if yer then you should return false from if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {return false;} condition – Rajendra Sep 01 '16 at 18:33
  • sorry, I don't know, what you mean with "dragListener is also set with homebutton". :-( – SilverBlue Sep 01 '16 at 18:53

2 Answers2

1

return false from your listener

View.OnTouchListener dragListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //your logic 
        return false;
    }
};

otherwise your event would be treated as completed

barbiepylon
  • 891
  • 7
  • 23
surya
  • 607
  • 5
  • 18
  • findViewById(R.id.homebutton).setOnTouchListener(dragListener); – SilverBlue Sep 06 '16 at 15:56
  • after handling the onTouchEvent - you were returning true , returning true will not propagate the touch event further in the hierarchy, the event is treated as consumed .Only if you return false it will propagate the touch event. – surya Sep 06 '16 at 16:27
  • Okay, I've set return false. But the Code doesn't works. I can only drag and drop the imgBtn, but don't click :-( – SilverBlue Sep 06 '16 at 16:34
  • If Idelete the line "findViewById(R.id.imgBtnItem1).setOnTouchListener(dragListener);" the button is clickable, but I can't use drag and drop – SilverBlue Sep 06 '16 at 16:38
  • http://stackoverflow.com/questions/4324362/detect-touch-press-vs-long-press-vs-movement Please check how to override the Touch_move and detecting longClick – surya Sep 06 '16 at 16:49
1

Edit your Touch Event and based on threshold you decide the TOuch or Long Press

private float mDownX;
private float mDownY;
private final float SCROLL_THRESHOLD = 10;
private boolean isOnClick;

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mDownX = ev.getX();
            mDownY = ev.getY();
            isOnClick = true;
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (isOnClick) {
                Log.i(LOG_TAG, "onClick ");
                //TODO onClick code
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (isOnClick && (Math.abs(mDownX - ev.getX()) > SCROLL_THRESHOLD || Math.abs(mDownY - ev.getY()) > SCROLL_THRESHOLD)) {
                Log.i(LOG_TAG, "movement detected");
                isOnClick = false;
            }
            break;
        default:
            break;
    }
    return true;
}
surya
  • 607
  • 5
  • 18