android - ontouch后,在Android上无法继续ondrag

标签 android

(我删除了所有代码,因为下面的代码也不起作用。)

附加信息和简短摘要 - 真正的问题

这很有趣。我有另一个项目一个简单的 Activity 。在那里,我可以拖动。现在在这个项目中,我到处屏蔽评论。只有几行,就像另一个项目,但仍然没有拖沓。

 Drag is in progress, but there is no drag window handle.

有时它说。我也搜过了,但是我做不到。

现在的代码是:

protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "matching ggame OnCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_matchgame);
    setupForGame();
    Log.d(TAG, "matching game OnCreate ends");
}//oncreate end

void setupForGame(){
    Log.d(TAG, "matching game setupforgame");

    imageAnswer = (ImageView) findViewById(R.id.imgAnswer);

    imageAnswer = (ImageView) findViewById(R.id.imgAnswer);
}

public boolean onTouch(View view, MotionEvent motionEvent) {
    Log.d(TAG, "ontouch never");
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        View.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 receivingLayoutView, DragEvent dragEvent) {
    Log.d(TAG, "ondrag start");

仍然没有进去。

我把它设为静态,看看是否是问题所在。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/relative_layout"
    tools:context=".games.MatchingGame">

    <ImageView
        android:id="@+id/imgAnswer"
        android:layout_width="111dp"
        android:layout_height="111dp"
        android:src="@drawable/b3_1"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

它与另一个项目相同,但它不起作用。我查看了所有可能性并进行了调试。

并且请不要要求拖动方法代码;这不重要。进不去。

当我按住并拖动图像时,这会循环进入日志,直到我离开:

I/SPRDHWComposer: util[1759] warning: osdLayer width 111, stride 112, not equal!

当我离开图像时:

V/WindowManager: rotationForOrientationLw(orient=0, last=1); user=0 USER_ROTATION_LOCKED sensorRot
V/WindowOrientationListener: getProposedRotation : mEnabled = false, mUsedautorotioansensor =true
 W/WindowManager: Drag is in progress but there is no drag window handle.

如果触摸有问题,我也做了这个,但还是一样:

imageAnswer.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //if (event.getAction() == MotionEvent.ACTION_DOWN) { Log.d(TAG, "ontouch down");

        //Log.d(TAG,"parent "+ String.valueOf(img1.getParent()));
        Log.d(TAG, "view "+String.valueOf(v));

        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
        v.startDrag(data, shadowBuilder, v, 0);
        v.setVisibility(View.INVISIBLE);
        return true;

这是我使用拖放操作的旧项目。我看不出区别:

https://gist.github.com/anonymous/540a70e05de6d765e20896b47deb7eb2

当我这样做的时候

imageAnswer.setOnDragListener(new View.OnDragListener() {
    public boolean onDrag(View v, DragEvent event) {  Log.d(TAG, "drag");

        return false;
    }
});

也不行。没有日志。

此外,这不会给出日志:

imageAnswer.setOnDragListener(new dropListener());

private class dropListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {Log.d("Chic","ondrag");
        switch (event.getAction()) {

            case DragEvent.ACTION_DRAG_STARTED:
                break;

            case DragEvent.ACTION_DRAG_ENTERED:
                break;

            case DragEvent.ACTION_DRAG_EXITED:
                break;

            case DragEvent.ACTION_DROP:
                break;

            case DragEvent.ACTION_DRAG_ENDED:
                break;

            default:
                break;
        }
        return true;
    }
 }

两个项目的区别:

Differences between projects

会是这个原因吗?

我还尝试将所有 setondrag setontouch 放在 oncreate 中。

最佳答案

据我所知(例如 here ),onTouch 和 onDrag 根本不能一起工作。为什么不把你们所有的代码都放在 onDrag 中?从您的代码来看,这应该足够了。

关于android - ontouch后,在Android上无法继续ondrag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313631/

相关文章:

android - Ionic + Angular - 平台添加 Android 错误 - 错误 : Failed to fetch platform cordova-android@0. 0.8

java - 方法调用 'fab.setOnClickListener(new View.OnClickListener() { @Override public void on...' 可能产生 'java.lang.NullPointerException'

android - 新的 Facebook API 3.0。和 ActionBarSherlock 的兼容性

android - RecycleView 的 PointToPosition 替代方案

android - Android/iOS 应用程序内浏览器上的证书固定

android - 无法读取手机通讯录

java - 捕获当前Webview并保存到SD卡

Android 屏幕截图 - 如何?

android - 了解类型化值类

Android:在恢复时刷新 ArrayAdapter