继承了LinearLayout重写dispatchTouchEvent的问题

Jing丶無雙 2014-11-12 04:43:36
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: 里的内容没有执行
...全文
348 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jing丶無雙 2014-11-14
  • 打赏
  • 举报
回复
感谢各位
gqjjqg 2014-11-13
  • 打赏
  • 举报
回复
引用 3 楼 xj396282771 的回复:
我现在在原代码基础上重写了这个
@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
为毛就行了???成功的显示了日志
建议先去看看android 的Event机制。 这个是写的比较好的: http://www.cnblogs.com/jqyp/archive/2012/04/25/2469758.html
画虎烂 2014-11-13
  • 打赏
  • 举报
回复
dispatchTouchEvent是事件分发,onTouchEvent是事件响应
Jing丶無雙 2014-11-13
  • 打赏
  • 举报
回复
我现在在原代码基础上重写了这个
@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
为毛就行了???成功的显示了日志
红袖满楼招 2014-11-12
  • 打赏
  • 举报
回复
你的日志应该在ontouchevent里面打出来,你写的是时间分发不是事件响应
bdmh 2014-11-12
  • 打赏
  • 举报
回复
重载onInterceptTouchEvent及TouchEvent 具体动作参考 http://mobile.51cto.com/abased-374715.htm

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧