Android 在按钮/图像单击上切换导航抽屉“打开”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22341816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:51:26  来源:igfitidea点击:

Toggle Navigation Drawer 'open' on Button/ Image click

androidandroid-fragmentsnavigation-drawerandroid-actionbar-compat

提问by sabergeek

I've created a fragment that is hooked to an XML containing one image view. A navigation drawer (using actionbar compat) is set up inside activity_main.java and is working fine. What i'm wanting to do is, when I tap on the image inside the fragment, the navigation drawer should 'toggle open'.

我创建了一个与包含一个图像视图的 XML 挂钩的片段。在activity_main.java 中设置了一个导航抽屉(使用actionbar compat)并且工作正常。我想要做的是,当我点击片段内的图像时,导航抽屉应该“切换打开”。

The following code inside onCreateView method returns a null pointer:

onCreateView 方法中的以下代码返回空指针:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
            {   

                View view=inflater.inflate(R.layout.home, container, false);

                ImageView img=(ImageView)view.findViewById(R.id.imageView1);

               // Defined inside activity_main.xml
                final LinearLayout mDrawer = (LinearLayout) view.findViewById(R.id.drawer); 

               // Defined inside activity_main.xml
                final DrawerLayout mDrawerLayout = (DrawerLayout)view.findViewById(R.id.drawer_layout); 

                img.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                     mDrawerLayout.openDrawer(mDrawer);

                    }
                });

                return view;    
                }

A null pointer is returned at the following statement:

在以下语句中返回空指针:

mDrawerLayout.openDrawer(mDrawer);

mDrawerLayout.openDrawer(mDrawer);

回答by Steven Trigg

These lines are referencing the wrong view:

这些行引用了错误的视图:

final LinearLayout mDrawer = (LinearLayout) view.findViewById(R.id.drawer); 
final DrawerLayout mDrawerLayout = (DrawerLayout)view.findViewById(R.id.drawer_layout);

They are looking for the drawer in 'R.layout.home' because that's what you inflate and set to the view variable, findViewById() returning null because it's not there.

他们正在“R.layout.home”中寻找抽屉,因为那是您膨胀并设置为视图变量的内容, findViewById() 返回 null 因为它不存在。

If this view is a child of activity_main then you should be able to change these to:

如果此视图是 activity_main 的子视图,那么您应该能够将它们更改为:

final LinearLayout mDrawer = (LinearLayout) getActivity().findViewById(R.id.drawer); 
final DrawerLayout mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);

Additionally, I'd make these class members and not final.

此外,我会让这些班级成员成为最终成员。

回答by Maksudul Hasan Raju

It works on Button click

它适用于按钮点击

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            drawer.openDrawer(GravityCompat.START);

        }
    });

It works on Image click

它适用于图像点击

mImageview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            drawer.openDrawer(GravityCompat.START);

        }
    });

回答by Hossin Asaadi

You need to use it like this in your activity:

您需要在您的活动中像这样使用它:

final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
imageViewMenu = (ImageView)  findViewById(R.id.imageViewMenu);
imageViewMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mDrawerLayout.openDrawer(Gravity.START);
    }
});