我试图将拖放运动限制为仅在 Y 轴上,以便用户只能查看视图并将其向上或向下拖动,而不能向左或向右拖动。
我现在有两个视图(textView 和 dropZone 的 id)。其中一个 (textView) 设置了触摸侦听器,另一个 (dropZone) 设置了拖动侦听器。
这是布局 xml (activity_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="#FFFFFF00"
android:text="Text Drag" />
<TextView
android:id="@+id/dropZone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:background="#FFFF0000"
android:text="Drop Zone" />
</RelativeLayout>
以下是活动代码:
package com.example.dragexperiment;
import android.app.Activity;
import android.content.ClipData;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnGenericMotionListener;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv, dz, sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
dz = (TextView) findViewById(R.id.dropZone);
tv.setOnTouchListener(new MyTouchListener());
dz.setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
int viewX0, viewY0,
cY0, cY1,
deltaCursorY;
@Override
public boolean onTouch(final View v,final MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
viewX0 = (int) v.getX();
viewY0 = (int) v.getY();
cY0 = (int) event.getRawY();
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadow = new View.DragShadowBuilder(v);
v.startDrag(data, shadow, v, 0);
return true;
case MotionEvent.ACTION_MOVE:
// cY1 = (int) event.getRawY();
// deltaCursorY = cY1 - cY0;
// v.setX(viewX0);
// v.setY(viewY0 + deltaCursorY);
return true;
}
return false;
}
}
class MyDragListener implements OnDragListener {
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(Color.BLUE);
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
v.setBackgroundColor(Color.BLUE);
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
从上面的代码中可以看到,当用户将 textView 拖动到 dropZone 上时,我尝试将 dropZone 视图背景颜色变为蓝色。如果我在 ACTION_DOWN 运动事件中创建 DragShadowBuilder,效果很好:
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadow = new View.DragShadowBuilder(v);
v.startDrag(data, shadow, v, 0);
问题是我无法控制阴影,使其仅沿 Y 轴(垂直)轴移动。
如果我取出 DragShadowBuilder 代码(上面的三行),并添加代码以在 ACTION_MOVE 运动事件中移动拖动的视图(上面已注释掉):
cY1 = (int) event.getRawY();
deltaCursorY = cY1 - cY0;
v.setX(viewX0);
v.setY(viewY0 + deltaCursorY);
然后我可以控制textView仅沿Y轴移动。不幸的是,如果没有 DragShadowBuilder,我无法触发 ACTION_DRAG_ENTERED DragEvent 将 dropZone 视图变为蓝色。
如果我保留 DragShadowBuilder 代码和上面的四行代码,则 ACTION_MOVE 动作事件只会在拖动时触发一次;它不会继续跟随周围的阻力。
有人对我能做什么有什么想法吗?我已经对此进行了一段时间的工具,但没有运气。我什至一度尝试创建一个扩展 Android View 类的自定义视图,但我无法重写 startDrag 方法,因为它被声明为最终方法。我希望我在这方面做得更好。 :(