Android Development and Implementation of the Monitoring Interface Encapsulation Class for Control Double click Events
- 2021-11-29 08:24:19
- OfStack
When writing a project, it is required to imitate the WeChat circle of friends and double-click the top bar to set the top, so the double-click callback interface is packaged, which is convenient for everyone to use
/**
* Created by Administrator on 2018/4/24.
* Double-click
*/
public class OnDoubleClickListener implements View.OnTouchListener{
private int count = 0;// Number of clicks
private long firstClick = 0;// No. 1 1 Time per click
private long secondClick = 0;// No. 1 2 Time per click
/**
* Two-click interval in milliseconds
*/
private final int totalTime = 1000;
/**
* Custom callback interface
*/
private DoubleClickCallback mCallback;
public interface DoubleClickCallback {
void onDoubleClick();
}
public OnDoubleClickListener(DoubleClickCallback callback) {
super();
this.mCallback = callback;
}
/**
* Touch event handling
* @param v
* @param event
* @return
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {// Press
count++;
if (1 == count) {
firstClick = System.currentTimeMillis();// Record number 1 Time per click
} else if (2 == count) {
secondClick = System.currentTimeMillis();// Record number 2 Time per click
if (secondClick - firstClick < totalTime) {// Judge 2 Whether the time interval of the second click is within the set interval time
if (mCallback != null) {
mCallback.onDoubleClick();
}
count = 0;
firstClick = 0;
} else {
firstClick = secondClick;
count = 1;
}
secondClick = 0;
}
}
return true;
}
}
Specific application key codes:
/**
* Top bar double-click , Set top
*/
private void titleDoubleOnClick(){
titleLayout.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.DoubleClickCallback() {
@Override
public void onDoubleClick() {
listView_neighbour.setSelection(0);// Handle double-click events
}
}));
}
This article provides us with Android development control double-click event monitoring interface encapsulation class, we can use it, more about Android double-click event skills please see the following links