gpt4 book ai didi

android 将事件从 subview 传递给父 View

转载 作者:太空狗 更新时间:2023-10-29 12:43:20 33 4
gpt4 key购买 nike

我有两个自定义 View subview (相对布局)和父 View (框架布局)。当发生单击时,我想让子进程处理该事件,但仍将其进一步传递给父进程,对于子进程中的任何其他事件,只需将其进一步传递而不进行处理。

subview 设置为 match_parent。

这是我目前所拥有的:

public class Child extends RelativeLayout implements View.OnTouchListener {
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// process the event
return true;
}
}

public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}

@Override
public boolean dispatchTouchEvent(MotionEvent e) {
gestrDetect_.onTouchEvent(e);
return super.dispatchTouchEvent(e);
}
}


public class Parent extends FrameLayout {
public Parent(Context context, AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(this);
}

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

当我在 subview 中获取单击事件时,所有其他事件(如滑动或缩放)都不会传递给父 View 。处理子事件的特定子集,同时仍将其中一些事件传递给父事件的正确方法是什么?

编辑

我已将代码更改为以下内容:

public class Child extends RelativeLayout implements View.OnTouchListener {
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(android.view.MotionEvent e1, android.view.MotionEvent e2, float velocityX, float velocityY) {
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// process the event
return true;
}
}

public boolean onTouch(View view, MotionEvent motionEvent) {
return gestrDetect_.onTouchEvent(e);
}

@Override
public boolean dispatchTouchEvent(MotionEvent e) {
return super.dispatchTouchEvent(e);
}
}


public class Parent extends FrameLayout {
@Override
public boolean onTouch(View view, MotionEvent event) {
return gestrDetect_.onTouchEvent(event);
}
}

并且事件仍然没有传递给父级。当我改变

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

return false;

我什至没有收到 child 的 singleTapUp 事件。

最佳答案

您需要创建自己的父 ViewGroup 实现并覆盖 onInterceptTouchEvent() 方法(查看本教程 http://developer.android.com/training/gestures/viewgroup.html)。

有了这个,您可以确定 with 方法应该由父级处理,哪些将传递给子级。

关于android 将事件从 subview 传递给父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21969064/

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