赞
踩
看到很多双击的实现方式都是在单击的前提下实现的;
会出现冲突,双击的时候第一个其实是单击;
我们想要实现的方式是,双击和单击分开,双击的时候不会执行单击的响应,下面我们来看下核心代码:
private long firstClickTime = 0;
private long secondClickTime = 0;
private boolean isDoubleClick;
private void OnClick() {
if (firstClickTime > 0) {
secondClickTime = System.currentTimeMillis();
if (secondClickTime - firstClickTime < 200) {
doubleClick();//双击回调
firstClickTime = 0;
isDoubleClick = true;
return;
}
}
firstClickTime = System.currentTimeMillis();
isDoubleClick = false;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(200);
firstClickTime = 0;
if (!isDoubleClick) {
onClick()//单击回调
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。