gpt4 book ai didi

android - onLongPress 后继续 onScroll 事件

转载 作者:太空狗 更新时间:2023-10-29 14:38:46 26 4
gpt4 key购买 nike

有谁知道如何结合使用 onLongPress(MotionEvent e)onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)?想法是从 onLongPress 开始,但继续滑动某些 View 。

我目前的代码:

private class CustomListener implements View.OnTouchListener {

ImagePreviewer imagePreviewer = new ImagePreviewer();

private GestureDetector gestureDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {

ImageView currentImageView;

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

float x = e2.getX();
float y = e2.getY() + mConstraintLayoutContainerCoordinates[1];

currentImageView = getProperImage(mCurrentImagesNumberForPoll, (int) x, (int) y);

if(currentImageView != null) {
imagePreviewer.show(getActivity(), currentImageView);
}

return super.onScroll(e1, e2, distanceX, distanceY);
}

@Override
public void onLongPress(MotionEvent e) {

Log.d(TAG, "LONG PRESS");

float x = e.getX();
float y = e.getY() + mConstraintLayoutContainerCoordinates[1];
currentImageView = getProperImage(mCurrentImagesNumberForPoll, (int) x, (int) y);
if(currentImageView != null) {
imagePreviewer.show(getActivity(), currentImageView);
}

super.onLongPress(e);
}
});

@Override
public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("TAG", "DOWN or CANCEL ");
imagePreviewer.hide();
}
gestureDetector.onTouchEvent(event);
return true;
}
}

事情是 onScroll() 被调用(当我在 View 上滑动手指时)或 onLongPress(),但我想从 onLongPress 开始(),然后检测滑动,这样我就可以选择适当的 View 来相应地显示。

最佳答案

GestureDetectorCompat 在检测到 onLongPress 后阻止调用 onScroll。可以通过向检测器发送 ACTION_CANCEL 来取消阻止。

@Override
public void onLongPress(MotionEvent e) {
isLongPressed = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (detector.onTouchEvent(event)) {
return true;
}

switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
if (isLongPressed) {
isLongPressed = false;
MotionEvent cancel = MotionEvent.obtain(event);
cancel.setAction(MotionEvent.ACTION_CANCEL);
detector.onTouchEvent(cancel);
}
break;
}
return super.onTouchEvent(event);
}

关于android - onLongPress 后继续 onScroll 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51704307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com