继承了LinearLayout重写dispatchTouchEvent的问题

Jing丶無雙 Jing丶無雙 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package app.example.draglinearlayout;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class DragLinearLayout extends LinearLayout {

private Vibrator vibrator;
/**
* 是否可以拖拽,默认不可以
*/
private boolean isDrag;
/**
* 用来处理是否为长按的Runnable
*/
private Runnable onLongRunnable = new Runnable() {

@Override
public void run() {
isDrag = true; // 设置可以拖拽
vibrator.vibrate(100); // 震动一下
// 隐藏自己
// DragLinearLayout.this.setVisibility(View.INVISIBLE);
}
};
private Handler handler = new Handler();

@SuppressLint("NewApi")
public DragLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 使用Handler延迟dragResponseMS执行onLongRunnable
handler.postDelayed(onLongRunnable, 1000);

break;
case MotionEvent.ACTION_MOVE:
Log.d("2", "*************");
break;
case MotionEvent.ACTION_UP:
Log.d("1", "*************");
break;
}
return super.dispatchTouchEvent(ev);
}

}



<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" >

<app.example.draglinearlayout.DragLinearLayout
android:layout_width="200dp"
android:layout_height="300dp"
android:background="#FAFDAD" />

</RelativeLayout>


我的问题是为什么没有打印出Log.d("1", "*************"); Log.d("2", "*************");这两个Log日志;即MotionEvent.ACTION_MOVE:与MotionEvent.ACTION_UP: 里的内容没有执行
...全文
评论 6
收藏 已收藏
分享
返回顶部
Jing丶無雙
Android 80,489 成员 91,375 内容