How to open Navigation Drawer with no actionbar, open with just a button

前端 未结 7 1532
悲&欢浪女
悲&欢浪女 2020-12-02 09:37

I have a navigation bar without any actionbar (I don\'t want an actionbar). I\'m trying to make it so that I have a button that can open the navigation drawer.

I kn

相关标签:
7条回答
  • 2020-12-02 10:05

    It's giving you a null pointer because you are trying to find the drawer layout in the fragment's view, when it is actually in the activities view.

    A quick hack to do what you want is to find the view like:

    getActivity().findViewById(R.id.drawer_layout)
    

    That should work. A better way is to have a method on the activity for opening the drawer

    public void openDrawer(){
        mDrawerLayout.openDrawer(mDrawerLayout);
    }
    

    In the activity onCreate run your findViewById:

    mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
    

    mDrawerLayout should be a member variable of your activity.

    Then in your fragment you can call:

    //cast activity to MyActivity so compiler doesn't complain
    ((MyActivity)getActivity()).openDrawer();
    

    An even better way to do it is to create a listener in the fragment and set the activity as a listener to the fragment. Then you can call a method on the activity, similar to above. I'll let you do some research on how to do that.

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