android - 将触摸事件传递到下方 View 取决于上方 View 触摸

标签 android view touch gesture touch-event

我的布局如下。

<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">

 <LinearLayout android:layout_width="match_parent"
   android:layout_height="match_parent">
       <Button/><Button/>
 </LinearLayout>

 <DisabledGestureView android:layout_width="match_parent"
  android:layout_height="match_parent">

      <FrameLayout android:layout_width="match_parent"
      id="@+android/content_frame" 
      android:layout_height="match_parent">
         <ImageView android:layout_width="200dp"
         id="@+android/icon" 
         android:layout_height="200dp">
      </FrameLayout>

 </DisabledGestureView>
</FrameLayout>

DisableGestureView 是我从 GestureOverlayView 扩展而来的自定义 View 。看起来像以下..

public class DisabledGestureView extends GestureOverlayView {

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

private boolean mIsDisable = true;

public void setDisableStatus(boolean status) {
    mIsDisable = status;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    MLog.d("", "overlay touch : " + mIsDisable);
    if (mIsDisable) {
        return false;
    }       
    return super.onTouchEvent(ev);
}

public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mIsDisable) {
        return false;
    }
    return super.dispatchTouchEvent(ev);
}
}

如果用户点击 imageview(id = icon),我不想将触摸事件传递给 linearlayout。如果用户点击 framelayout(id = content_frame),我想将触摸事件传递给 linearlayout 以支持按钮的点击。谁能建议我怎么做?

Ontouch ImageView 和 frameLayout:

 imageview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

                disabledGestureView.setDisableStatus(false);


            return false;
        }
    });


    framelayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            disabledGestureView.setDisableStatus(true);
            return false;
        }
    });

最佳答案

如果您想传递触摸,则在 onTouch 中返回 false(未处理),如果您不想传递它,则返回 true(已处理)。你所要做的就是找出被触及的 View

关于android - 将触摸事件传递到下方 View 取决于上方 View 触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22939992/

相关文章:

iphone - Objective - C 如何使用 View Controller iphone 管理多个 View

javascript - 触摸设备阻止父元素滚动

c# - Windows 窗体触地事件

android - 获得一个简单的 throw 手势

java - Android API 23 java.net.UnknownHostException : Unable to resolve host "URL": No address associated with hostname

android - 运动事件的隐形 View 检测

iphone - Cocoa touch - UIAnimation问题

android - 在 Android 中使用 C++ 构建 NDK 的问题

android - 可以控制整个应用程序的 super Activity Controller

mysql - 从此查询创建 View