The dragged view does not move, calls ACTION_DOWN and then directly to ACTION_CANCEL

I have a custom view that can be dragged and dropped here for the complete code.

package com.neibrapp.neibr;

import android.app.ActionBar;
import android.content.ClipData;
import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureOverlayView;
import android.gesture.GestureUtils;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by me on 6/16/15.
 */
public class DraggableView extends View implements View.OnTouchListener {
    float originX,originY;
    int width,height;
    String TAG = "DraggableView";

    public DraggableView(Context context) {
        super(context);
        this.setLayoutParams(new AbsoluteLayout.LayoutParams(0, 0, 0, 0));
        this.setOnTouchListener(DraggableView.this);
    }
    public void setSize(int  x,int y,int width,int height){
        this.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,x,y));

        this.width =width;
        this.height =height;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        int action = motionEvent.getAction() & MotionEvent.ACTION_MASK;
        Log.v(TAG,"CALLED!");
        if(action==MotionEvent.ACTION_DOWN){
            Log.v(TAG,"Action (DOWN): "+MotionEvent.ACTION_DOWN);
            originX = this.getX();
            originY = this.getY();
            ClipData clipData = ClipData.newPlainText("","");
            View.DragShadowBuilder dsb = new View.DragShadowBuilder(view);
            view.startDrag(clipData, dsb, view, 0);
            return true;
        }
        else if(action == MotionEvent.ACTION_MOVE){
            Log.v(TAG, "moving to: (" + motionEvent.getX() + "," + motionEvent.getY() + ")");
            this.setSize((int) motionEvent.getX(), (int) motionEvent.getY(), width,height);
            return true;
        }
        else if(action == MotionEvent.ACTION_CANCEL){
            Log.v(TAG,"Action (CANCEL): "+MotionEvent.ACTION_CANCEL);
        }



        return true;
    }


}

      

and I inserted an instance of this view into an absolute layout:

<AbsoluteLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/cardContainer"
  android:orientation="vertical"
  android:background="@color/background_color_green">
</AbsoluteLayout>

      

But when I tried to drag the view ACTION_DOWN

, and then ACTION_CANCEL

. ACTION_MOVE

will not be called. The onTouch method always returns true, so I can't figure out what's wrong. Please help me with this.

Here are the logs:

06-17 00:13:32.062  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:13:32.062  19057-19057/? V/DraggableView﹕ Action (DOWN): 0
06-17 00:13:32.067  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:13:32.067  19057-19057/? V/DraggableView﹕ Action (CANCEL): 3
06-17 00:20:56.245  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:20:56.245  19057-19057/? V/DraggableView﹕ Action (DOWN): 0
06-17 00:20:56.249  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:20:56.249  19057-19057/? V/DraggableView﹕ Action (CANCEL): 3
06-17 00:20:56.750  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:20:56.750  19057-19057/? V/DraggableView﹕ Action (DOWN): 0
06-17 00:20:56.754  19057-19057/? V/DraggableView﹕ CALLED!
06-17 00:20:56.754  19057-19057/? V/DraggableView﹕ Action (CANCEL): 3

      

+3


source to share





All Articles