【问题标题】:pass touch event to beneath view depends on upper views touch将触摸事件传递给下视图取决于上视图触摸
【发布时间】:2014-05-21 07:46:07
【问题描述】:

我有如下布局。

<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 扩展而来的 customView。它看起来像以下..

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),我不想将触摸事件传递给线性布局。如果用户点击框架布局(id = content_frame),我想将触摸事件传递给线性布局以支持按钮的点击。任何人都可以建议我如何做到这一点?

图像视图和frameLayout的Ontouch:

 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;
        }
    });

【问题讨论】:

  • 在 imageView 的 onTouch 中我调用 DisabledGestureView.setDisableStatus(false);在 framelayout(id = content_frame) 的 onTouch 中,我正在调用 DisabledGestureView.setDisableStatus(true);允许/允许触摸事件在 DisabledGestureView 的视图下方。但它没有用。

标签: android view touch gesture touch-event


【解决方案1】:

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

【讨论】:

  • @tyczg..,我已经按照你说的做了。我添加了 imageview 和 framelayout 的 touchevent 供您参考。但是当我点击框架布局上的确切位置时,按钮点击不起作用。
  • 不,你没有,返回super方法传递了触摸事件,所以你总是返回false
  • 欢迎 4 你的回复.. 你想让我在 DisabledGestureView.. 的 onTouch 上返回 true 而不是返回 super.onTouchEvent(ev) 吗?
  • 如果您只想传递框架布局上的触摸并且这永远不会改变您在onTouchListener 中所做的所有ImageView 是返回true。你不需要你的手势覆盖类
  • 我使用 GestureOverlayView 的原因是为了绘制自定义手势。我非常需要这个。触摸 imageView 应该开始绘制手势绘图,并且触摸 Framelayout 应该将触摸传递给按钮单击。这就是我想要达到的目标......
猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 2017-04-28
相关资源
最近更新 更多