可拖动视图不移动,调用 ACTION_DOWN 然后直接调用 ACTION_CANCEL

2023-12-21

我有一个可以通过触摸拖动的自定义视图 这是完整的代码。

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

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

可拖动视图不移动,调用 ACTION_DOWN 然后直接调用 ACTION_CANCEL 的相关文章

  • 如何在 Android 中保存相机的临时照片?

    在尝试从相机拍照并将其保存到应用程序的缓存文件夹中时 我没有得到任何可见的结果 应用程序不会崩溃 但在 LogCat 上 当我尝试将 ImageView src 字段设置为刚刚获取的文件的 URI 时 我收到此消息 09 17 14 03
  • Android Studio 3.0 Canary 9 - 无法解析包

    我在 Android Studio 3 0 Canary 9 中遇到几个错误 这些错误是 无法解析 android 软件包 下面列出了一些错误 我刚刚安装了 SDK 的所有额外软件包 但仍然收到 gradle 构建错误 Error 82 1
  • 使用workmanager时Firestore脱机持久性错误

    我正在使用一个WorkManger定期从我的中检索信息Firestore当应用程序处于后台和前台时的数据库 此信息用于根据状态更新 UI 因此不同的状态会添加或删除 UI 的不同部分 第一次运行时效果很好 但是 一旦应用程序处于后台并且Wo
  • 类型容器“Android 依赖项”引用不存在的库 android-support-v7-appcompat/bin/android-support-v7-appcompat.jar

    我在尝试在我的项目中使用 Action Bar Compat 支持库时遇到了某种错误 我不知道出了什么问题 因为我已按照此链接中的说明进行操作 gt http developer android com tools support libr
  • android中向sqlite中插入大量数据

    目前 我必须一次向我的 Android 中插入超过 100 亿条数据 然而 内存不足的问题会使程序崩溃 sqlite 插入测试非常简单 只需使用 for 循环生成 sql 插入命令并通过 开始 和 提交 进行包装 private Array
  • Android - 从资产中解析巨大(超大)JSON 文件的最佳方法

    我正在尝试从资产文件夹中解析一些巨大的 JSON 文件 我如何加载并添加到 RecyclerView 我想知道解析这种大文件 大约 6MB 的最佳方法是什么 以及您是否知道可以帮助我处理此文件的良好 API 我建议您使用GSON lib h
  • CardView 圆角获得意想不到的白色

    When using rounded corner in CardView shows a white border in rounded area which is mostly visible in dark environment F
  • 无法获取log.d或输出Robolectrict + gradle

    有没有人能够将 System out 或 Log d 跟踪从 robolectric 测试输出到 gradle 控制台 我在用Robolectric Gradle 测试插件 https github com robolectric robo
  • 是否可以将数组或对象添加到 Android 上的 SharedPreferences

    我有一个ArrayList具有名称和图标指针的对象 我想将其保存在SharedPreferences 我能怎么做 注意 我不想使用数据库 无论 API 级别如何 请检查SharedPreferences 中的字符串数组和对象数组 http
  • CollapsingToolBarLayout - 状态栏稀松布颜色不改变

    几天前我更新了我的 android studio 并开始使用 CoordinatorLayout 和 CollapsingToolbarLayout 只是尝试一些东西 工具栏稀松布颜色似乎覆盖了状态栏初始颜色和状态栏稀松布颜色 从 xml
  • 在 HTTPResponse Android 中跟踪重定向

    我需要遵循 HTTPost 给我的重定向 当我发出 HTTP post 并尝试读取响应时 我得到重定向页面 html 我怎样才能解决这个问题 代码 public void parseDoc final HttpParams params n
  • 带有 EditText 和 Spinner 的对话框

    我有一个按钮 单击后会弹出一个对话框 我希望对话框有一个EditText and a Spinner对话框内 我不知道如何设置它的视图 我有一个代码AlertDialog它有效 只是EditText and Spinner我需要将其放入其中
  • 如何使用 Cordova 获取当前安装的应用程序的版本?

    我已经找到了应用程序可用性插件 https github com ohh2ahh AppAvailability它主要检查用户是否在其设备上安装了某个应用程序 是否有可能获得应用程序的当前版本 开发者名称 重要 以及所有可能的信息 一般来说
  • 在gradle插件中获取应用程序变体的包名称

    我正在构建一个 gradle 插件 为每个应用程序变体添加一个新任务 此新任务需要应用程序变体的包名称 这是我当前的代码 它停止使用最新版本的 android gradle 插件 private String getPackageName
  • 错误:在根项目“projectName”中找不到项目“app”

    我有一个在 Eclipse 中开发的旧应用程序 现在尝试将其迁移到 Android Studio 我更新了库并遵循了基本步骤 现在 我收到此错误 Error Project app not found in root project pro
  • 如何确定对手机号码的呼叫是本地呼叫还是 STD 或 ISD

    我正在为 Android 开发某种应用程序 但不知道如何获取被叫号码是本地或 STD 的号码的数据 即手机号码检查器等应用程序从哪里获取数据 注意 我说的是手机号码 而不是固定电话 固定电话号码 你得到的数字是字符串类型 因此 您可以获取号
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • Firebase 添加新节点

    如何将这些节点放入用户节点中 并创建另一个节点来存储帖子 我的数据库参考 databaseReference child user getUid setValue userInformations 您需要使用以下代码 databaseRef
  • 将 Intent 包装在 LabeledIntent 中以用于显示目的

    要求 我的应用程序中有一个 共享 按钮 我需要通过 Facebook 分享 我需要选择是否安装原生 Facebook 应用程序 我们的决定是 如果未安装该应用程序 则将用户发送到 facebook com 进行分享 当前状态 我可以检测何时
  • Crashlytics 出现 Android Studio 构建错误

    我正在尝试将 CrashLytics 与 Android Studio 和 gradle 一起使用 但出现一个令人困惑的错误 java lang NoSuchMethodError 我的 build gradle 是 buildscript

随机推荐