Contextual Action Mode not filtering touches

北慕城南 提交于 2019-12-14 03:46:50

问题


The contextual action mode, when overlayed on top of an android.support.v7.widget.Toolbar, does not appear to filter touches over its entire width, but lets touches "fall through" to (invisible) widgets on the Toolbar.

My Toolbar contains a custom widget (one of the declared benefits of Toolbar). I have the contextual action mode styled to overlay the toolbar, and it hides it entirely. However, when I touch where the (now obscured) custom widget was located, the touch passes through to it. I wonder if this is a bug/oversight, but I may not have grasped this right, or my expectations may be lofty. I assume that the touch would not pass through if there was something on the contextual action overlay in the location of the obscured custom component.

I am testing this on Kitkat.

Here is some sample code:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar_toolbar);
    setSupportActionBar(toolbar);

    TextView barTextView = (TextView) toolbar.findViewById(R.id.textview_bar);
    barTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Bar TextView clicked", Toast.LENGTH_SHORT).show();
        }
    });

    mActionModeCallback = new ActionModeCallback();

    TextView mainTextView = (TextView) findViewById(R.id.textview_main);
    mainTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Main TextView clicked", Toast.LENGTH_SHORT).show();
            if (mActionMode != null) {
                return;
            }
            getSupportActionBar().startActionMode(mActionModeCallback);
        }
    });
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/actionbar_toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        <TextView
            android:id="@+id/textview_bar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Bar"
            android:textColor="#FF0000"/>
    </android.support.v7.widget.Toolbar>
    <TextView
        android:id="@+id/textview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SomeTextToClick"/>
</LinearLayout>

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="windowActionModeOverlay">true</item>
    </style>
</resources>

I also tried setting up the action mode with the following alternative, but the behavior was the same:

Toolbar toolbar = (Toolbar) MainActivity.this.findViewById(R.id.actionbar_toolbar);
mActionMode = toolbar.startActionMode(mActionModeCallback);

At this point I can only think of either explicitly disabling the custom component whenever the contextual action mode comes on, or somehow filling the contextual action bar with invisible junk to more seriously obscure the toolbar.

Some screenshots below.

Before clicking anything:

After clicking "SomeTextToClick" - contextual action mode is now on, and the text in the toolbar is obscured, but at this point I can click in the center of the contextual action bar, and the click gets handled by the listener on the "Bar" TextView in the toolbar:

Can anyone help ?

来源:https://stackoverflow.com/questions/29714057/contextual-action-mode-not-filtering-touches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!