我真的很花时间弄清楚这个,到目前为止找不到任何有相关经验的朋友。这是我发布我的第一个应用程序之前的最后一个功能,所以在看到结局的情况下陷入困境让我发疯!
如果我将对象放在可接受的区域中,我的拖放操作会非常完美。但是,如果拖放到其他地方,我不会收到事件,并且在拖放操作失败后无法清理。我需要触发 ACTION_DRAG_ENDED 事件。但是,如果我以 true 值响应 ACTION_DRAG_STARTED (这意味着它可以接受对象),这只会触发(根据文档)。问题是 ACTION_DRAG_STARTED 没有触发!
所以我认为问题的症结在于我没有得到 ACTION_DRAG_STARTED。我将所有相关的拖放代码(或至少几乎所有)放在这里:
private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Item current = (Item) ((View) v.getTag()).getTag();
//ClipData data = ClipData.newPlainText("test", "testText");
View dragView = (View) v.getTag();
DragShadowBuilder shadowBuilder = new ItemDragShadowBuilder(dragView,
new Point(v.getWidth()/2, v.getHeight()/2));
dragSource = dragView;
v.startDrag(null, shadowBuilder, current, 0);
dragView.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
};
private static class ItemDragShadowBuilder extends View.DragShadowBuilder {
private Point touchPoint = null;
private Point sizePoint = null;
private View dragView = null;
public ItemDragShadowBuilder(View dragView, Point touchPoint) {
super();
sizePoint = new Point(dragView.getWidth(), dragView.getHeight());
this.touchPoint = touchPoint;
this.dragView = dragView;
}
@Override
public void onProvideShadowMetrics (Point size, Point touch) {
size.set(sizePoint.x, sizePoint.y);
touch.set(touchPoint.x,touchPoint.y);
}
@Override
public void onDrawShadow(Canvas canvas) {
dragView.setBackgroundColor(dragView.getContext().getResources().getColor(R.color.holoBlueDark));
dragView.draw(canvas);
//canvas.setBitmap(dragView.getDrawingCache());
//canvas.drawColor(Color.WHITE);
//dragView.setAlpha(1);
//How do I change the transparency?
//super.onDrawShadow(canvas);
}
}
private View dragSource = null;
private OnDragListener onDragEventListener = new OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Toast.makeText(v.getContext(), ((EditText) v.findViewById(R.id.edtParent)).getText().toString() + " started", Toast.LENGTH_SHORT/8).show();
return true;
//break;//DRAG AND DROP>>>START DOESN"T FIRE
case DragEvent.ACTION_DROP:
if ((dragSource != null) && (dragSource != v)) {
Item source = (Item) dragSource.getTag();
Item target = (Item) v.getTag();
int sourcePos = source.getPosition();
int targetPos = target.getPosition();
if (targetPos < sourcePos)
source.moveUp(sourcePos - targetPos);
else
source.moveDown(targetPos - sourcePos);
dragSource.setVisibility(View.VISIBLE);
dragSource = null;
fireOnChecklistNeedsResetListener();
return true;
}
// View view = (View) event.getLocalState();
// Toast.makeText(v.getContext(), ((EditText) v.findViewById(R.id.edtParent)).getText().toString(), Toast.LENGTH_SHORT/8).show();
// Toast.makeText(v.getContext(), ((EditText) view.findViewById(R.id.edtParent)).getText().toString(), Toast.LENGTH_SHORT/8).show();
// Dropped, reassign View to ViewGroup
// View view = (View) event.getLocalState();
// ViewGroup owner = (ViewGroup) view.getParent();
// owner.removeView(view);
// LinearLayout container = (LinearLayout) v;
// container.addView(view);
// view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
dragSource.setVisibility(View.VISIBLE);
dragSource = null;
fireOnChecklistNeedsResetListener();
if (!event.getResult())
Toast.makeText(v.getContext(), "UNHANDLED", Toast.LENGTH_SHORT/8).show();
// View vieww = (View) event.getLocalState();
// Toast.makeText(v.getContext(), ((EditText) v.findViewById(R.id.edtParent)).getText().toString(), Toast.LENGTH_SHORT/8).show();
// Toast.makeText(v.getContext(), ((EditText) vieww.findViewById(R.id.edtParent)).getText().toString(), Toast.LENGTH_SHORT/8).show();
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(v.getContext().getResources().getColor(R.color.holoBlueLight));
return true;
//break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(v.getContext().getResources().getColor(android.R.color.background_light));
return false;
//break;
default:
break;
}
return false;
}
};
扩展信息:dragView 是列表视图(特别是可扩展列表视图)中的项目布局。接受触摸事件的视图是视图上的一个小字形,带有一个小夹子。这是一个正在进行拖动操作的屏幕截图(需要一些花哨的手指工作才能得到这个哈哈):
因此,如果我要将拖动的项目放在另一个项目上(这会重新排序它们),它会很好用。如果我把它丢到别的地方,就不会触发任何事件,所以我永远无法清理它。因此,例如在屏幕的上部,或者如果列表在空白区域或底部的蓝色条或任何系统区域中很短。
我也可以通过找出一种防止拖离有效区域的方法来解决这个问题,所以如果你知道如何做到这一点,那是另一种解决方法。我的最后一个选择是为屏幕上的每个可能视图实现一个事件(同样,这些是列表视图布局,在一个列表视图上,它在一个片段上,可能在各种活动上)所以显然这不是可取的。事实上,如果我这样做了,如果它掉到系统 UI 区域(如系统栏或电容式按钮区域),我仍然可能会遇到问题,但我不确定。
如果这里有帮助,我在列表视图适配器获取视图项方法中分配相关事件过程(我删除了不相关的行):
public View getGroupEdit(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView != null) {
//irrelevant stuff omitted
} else
convertView = inf.inflate(R.layout.edit_group_item, null);
Item group = (Item) getGroup(groupPosition);
convertView.setTag(group);
EditText edtParent = (EditText) convertView.findViewById(R.id.edtParent);
edtParent.setTag(convertView);
scatterItemText(edtParent);
//irrelevant stuff omitted
ImageView imgGrip = (ImageView) convertView.findViewById(R.id.imgGrip);
imgGrip.setTag(convertView);
//irrelevant stuff omitted
imgGrip.setOnTouchListener(onTouchListener);
convertView.setOnDragListener(onDragEventListener);
return convertView;
}
最后是 FWIW,这是列表项视图的布局 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgGrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/dark_grip2"
android:contentDescription="@string/strReorder"/>
<CheckBox
android:id="@+id/chkParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imgGrip"
android:checked="false"/>
<EditText
android:id="@+id/edtParent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chkParent"
android:layout_toLeftOf="@+id/btnInsert"
android:textSize="17dip"
android:inputType="text"
android:hint="@string/strItem"/>
<ImageButton
android:id="@id/btnInsert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/viewParent"
android:src="@drawable/dark_content_new"
android:clickable="true"
android:hint="@string/strAddItemAbove"
android:contentDescription="@string/strAddItemAbove" />
<View
android:id="@id/viewParent"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_toLeftOf="@+id/btnExpand"/>
<ImageButton
android:id="@id/btnExpand"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/dark_add_sub_item"
android:clickable="true"
android:hint="@string/strViewSubItems"
android:contentDescription="@string/strViewSubItems"/>
</RelativeLayout>
非常感谢您的帮助!