package com.dianchou.dcw.view.main.point;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
/**
* 创建者 yf
* 创建时间 2018/8/28 17:12
* 描述 ${TODO}
*/
public class SingleDoubleClickListener implements View.OnTouchListener {
//双击间四百毫秒延时
private static int timeout = 400;
//记录连续点击次数
private int clickCount = 0;
private Handler handler;
private MyClickCallBack myClickCallBack;
public interface MyClickCallBack {
void oneClick();//点击一次的回调
void doubleClick();//连续点击两次的回调
}
public SingleDoubleClickListener(MyClickCallBack myClickCallBack) {
this.myClickCallBack = myClickCallBack;
handler = new Handler();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
clickCount++;
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (clickCount == 1) {
myClickCallBack.oneClick();
} else if (clickCount == 2) {
myClickCallBack.doubleClick();
}
handler.removeCallbacksAndMessages(null);
//清空handler延时,并防内存泄漏
//计数清零
clickCount = 0;
}
//延时timeout后执行run方法中的代码
}, timeout);
}
//让点击事件继续传播,方便再给View添加其他事件监听
return true;
}
}
使用举例:
View button= findViewById(R.id.button);
view.setOnTouchListener(new MyClickListener
(new MyClickListener.MyClickCallBack() {
@Override
public void oneClick() {
Toast.makeText(MainActivity.this, “单击”,
Toast.LENGTH_SHORT).show();
}
@Override
public void doubleClick() {
Toast.makeText(MainActivity.this, “双击”,
Toast.LENGTH_SHORT).show();
}
}));
0人点赞
作者:木叶纷飞
链接:https://www.jianshu.com/p/554791ef77f7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。