如何在RelativeLayout的触摸事件传送到视图下面? [英] How to transfer touch events to the view beneath in a RelativeLayout?
问题描述
我有一个滚动型
另一个视图顶部(以按钮
)。在滚动型
走的是整个屏幕,并模糊是它下面的视图。
I have a ScrollView
on top of another view(with Buttons
). The ScrollView
is taking the whole screen and is obscuring the view that is beneath it.
在我的应用程序某些时候,我所需要的滚动型
被禁用(但仍然可见),所有的触摸事件转移到按钮
这是滚动型
之下。我该怎么办呢?像按钮的一些看法
自动这样做时禁用但滚动型
不这样做。
At some point in my app I need the ScrollView
to be disabled (but still visible) and transfer all the touch events to the Buttons
that are beneath the ScrollView
. How can I do that? Some views like Buttons
are automatically doing that when disabled but a ScrollView
is not doing that.
推荐答案
尝试实现自己的滚动型
里面有一个标志,指示状态(禁用/启用)和还覆盖了 onTouchEvent
和 dispatchTouchEvent
来让触摸事件获得通过滚动型
。下面是一个例子:
Try to implement your own ScrollView
which has a flag to indicate the status(disabled/enabled) and also overrides the onTouchEvent
and dispatchTouchEvent
to let the touch events get pass the ScrollView
. Here is an example:
public class DisabledScrollView extends ScrollView {
private boolean mIsDisable = false;
// if status is true, disable the ScrollView
public void setDisableStatus(boolean status) {
mIsDisable = status;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// no more tocuh events for this ScrollView
if (mIsDisable) {
return false;
}
return super.onTouchEvent(ev);
}
public boolean dispatchTouchEvent(MotionEvent ev) {
// although the ScrollView doesn't get touch events , its children will get them so intercept them.
if (mIsDisable) {
return false;
}
return super.dispatchTouchEvent(ev);
}
}
然后,所有你需要做的是改变标志的值。看看它是否工作。
Then all you have to do is change the value of that flag. See if it works.
这篇关于如何在RelativeLayout的触摸事件传送到视图下面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!