1

我正在使用向右滑动NavigationDrawer来显示一些过滤器。问题是右侧的抽屉只有在做出甩动手势时才会保持打开状态。如果我将重力属性更改为startor left,那么它会保持正常打开状态。我认为正常是缓慢拖动然后放手将使抽屉保持打开状态。抽屉在右侧,似乎有偏向关闭位置。

难道我做错了什么?有没有办法让它正常滑动?我的用户无法确定他们需要甩动才能保持抽屉打开。

使用支持库。

需要投掷:

<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" >

谢谢!

4

2 回答 2

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);
            }
        }
    }
});
于 2013-10-31T06:47:18.203 回答
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);
                }
            }
        }
    });
于 2013-09-02T23:44:17.897 回答