我有一个可以通过触摸拖动的自定义视图 这是完整的代码。
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>
但是当我尝试拖动视图时,ACTION_DOWN
会被调用,然后调用ACTION_CANCEL
。 ACTION_MOVE
不会被调用。 onTouch方法总是返回true,所以我无法弄清楚出了什么问题。请帮我解决这个问题。
以下是日志:
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