简体   繁体   中英

Open a navigation drawer by image click

I have a Navigation Drawer Activity. I just can open the navigation drawer by clicking the home button in Action Bar.

I want to know if it is possible to open it by clicking a ImageView, that would be in a fragment and how to do that.

thanks.

===== Edit:

I created my activity by the wizard, so I have the MainActivity, the Placeholder Activity (for content) and NavigationDrawerActivity (for actionBar and Drawer).

In MainActivity's onCreate(), I have: setContentView(R.layout.activity_main);

                //Cria menu de navegação
                mNavigationDrawerFragment = (NavigationDrawerFragment)
            locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
                mTitle = getTitle();
                mDrawerLayout= mNavigationDrawerFragment.getmDrawerLayout();

                // Configura menu de navegação
                mNavigationDrawerFragment.setUp(
                        R.id.navigation_drawer,
                        (DrawerLayout) findViewById(R.id.drawer_layout));

In Placeholder fragment I have:

 rootView = inflater.inflate(R.layout.fragment_main, container, false);
                ImageView imageView = (ImageView) rootView.findViewById(R.id.logoInicio);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("Image click", "Clicked");
                        if (main.mNavigationDrawerFragment!=null){
                            if (!main.mNavigationDrawerFragment.isDrawerOpen()) {
                                main.mDrawerLayout.openDrawer(main.mydrawer);
                            }
                        }
                    }
                });

The navigationDrawer fragment is just like the wizard left.

This is my stacktrace:

FATAL EXCEPTION: main
    Process: workshopee.ct.ufrn.br.ssmonitor, PID: 15902
    java.lang.NullPointerException
            at workshopee.ct.ufrn.br.ssmonitor.PlaceholderFragment$2.onClick(PlaceholderFragment.java:144)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18462)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Yes it is possible. On button click use: ((YourActivityWithDrawer)getActivity()).drawerLayout.openDrawer(your_drawer);

where:

  • YourActivityWithDrawer is your Activity containing drawer,

  • drawerLayout is your DrawerLayout object

  • your_drawer is the actual drawer object.

Method is described here

I created these two global variables in MainActivity:

DrawerLayout drawerLayout;
int fragmentId;

MainActivity's onCreate() looks like that:

setContentView(R.layout.activity_main);

                //Cria menu de navegação
                mNavigationDrawerFragment = (NavigationDrawerFragment)
                        getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
                mTitle = getTitle();

                fragmentId = R.id.navigation_drawer;
                drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                // Configura menu de navegação
                mNavigationDrawerFragment.setUp(
                        fragmentId,
                        drawerLayout);

And, finally, my Placeholder fragment:

rootView = inflater.inflate(R.layout.fragment_main, container, false);
                ImageView imageView = (ImageView) rootView.findViewById(R.id.logoInicio);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (main.mNavigationDrawerFragment!=null){
                            if (!main.mNavigationDrawerFragment.isDrawerOpen()) {
                                main.drawerLayout.openDrawer(getActivity().findViewById(main.fragmentId));
                            }
                        }
                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM