sách gpt4 ăn đã đi

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

In lại 作者:搜寻专家 更新时间:2023-11-01 08:58:37 27 4
mua khóa gpt4 giày nike

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

cách trình bày



android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<>

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


android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="200dp" android:layout_marginTop="100dp">





父 View :

public class ParentView extends FrameLayout implements View.OnTouchListener {
public ParentView(Context context, AttributeSet attrs) {
super(bối cảnh, thuộc tính);
setOnTouchListener(this);
}

@Ghi đè
public boolean onTouch(View view, MotionEvent event) {
Logger.write("Parent is touching");
trả về false;
}
}

客户端 View :

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

public ChildView(Context context, AttributeSet attrs) {
super(bối cảnh, thuộc tính);
myGestureDetector=new GestureDetector(context, new MyGestureDetector());
setOnTouchListener(this);
}


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

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

@Ghi đè
public boolean onDown(MotionEvent e) {
trả về giá trị đúng;
}

@Ghi đè
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
thử {
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) {
trả về false;
}

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

}
} catch (Ngoại lệ e) {
// nothing
}

trả về false;
}
}
}

câu trả lời hay nhất

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

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

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress