右侧的导航抽屉不会保持打开状态

时间:2013-06-26 17:09:30

标签: android navigation-drawer

我正在使用右侧滑动NavigationDrawer来显示一些过滤器。问题是右边的抽屉只做一个投掷手势时保持打开状态。如果我将重力属性更改为startleft,则它会保持正常打开状态。我认为正常情况是缓慢拖动然后放手将保持抽屉打开。右侧有抽屉,似乎对关闭位置有偏见。

我做错了吗?有没有办法让它正常滑动?我的用户很难搞清楚他们需要扔掉以保持抽屉打开。

使用支持库。

需要投掷:

<LinearLayout
    android:id="@+id/filtersDrawer"
    android:layout_width="200dp"
    android:layout_height="fill_parent"
    android:layout_gravity="end"
    android:background="@color/drawer_color"
    android:orientation="vertical" >

不需要投掷:

<LinearLayout
    android:id="@+id/filtersDrawer"
    android:layout_width="200dp"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:background="@color/drawer_color"
    android:orientation="vertical" >

谢谢!

2 个答案:

答案 0 :(得分:2)

我觉得我已经提出了正确的逻辑来解决股票实施中的不足。

drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
    /**
     * percentage of its width that the drawer needs to be open to
     * trigger a toggle (between 0 and 1).
     **/
    private static final float TOGGLE_OFFSET = 0.3f;

    private float lastSlideOffset;
    private boolean isDragging;

    @Override
    public void onDrawerSlide(View view, float slideOffset) {
        lastSlideOffset = slideOffset;
    }

    @Override
    public void onDrawerOpened(View view) {}

    @Override
    public void onDrawerClosed(View view) {}

    @Override
    public void onDrawerStateChanged(int newState) {
        switch (newState) {
            case DrawerLayout.STATE_DRAGGING:
                isDragging = true;
                break;

            case DrawerLayout.STATE_IDLE:
                isDragging = false;
                break;

            case DrawerLayout.STATE_SETTLING:
                if (isDragging) {
                    attemptToggle();
                }
                break;
        }
    }

    private void attemptToggle() {
        if (drawerLayout.isDrawerOpen(Gravity.END)) {
            if (lastSlideOffset <= 1 - TOGGLE_OFFSET) {
                drawerLayout.closeDrawer(Gravity.END);
            } else {
                drawerLayout.openDrawer(Gravity.END);
            }

        } else {
            if (lastSlideOffset >= TOGGLE_OFFSET) {
                drawerLayout.openDrawer(Gravity.END);
            } else {
                drawerLayout.closeDrawer(Gravity.END);
            }
        }
    }
});

答案 1 :(得分:1)

您可以使用侦听器解决此问题,并在用户拖动某个百分比时强制打开/关闭抽屉,然后释放抽屉,如下所示:

    mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        private float mLastOffset;
        @Override
        public void onDrawerSlide(View view, float slideOffset) {
            mLastOffset = slideOffset;
        }

        @Override
        public void onDrawerOpened(View view) {

        }

        @Override
        public void onDrawerClosed(View view) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {
            if (newState == DrawerLayout.STATE_SETTLING) {
                if (mLastOffset > 0.5f) {
                    mDrawerLayout.openDrawer(Gravity.END);
                } else {
                    mDrawerLayout.closeDrawer(Gravity.END);
                }
            }
        }
    });