【问题标题】:NavigationDrawer isDrawerOpened always returns trueNavigationDrawer isDrawerOpened 始终返回 true
【发布时间】:2015-09-14 17:52:34
【问题描述】:

我遇到了奇怪的问题。我在我的应用程序中使用支持库中的导航抽屉。
这是布局

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear_layout_main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:visibility="gone" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/layout_drawer_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fillViewport="true">

    </ScrollView>

    <ScrollView
        android:id="@+id/layout_drawer_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fillViewport="true" />
</android.support.v4.widget.DrawerLayout>

我在设置内容视图后找到所有视图。

private FrameLayout mLeftDrawerContainer, mRightDrawerContainer;
private DrawerLayout mDrawerMainLayout;
        mDrawerMainLayout = (DrawerLayout) rootBaseView.findViewById(R.id.navigation_drawer);
        mLeftDrawerContainer = (ScrollView) rootBaseView.findViewById(R.id.layout_drawer_left);
        mRightDrawerContainer = (ScrollView) rootBaseView.findViewById(R.id.layout_drawer_right);

我尝试了很多方法来关闭抽屉,它在视觉上关闭它,我的意思是一切都很好,抽屉慢慢离开屏幕并保持关闭状态,但从代码的角度来看它总是返回 true。

 protected boolean isDrawersOpened() {
    boolean rightDrawerOpened = mDrawerMainLayout.isDrawerOpen(mRightDrawerContainer);
    boolean leftDrawerOpened = mDrawerMainLayout.isDrawerOpen(mLeftDrawerContainer);
    return  rightDrawerOpened || leftDrawerOpened ;
}

永远正确

public void closeDrawer() {
    mDrawerMainLayout.closeDrawer(GravityCompat.END);
    mDrawerMainLayout.closeDrawer(GravityCompat.START);
    mDrawerMainLayout.closeDrawer(mRightDrawerContainer);
    mDrawerMainLayout.closeDrawer(mLeftDrawerContainer);
    mDrawerMainLayout.closeDrawers();
}

所以它应该可以工作,但总是返回 true。

如果是支持库中的错误,没问题我会等待并在活动中使用布尔变量来指示这一点,但也许有人处理了同样的问题。

提前感谢您的任何帮助和想法。

【问题讨论】:

  • 您是否尝试过通过重力而不是视图进行检查?
  • 当然,我已经更新了我的问题。我错过了这个抱歉

标签: android navigation-drawer drawerlayout android-compatibility


【解决方案1】:

对不起,我的疏忽。
我在我的代码中犯了一个错误

   if (hasLeftDrawer) {
        setupLeftDrawer();
    } else {
        mDrawerMainLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, mLeftDrawerContainer);
    }
    if (hasRightDrawer) {
        setupRightDrawer();
    } else {
        mDrawerMainLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, mRightDrawerContainer);
    }

应该是

    if (hasLeftDrawer) {
        setupLeftDrawer();
    } else {
        mDrawerMainLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mLeftDrawerContainer);
    }
    if (hasRightDrawer) {
        setupRightDrawer();
    } else {
        mDrawerMainLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mRightDrawerContainer);
    }

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多