通过图像单击打开导航抽屉

时间:2015-04-16 20:27:17

标签: android navigation-drawer

我有导航抽屉活动。我只需单击操作栏中的主页按钮即可打开导航栏。

我想知道是否可以通过单击ImageView来打开它,这将是一个片段以及如何做到这一点。

感谢。

===== 编辑:

我通过向导创建了我的活动,因此我有MainActivity,Placeholder Activity(用于内容)和NavigationDrawerActivity(用于actionBar和Drawer)。

在MainActivity的onCreate()中,我有:                     的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));

在占位符片段中,我有:

 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);
                            }
                        }
                    }
                });

navigationDrawer片段就像左边的向导一样。

这是我的堆栈跟踪:

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)

2 个答案:

答案 0 :(得分:1)

是的,这是可能的。在按钮上单击使用: ((YourActivityWithDrawer)getActivity()).drawerLayout.openDrawer(your_drawer);

其中:

  • YourActivityWithDrawer是您的Activity抽屉,

  • drawerLayout是您的DrawerLayout对象

  • your_drawer是实际的抽屉对象。

方法描述为here

答案 1 :(得分:0)

我在MainActivity中创建了这两个全局变量:

DrawerLayout drawerLayout;
int fragmentId;

MainActivity的onCreate()看起来像这样:

的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);

最后,我的占位符片段:

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));
                            }
                        }
                    }
                });