gpt4 book ai didi

javascript - 如果 webview 中有触摸事件,则阻止触摸事件被 ViewPager 拦截

转载 作者:可可西里 更新时间:2023-11-01 18:47:27 25 4
gpt4 key购买 nike

我有一个显示 fragment 页面的 ViewPager。每个 Fragment 都是一个显示离线网站的 WebView。其中一些 webviews 中有 javascript 滑动事件以使其工作。

我想这样实现,如果 webview 没有滑动事件(在 javascript 中),那么触摸事件将传递给 ViewPager,如果没有,则由 webview 使用。

我已经尝试覆盖 ViewPagers

@Override
public boolean onInterceptTouchEvent(MotionEvent event)
{
return (this.swipeable) ? super.onInterceptTouchEvent(event) : false;
}

然而,这阻止了 ViewPager 的滑动(如预期的那样),但也阻止了滑动事件到达 webview,所以我有两个不希望的结果。

有没有办法先将触摸事件传递给 webView,然后如果它没有在 fragment 中被消耗,则将其传递回 ViewPager

如果我在我的 MyViewPager 中出错,我有代码:

public class MyViewPager extends ViewPager implements   GestureDetector.OnGestureListener
{
private static final String TAG = MyViewPager.class.getSimpleName();
private boolean swipeable = true;
private float lastX = 0;
private long lastTime = 0 ;
private GestureDetector mGestureDetector;
private boolean mScrolling = false;

public MyViewPager(Context context) {
super(context);
mGestureDetector = new GestureDetector(context, this);
}

public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, this);
}

public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event)
{

/* float speed = 0;
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
if(lastX!=0) {
speed = (event.getRawX()- lastX) / (System.currentTimeMillis()-lastTime);
if(speed < 0.0 ){speed *=-1;}
Log.e(TAG, "Move at speed ->"+speed);
if(speed >0.5){return false;}
}
lastX = event.getRawX();
lastTime = System.currentTimeMillis();
return true;
}*/
// return true;

return (this.swipeable) ? super.onInterceptTouchEvent(event) : false;
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
Log.i(TAG, "onTouch event");
// mGestureDetector.onTouchEvent(event);
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velX, float velY) {
Log.i(TAG, "flinging");
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distX, float distY)
{
float displacement = distX;
return false;
}

// Unused Gesture Detector functions below

@Override
public boolean onDown(MotionEvent event) {
return false;
}

@Override
public void onLongPress(MotionEvent event) {
// we don't want to do anything on a long press, though you should probably feed this to the page being long-pressed.
}

@Override
public void onShowPress(MotionEvent event) {
// we don't want to show any visual feedback
}

@Override
public boolean onSingleTapUp(MotionEvent event) {
// we don't want to snap to the next page on a tap so ignore this
return false;
}

最佳答案

我现在有两个解决这个问题的方法是扩展 ViewPager 类并覆盖

public boolean onInterceptTouchEvent(MotionEvent event)
{
// code
}

然而,更好的解决方案是覆盖

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

为了完整性这里是全类

public class ZoltansViewPager extends ViewPager {

private static final String TAG = ZoltansViewPager.class.getSimpleName();
private boolean swipeable = true;
private float lastX = 0;
private long lastTime = 0 ;
private GestureDetector mGestureDetector;
private boolean mScrolling = false;


public ZoltansViewPager(Context context) {
super(context);
}

public ZoltansViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}


public void setSwipeable(boolean swipeable) {
this.swipeable = swipeable;
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
Log.i(TAG , " boolean checkV ->"+checkV + ", int dx-> "+dx+", int x->"+x+", int y->"+y);
if(x <300 || x >1700 ){return true;}
return false;
}
}

希望对别人有帮助

关于javascript - 如果 webview 中有触摸事件,则阻止触摸事件被 ViewPager 拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369671/

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