android - 打开抽屉导航时,工具栏按钮不响应触摸

标签 android android-appcompat material-design android-toolbar

我正在使用 appcompat v7 库开发我的应用程序的 Material Design 版本,但我遇到了抽屉导航的问题。当它打开时, Material 设计工具栏中的按钮停止运行 - 抽屉导航外的任何触摸都会关闭抽屉。以下是我的意思的 gif

the gif

这是我用于 Activity 的 xml 布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:elevation="4dp"
            />

        <FrameLayout
            android:id="@+id/note_list_container"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <net.rymate.notes.ui.FloatingActionButton
            android:id="@+id/fabbutton"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp" />
    </FrameLayout>
    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="300dp"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ListView
            android:id="@+id/cat_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="?catBackColour" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

下面是初始化抽屉和工具栏的 onCreate 代码。

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

    ROBOTO_LIGHT = Typeface.createFromAsset(this.getAssets(), "Roboto-Light.ttf");
    ROBOTO_LIGHT_ITALICS = Typeface.createFromAsset(this.getAssets(), "Roboto-LightItalic.ttf");

    setContentView(R.layout.activity_notes);

    if (findViewById(R.id.note_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        FragmentManager fm = getSupportFragmentManager();
        //list.setActivateOnItemClick(true);
    }


    if (!mTwoPane) {
        final FloatingActionButton mFab = (FloatingActionButton) findViewById(R.id.fabbutton);
        mFab.init(Color.parseColor("#1e90ff"));
        mFab.setFabDrawable(getResources().getDrawable(R.drawable.ic_action_new));
        mFab.showFab();

        mFab.setOnClickListener(this);

        list = new NotesListFragment(mFab);
    } else {
        list = new NotesListFragment();
    }


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

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.note_list_container, list)
            .commit();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // the layout
    mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            toolbar,  /* toolbar */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle("Rymate Notes");
            supportInvalidateOptionsMenu();
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle("Categories");
            supportInvalidateOptionsMenu();
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    pref = getSharedPreferences("rymatenotesprefs", MODE_PRIVATE);

    mDrawerList = (ListView) findViewById(R.id.cat_list);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    if (mDbHelper.fetchAllNotes().getCount() == 0) {
        IntroFragment fragment = new IntroFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.note_list_container, fragment)
                .commit();

    }

    getCategories(); // calls a function which populates the listview

}

有没有办法解决这个问题?

最佳答案

似乎触摸事件被抽屉的阴影偷走了,即 DrawerLayout 一直拦截触摸事件,因为 Toolbar 是内容 View 的一部分,不像 ActionBar 位于装饰 View 之上。

可能的解决方法是拦截触摸事件:

如果它介于 0(顶部)和 Toolbar 高度(底部)之间,则将事件直接分派(dispatch)给 Toolbar 对象。否则保持正常行为。

抽屉切换点击的情况是否相同?

关于android - 打开抽屉导航时,工具栏按钮不响应触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441624/

相关文章:

html - 从 Material 设计中的 .mdl-layout__drawer-button 中删除外边框线?

css - 如何在纸图标按钮的标签后获取伪元素

java - android studio中的 "Throwable Result Of Method Call Ignored"是什么?

java - Android MapView - 扩展 OverlayItem 以存储用于在 onTap 中访问的 URL

java - 如何连接到时间服务器以获取当前时间设置?也有任何时间服务器吗?

android - 在 Android 版 Firebase 上存储变量的正确方法

android - 在Android中使用 “prominent top app bar”滚动的应用程序中延迟的滚动行为

android - 强制在android中的searchview中写大写

java - 无法在 Android Studio 中导入 android.support.v7.appcompat.R

android - 无法解析 AppCompatActivity