我的布局如下。
<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/