有人知道如何组合onLongPress(MotionEvent e)onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)吗?我们的想法是从长按开始,然后继续在某些视图上滑动。
当前代码:

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()(当我将手指滑过视图时)或onLongPress(),但我想从onLongPress()开始,然后检测滑动,以便我可以选择相应的视图来显示。

最佳答案

gesturedector.setislongpressenabled(false):https://developer.android.com/reference/android/view/GestureDetector.html#setIsLongpressEnabled(boolean)但通过这种方式可以释放onlongclick事件。但是,您可以通过计算从最初的“向下”键经过的时间来实现onlongclick事件,然后在这段时间之后提升代码/侦听器。

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

10-08 23:47