如何点击工具栏后面的视图?

Mat*_*ers 12 android android-actionbar android-support-library material-design android-toolbar

我有一个透明/半透明背景的工具栏,覆盖内容.因此,在工具栏后面,可以显示可单击的视图.问题是它们无法通过工具栏单击,因为工具栏正在捕获click事件.

我尝试设置android:clickable="false",android:focusable="false"android:focusableInTouchMode="false"为工具栏,但它没有任何效果.如何通过工具栏将点击发送到基础视图?

Mat*_*ers 27

看看实施情况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;
}
Run Code Online (Sandbox Code Playgroud)

解决方案是扩展Toolbar和覆盖onTouchEvent.

public class NonClickableToolbar extends Toolbar {

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)