What do you need to pass to v4.widget.DrawerLayout.isDrawerOpen()/.openDrawer()/.closeDrawer()

后端 未结 4 1232
死守一世寂寞
死守一世寂寞 2020-12-18 18:30

I\'ve been trying to move my code across to the DrawerLayout as suggested by android here as SlidingDrawer is deprecated.

My problem is tha

相关标签:
4条回答
  • 2020-12-18 18:39

    And, the answer:

    The second child (aka, the "Drawer") is what needs to be passed to the methods. My problem was that by the time I had figured that out I'd reduced the layout to as simple as possible implementation to test - I'd removed the gravity from the "drawer". Without a gravity, you get the above completely unrelated error messages.

    I can confirm I got the code to work using the following setup:

    mMenuPanel.isDrawerOpen(findViewById(R.id.drawer));
    

    and with the layout:

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/mainmenuPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <FrameLayout
            android:id="@+id/drawer"
            android:layout_width="300dp"
            android:layout_height="match_parent" 
            android:layout_gravity="start"/> <!-- This line was the problem!!!!!!!-->
    </android.support.v4.widget.DrawerLayout>
    
    0 讨论(0)
  • 2020-12-18 18:41

    if you want to avoid references to views or layouts in order to call isDrawerOpen, another way can be applied.

    In this case you have to indicate the Gravity:

    mDrawerLayout.isDrawerOpen(Gravity.START);
    

    Being mDrawerLayout a reference to your DrawerLayout.

    as you said, don't forget android:layout_gravity="start"

    0 讨论(0)
  • 2020-12-18 18:47

    If you are using the drawer layout from the support library as we can see from your question that you are, there is also the method:

    closeDrawers()
    

    This will close all open drawers (which is usually just the one).

    0 讨论(0)
  • 2020-12-18 18:54

    In your Activity if you have a reference to the DrawerLayout and in this case the FrameLayout with the id R.id.menuPane you can also do...

    DrawerLayout mMenuPanel = (DrawerLayout) findViewById(R.id.mainmenuPanel);
    FrameLayout mMenuPane = (FrameLayout) findViewById(R.id.menuPane);
    mMenuPanel.isDrawerOpen(mMenuPane);
    

    Basically you have to pass in a reference to the view being used as the drawer within the DrawerLayout, which will always be mMenuPanel.getChildAt(1) anyways.

    0 讨论(0)
提交回复
热议问题