如何单击工具栏后面的视图?
How to click views behind a Toolbar?
我有一个工具栏,其中 transparent/translucent 背景覆盖了内容。因此,在工具栏后面,可以显示可单击的视图。问题是无法通过工具栏单击它们,因为工具栏正在捕获单击事件。
我尝试为工具栏设置android:clickable="false"
、android:focusable="false"
和android:focusableInTouchMode="false"
,但没有效果。如何通过工具栏将点击发送到基础视图?
看看 Toolbar
的实现。它 吃掉触摸事件,而不管 clickable
属性如何。
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Toolbars always eat touch events, but should still respect the touch event dispatch
// contract. If the normal View implementation doesn't want the events, we'll just silently
// eat the rest of the gesture without reporting the events to the default implementation
// since that's what it expects.
final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_DOWN) {
mEatingTouch = false;
}
if (!mEatingTouch) {
final boolean handled = super.onTouchEvent(ev);
if (action == MotionEvent.ACTION_DOWN && !handled) {
mEatingTouch = true;
}
}
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
mEatingTouch = false;
}
return true;
}
解决方案是从 Toolbar
扩展并覆盖 onTouchEvent
。
public class NonClickableToolbar extends Toolbar {
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
您的设计需要调整。您不能在 transparent/translucent 工具栏后面提供可点击的视图。您需要提供与工具栏高度相等的顶部填充 and/or 为工具栏实施快速 return 模式。
工具栏消耗所有点击。您需要子类化工具栏,就像已经提到的@Matthias Robbens。
如果您仍希望能够在工具栏上设置点击侦听器,请使用:
/** Do not eat touch events, like super does. Instead map an ACTION_DOWN to a click on this
* view. If no click listener is set (default), we do not consume the click */
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_POINTER_DOWN){
return performClick();
}
return false;
}
更简单的答案是:
toolbarV?.isClickable = false
toolbarV?.isFocusable = false
我有一个工具栏,其中 transparent/translucent 背景覆盖了内容。因此,在工具栏后面,可以显示可单击的视图。问题是无法通过工具栏单击它们,因为工具栏正在捕获单击事件。
我尝试为工具栏设置android:clickable="false"
、android:focusable="false"
和android:focusableInTouchMode="false"
,但没有效果。如何通过工具栏将点击发送到基础视图?
看看 Toolbar
的实现。它 吃掉触摸事件,而不管 clickable
属性如何。
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Toolbars always eat touch events, but should still respect the touch event dispatch
// contract. If the normal View implementation doesn't want the events, we'll just silently
// eat the rest of the gesture without reporting the events to the default implementation
// since that's what it expects.
final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_DOWN) {
mEatingTouch = false;
}
if (!mEatingTouch) {
final boolean handled = super.onTouchEvent(ev);
if (action == MotionEvent.ACTION_DOWN && !handled) {
mEatingTouch = true;
}
}
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
mEatingTouch = false;
}
return true;
}
解决方案是从 Toolbar
扩展并覆盖 onTouchEvent
。
public class NonClickableToolbar extends Toolbar {
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
您的设计需要调整。您不能在 transparent/translucent 工具栏后面提供可点击的视图。您需要提供与工具栏高度相等的顶部填充 and/or 为工具栏实施快速 return 模式。
工具栏消耗所有点击。您需要子类化工具栏,就像已经提到的@Matthias Robbens。
如果您仍希望能够在工具栏上设置点击侦听器,请使用:
/** Do not eat touch events, like super does. Instead map an ACTION_DOWN to a click on this
* view. If no click listener is set (default), we do not consume the click */
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_POINTER_DOWN){
return performClick();
}
return false;
}
更简单的答案是:
toolbarV?.isClickable = false
toolbarV?.isFocusable = false