-1

I have a drawer and ive tried to double it. Well it went pretty well, but i had an issue with part of it. If I open the first drawer and click on an item it opens a new drawer and you can choose again. So far so good. the problem is I was tring to add a feature that if you are on the second drawer and you close it, it will instantly open the first drawer.

public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
            if(layer == 1){
                layer = 0;
                positions = new int[]{-1, -1};
                drawerListview.setAdapter(adapter);
                ***drawerLayout.openDrawer(drawerLayout);***
            }else if(layer == 2){
                layer = 0;
                positions = new int[]{-1, -1};
                drawerListview.setAdapter(adapter);
            }
        }

I marked down where i tried to open the first drawer again. It didnt throw any error but it just didnt worked.

Tal Gonen
  • 29
  • 5

2 Answers2

0

In the following snippet I'm going to leave out the layer and adapter code as it has no obvious impact on drawers (at least to me).

What you want to do is check if the closed drawer was the right one then ask the drawer layout to open the left one.

public void onDrawerClosed(View drawerView) {
    super.onDrawerClosed(drawerView);
    // store the mRightDrawerView as variable in onCreate...
    if (drawerView == mRightDrawerView) {
        // you ask the layout to open left drawer
        mDrawerLayout.openDrawer(mLeftDrawerView);
        // or mDrawerLayout.openDrawer(GravityCompat.START);
    }
}

On a second glance, your code is very close.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

You want to wait for the drawer's animation to be finished before opening it again:

@Override
public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    mDrawer.post(new Runnable() {
        @Override
        public void run() {
            mDrawer.openDrawer(Gravity.START);
        }
    });
}

Gravity.START is used in the xml layout:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
              android:layout_width="240dp"
              android:layout_height="match_parent"
              android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Simas
  • 43,548
  • 10
  • 88
  • 116