gpt4 book ai didi

android - DragEvent.ACTION_DROP 永远不会被调用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:18 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序上使用图像拖动。我的问题是,当我停止拖动图像时,永远不会调用 DragEvent.ACTION_DROP。

在我的日志猫中,我接到了这个电话:

01-30 13:50:25.003: I/ViewRootImpl(2198): Reporting drop result: false

感谢您的帮助。

这是我的代码:

public class buildImage extends Activity implements OnTouchListener, OnDragListener
{
private LinearLayout slider;
private RelativeLayout board;

@Override
protected void onCreate(Bundle savedInstanceState) {

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

super.onCreate(savedInstanceState);
setContentView(R.layout.build_image);

//drawing = (SignatureViewModed) findViewById(R.id.myTest);
slider = (LinearLayout) findViewById(R.id.imageSlider);
board = (RelativeLayout) findViewById(R.id.borad);

setImagesSlider();

}

public void setImagesSlider()
{
ImageView image = new ImageView(this);
image.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

Drawable draw = getResources().getDrawable(R.drawable.images);

image.setImageDrawable(draw);
image.setOnDragListener(this);

image.setOnTouchListener(this);

slider.addView(image);

}

@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub

if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("tests", "test");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
//view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}

@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub

int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
{
Log.i("check6", "check6");
// do nothing
return true;
}
case DragEvent.ACTION_DRAG_ENTERED:
{
Log.i("check4", "check4");
//v.setBackgroundDrawable(enterShape);
return true;
}
case DragEvent.ACTION_DRAG_EXITED:
{
// v.setBackgroundDrawable(normalShape);
Log.i("check3", "check3");
return true;
}
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup

Log.i("check", "check");
addNewImage(event);

return true;
case DragEvent.ACTION_DRAG_ENDED:
{
Log.i("check2", "check2");


// addNewImage(event);

return true;
}
default:
break;
}
return true;
}

public void addNewImage(DragEvent event)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

Drawable draw = getResources().getDrawable(R.drawable.images);

image.setImageDrawable(draw);

board.addView(image);

float x = event.getX();
float y = event.getY();

image.setX(x);
image.setY(y);

Log.i("margin", "x " + x + " y " + y);

}

}

我的 XML

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/borad"
android:background="@android:color/darker_gray"
android:layout_above="@+id/temp"
android:layout_height="fill_parent" >
</RelativeLayout>

<ScrollView
android:layout_width="fill_parent"
android:id="@+id/temp"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/imageSlider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>

</RelativeLayout>

enter image description here

最佳答案

您必须有东西可以放置该图像。到目前为止,图像本身只有一个 OnDragListener。您要放置该图像的位置不包含任何带有 OnDragListener 的控件,因此不会调用您的监听器。

监听 OnDragListeners 的控件不能不可见或消失,因此您必须在单元格看起来为空但实际并非如此的地方放置某种网格。例如带有透明图像的 ImageView。或者将 Alpha 设置为 0f。

至少为您的内部 LinearLayout 设置一个 OnDragListener,然后您将获得 Drop 事件。

关于android - DragEvent.ACTION_DROP 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com