简体   繁体   English

如何在充气视图上显示上下文菜单?

[英]How to show contextmenu on inflated view?

I want to display contextmenu for an inflated view. 我想为膨胀的视图显示上下文菜单。 Here is the code sample: 这是代码示例:

for grid_layout.xml: 对于grid_layout.xml:

<?xml version="1.0" encoding="utf-8"?>    
    <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:antialias="true" />

Now I am using it in my activiy class as: 现在我在我的活动课中使用它:

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {       
            menu.setHeaderTitle("Select action");       
            menu.add(0, 1, 0, "Action1");
            menu.add(0, 2, 0, "Action2");

            super.onCreateContextMenu(menu, v, menuInfo);
    }

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
    imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               registerForContextMenu(v);
                       openContextMenu(v);
            }

        });

This code runs without any error but the context menu does not show up when I click the imageView. 此代码运行时没有任何错误,但单击imageView时上下文菜单不显示。 Is there anything wrong with this code? 这段代码有什么问题吗?

I have found a workaround for this situation and solved my problem. 我找到了解决此问题的方法并解决了我的问题。 As I told that the same code works fine and displays the ContextMenu, if I define the ImageView in the XML which I set in setContentView() method. 正如我所说,相同的代码工作正常并显示ContextMenu,如果我在我在setContentView()方法中设置的XML中定义ImageView。 I simply used the object of that imageView to register the contextMenu and displayed the ContextMenu on click of inflated item. 我只是使用该imageView的对象来注册contextMenu并在点击膨胀项目时显示ContextMenu。 Here is the sample code: 以下是示例代码:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {       
            menu.setHeaderTitle("Select action");       
            menu.add(0, 1, 0, "Action1");
            menu.add(0, 2, 0, "Action2");

            super.onCreateContextMenu(menu, v, menuInfo);
    }

    ImageView imageViewInContext = (ImageView) findViewById(R.id.imageview_in_main_xml);
    registerForContextMenu(imageViewInContext);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
    imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   
                 openContextMenu(imageViewInContext);
            }

        });

Hope this will help someone! 希望这会对某人有所帮助!

Add in your code: 添加您的代码:

imageView.setFocusable(true);
imageView.setClickable(true);

after that Imageview will get clickEvent. 之后,Imageview将获得clickEvent。

Because you are creating an Anonymous Inner Class (by doing new View.OnClickListener ) you are no longer operating in the UI thread (your Activity class), so the context menu does not load when you want to registerForContextMenu and openContextMenu . 因为您正在创建一个匿名内部类(通过执行new View.OnClickListener ),所以您不再在UI线程(您的Activity类)中操作,因此当您想要registerForContextMenuopenContextMenu时,不会加载上下文菜单。 You can either use a Handler to send a message to the UI thread(your Activity class) to perform these actions, or try to reference your Activity class in your inner class. 您可以使用Handler将消息发送到UI线程(您的Activity类)来执行这些操作,或者尝试在内部类中引用您的Activity类。 Something like this: 像这样的东西:

activityClassName.this.registerForContextMenu(v);
activityClassName.this.openContextMenu(v);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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