Android 可拖动视图不移动,向下调用ACTION\u,然后直接调用ACTION\u CANCEL
我有一个自定义视图,可以通过触摸拖动 这是完整的代码Android 可拖动视图不移动,向下调用ACTION\u,然后直接调用ACTION\u CANCEL,android,draggable,android-custom-view,Android,Draggable,Android Custom View,我有一个自定义视图,可以通过触摸拖动 这是完整的代码 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.Ges
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;
}
}
我在绝对布局中插入了该视图的一个实例:
<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>
你能解决这个问题吗?
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