0

我有一个 ViewPager,所以在这个视图寻呼机中我有不同的片段,所以在第一个片段中我有一个包含 360 度可旋转图像的 TouchImageView。当我为 Touchimageview 向左或向右滑动时,返回 viewpager 事件也触发意味着 viewpager 正在滚动,所以我想禁用此 imageview 区域的 viewpager 触摸事件。

例如:ViewPager -> Fragment -> TouchImageView

我已经根据需要完成了几个关于禁用 onInterceptTouchEvent() 和 onTouchEvent() 的答案,但它禁用了片段的完整视图。

任何帮助将不胜感激!

ViewPager.java

public class MyCardViewPager extends ViewPager {

private boolean isScrollingEnabled = true;

public MyCardViewPager(@Nonnull Context context, @Nonnull AttributeSet attrs) {
    super(context, attrs);
}

public MyCardViewPager(@NonNull Context context) {
    super(context);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (isScrollingEnabled) {
        return super.onInterceptTouchEvent(ev);
    }

    return false;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (isScrollingEnabled) {
        return super.onTouchEvent(ev);
    }
    return false;
}

public void enableScroll(boolean enable) {
    isScrollingEnabled = enable;
}

}

片段.java

@Override
        public View onCreateView(@Nonnull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    myCardViewPager = new MyCardViewPager(context);
    myCardViewPager.enableScroll(false);
    imageViewSwipeListener();
    }

    private void imageViewSwipeListener() {
        position = 18;
        objectList();
        imageview.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
                position += 1;
                if (position == 36) {
                    position = 0;
                }
                imageview.setImageResource(objectList.get(position));
            }

            @Override
            public void onSwipeLeft() {
                super.onSwipeLeft();
                position -= 1;
                if (position == -1) {
                    position = 35;
                }
                imageview.setImageResource(objectList.get(position));
              }
        });
    }

OnSwipeTouchListener 类

open class OnSwipeTouchListener(ctx: Context) : View.OnTouchListener {

private val gestureDetector: GestureDetector

companion object {

    private val SWIPE_THRESHOLD = 100
    private val SWIPE_VELOCITY_THRESHOLD = 100
}

init {
    gestureDetector = GestureDetector(ctx, GestureListener())
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
    return gestureDetector.onTouchEvent(event)
}

private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {


    override fun onDown(e: MotionEvent): Boolean {
        return true
    }

    override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
        var result = false
        try {
            val diffY = e2.y - e1.y
            val diffX = e2.x - e1.x
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight()
                    } else {
                        onSwipeLeft()
                    }
                    result = true
                }
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom()
                } else {
                    onSwipeTop()
                }
                result = true
            }
        } catch (exception: Exception) {
            exception.printStackTrace()
        }
        return result
    }
}

open fun onSwipeRight() {}

open fun onSwipeLeft() {}

open fun onSwipeTop() {}

open fun onSwipeBottom() {}

}

4

1 回答 1

0
swipeTouch = new OnSwipeTouchListener(getActivity()) {...};
imageview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        swipeTouch.onTouchEvent(motionEvent);
        imageview.onTouchEvent(motionEvent);
        return true;
    }
});

此方法的第一行只是将 every 传递MotionEvent motionEvent给 same View,这是默认行为,无需设置自定义触摸侦听器。BUTonTouchEvent方法返回布尔值,指示此View消耗的已传递事件。如果默认onTouchEvent返回false,则将相同的事件传递给view父级,如果它也不会消耗它,则再次在层次结构中向上传递,直到某些父级Views ( ViewGroups) 将return true确认它可以处理此事件。

imageview可能是一些自定义/lib 类,它偶尔(总是?)return false允许在层次结构中向上调度触摸事件ViewPager,它可以处理这种触摸和return true. 这就是为什么在触摸发生时,上面的代码片段onTouch方法总是返回- 这个触摸仍然传递给 this ,但即使它没有被处理,也会阻止将相同的事件分派给父母trueimageviewViewreturn true

于 2021-07-20T11:52:48.047 回答