gpt4 book ai didi

android - 将手势监听器添加到 subview 而不隐藏父级的触摸事件

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:37 27 4
gpt4 key购买 nike

我想要一个覆盖 View 来接收手势事件,但仍然继续接收父(下方) View 的 onTouch 事件。但是, subview 中的手势似乎隐藏父 onTouch 事件。

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ParentView

android:background="#FFFFFF"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ChildView android:background="#FF4433"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="200dp" android:layout_marginTop="100dp">

</ChildView>

</ParentView>
</LinearLayout>

父 View :

public class ParentView extends FrameLayout implements View.OnTouchListener {
public ParentView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(this);
}

@Override
public boolean onTouch(View view, MotionEvent event) {
Logger.write("Parent is touching");
return false;
}
}

客户端 View :

public class ChildView extends FrameLayout implements View.OnTouchListener {
private GestureDetector myGestureDetector;
private boolean isTapping;

public ChildView(Context context, AttributeSet attrs) {
super(context, attrs);
myGestureDetector=new GestureDetector(context, new MyGestureDetector());
setOnTouchListener(this);
}


@Override
public boolean onTouch(View view, MotionEvent event) {
return myGestureDetector.onTouchEvent(event);
}

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
final ViewConfiguration vc = ViewConfiguration.get(getContext());
final int swipeMinDistance = vc.getScaledPagingTouchSlop();
final int swipeMaxOffPath = vc.getScaledTouchSlop();
final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity() / 2;
if (Math.abs(e1.getY() - e2.getY()) > swipeMaxOffPath) {
return false;
}

// right to left swipe
if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) >
swipeThresholdVelocity) {
Logger.write("Swipe!!!!!!!!!!!!!!!!!!!!!!!");

}
} catch (Exception e) {
// nothing
}

return false;
}
}
}

最佳答案

在父 View 而不是 subview 中实现 GestureDetector,否则如果父 View 有 n 个 subview ,您将需要 n 个检测器。

关于android - 将手势监听器添加到 subview 而不隐藏父级的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17010764/

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