https://github.com/mubeenmajeed/dragable-clendar-event
这是我的公共存储库,用于在时间空间内拖动事件。但在长时间按下时,它能够在系统中拖动事件。我希望执行此操作的代码示例:
@Override
public void onLongPress(MotionEvent event) {
Log.d(TAG, "onLongPress: ");
scrollView.requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Start tracing finger movement
start.set(event.getX(), event.getY());
enableDragMode();
break;
case MotionEvent.ACTION_UP:
enableNoneMode(timeBlock);
// Allow other touch events to steal
scrollView.requestDisallowInterceptTouchEvent(false);
// Adjust block position
if (positionToLabelEntry != null) {
setTopMargin(positionToLabelEntry.getKey());
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// Set dragging distance
int draggingDistance = (int) (event.getY() - start.y);
boolean goingDown = draggingDistance > 0 ? true : false;
// Define dragging limits
int timeTableTopLimitLocation = lineNumberToScreenPosition.get(0);
int timeTableBottomLimitLocation = lineNumberToScreenPosition.get(lineNumberToScreenPosition.size() - 1);
int timeBlockBottomLimitLocation = timeTableBottomLimitLocation - params.height;
// Remove if you only want to allow events after red line
upperLimitPosition = timeTableTopLimitLocation;
int potentialTopMargin = timeBlockParentParams.topMargin + draggingDistance;
// Readjust top margin to prevent going over limits
if (potentialTopMargin < upperLimitPosition) {
potentialTopMargin = upperLimitPosition;
}
if (potentialTopMargin > timeBlockBottomLimitLocation) {
potentialTopMargin = timeBlockBottomLimitLocation;
}
// Make it scroll when finger is close to window top/down
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Rect rectangle = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
if ((event.getRawY() <= (statusBarHeight + event.getY() + getResources().getDimensionPixelSize(R.dimen.pad_10dp) + headerLayout.getLayoutParams().height) && !goingDown)
|| (event.getRawY() >= metrics.heightPixels - (timeBlock.getHeight() - event.getY()) && goingDown)
|| (timeBlockParentParams.topMargin + params.height >= lineNumberToScreenPosition.get(lineNumberToScreenPosition.size() - 1) && goingDown)) {
scrollView.smoothScrollBy(0, draggingDistance);
}
// Set top margin to position choose by finger
setTopMargin(potentialTopMargin);
// Get closest time block and show minutes
getClosestTimeBlockAndShowMinuteLabel();
// Adjust time block position to position that makes sense
setTimeBlockPosition(false);
}
break;
}
}
这个功能将很好地工作时,这个代码块是在触摸,但不长按工作。请帮忙。。
暂无答案!
目前还没有任何答案,快来回答吧!