2016-06-18 117 views
10

如何在MenuItem上设置长按听众?如何在MenuItem上(在NavigationView上)设置长按监听器?

我试过this answer,但该方法对我来说不存在。任何解决方案

代码:

Menu menu = navigationView.getMenu(); 
MenuItem menuItem = menu.findItem(R.id.menu_item); 

// TODO set a long click listener on the menuItem. 
menuItem.setOnLongClickListener(...); // Method does not exist, any other solutions? 

编辑:我不想设置自定义的ActionView,我想从长远点击收听到整个MenuItem,没有自定义视图。

+0

显示您的代码,请 – BooDoo

+0

@BooDoo没有什么可显示的,但我添加了一些代码。 –

+0

http://stackoverflow.com/q/23151117/6152781 – BooDoo

回答

5

的许多方面(假设我们使用的工具栏)一个 - 这个例子应该给你的想法如何实现工具栏上的按钮,长按:

class MyActivity extends Activity {  

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     /** get menu inflater */ 
     MenuInflater menuInflater = getMenuInflater(); 
     /** Inflate the menu 
     * this adds items to the action bar if it is present. */ 
     menuInflater.inflate(R.menu.menu_home, menu); 
     /** find interesting item */ 
     MenuItem item = menu.findItem(R.id.itemId); 
     /** set action view */ 
     item.setActionView(new ImageButton(this)); // this is a Context.class object 
     /** set listener on action view */ 
     item.getActionView().setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       return false; 
      } 
     }); 

     return super.onCreateOptionsMenu(menu); 
    } 
} 

的方法onCreateOptionsMenu - 或任何其他方法时,你可以获得菜单项参考(省略步骤1-2)

  1. 由充气
  2. 获取菜单项
  3. 创建菜单/例如
  4. 创建例如ImageButton的动作视图
  5. 行动视图
  6. 设置长按监听
  7. 上的菜单项目的动作视图

上面我设置一个动作视图,然后我把它找回来,从设置菜单项,并设置听众(顺序无论它也可以这样):

ImageButton imageButton = new ImageButton(Context); 
imageButton.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       return false; 
      } 
     }); 
item.setActionView(imageButton); 

ps。您还可以设置图像视图中的XML作为菜单项属性:

<item 
    ... 
    android:actionViewClass="android.widget.ImageButton" 
/> 

then you can get the action view by cast 

    View menuItemActionView = menu.findItem(R.id.itemId).getActionView(); 
    if(menuItemActionView != null 
      && ImageButton.class.isAssignableFrom(menuItemActionView.getCLass())) { 
     ImageButton imageButton = (ImageButton) menuItemActionView; 
    } 

但你设置长按听众的动作仅查看,而不是整个项目。 - SuperThomasLab

- 没有你在这种情况下,设置单个元素上的动作视图,您更改的菜单项的默认视图(到的ImageButton控件) - 动作视图可以是简单或复杂的视图类型

但是,如果你不想改变视图,但保持默认视图? - SuperThomasLab

例如(这是通过使用一个布局树观察者 /通过设置布局改变听众的许多单程):

private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      return false; 
     } 
    }; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     /** get menu inflater */ 
     MenuInflater menuInflater = getMenuInflater(); 
     /** Inflate the menu 
     * this adds items to the action bar if it is present. */ 
     menuInflater.inflate(R.menu.menu_home, menu); 
     /** geta menu item using findItem(int itemid) */ 
     MenuItem item = menu.findItem(R.id.itemLogOut); 
     /** check if we have item */ 
     if(item!=null) { 
      /** try get its action view */ 
      View actionView = item.getActionView(); 
      /** check if action view is already set? */ 
      if(actionView==null) { 
       /** get item id to comparte later in observer listener*/ 
       final int itemId = item.getItemId(); 
       /** if not set on top most window an layout changes listener */ 
       getWindow().getDecorView() 
          .addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
        @Override 
        public void onLayoutChange(View v, int left, int top, int right, 
         int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
          /** try get view by id we have stored few line up */ 
          View viewById = v.getRootView().findViewById(itemId); 
          /** check if we have any result */ 
          if(viewById!=null) { 
           /** set our listener */      
           viewById.setOnLongClickListener(onLongClickListener); 
           /** remove layout observer listener */ 
           v.removeOnLayoutChangeListener(this); 
          } 
        } 
       }); 
      } else { 
       /** if set we can add our on long click listener */ 
       actionView.setOnLongClickListener(onLongClickListener); 
      } 
     } 
    } 

我试图OnLayoutChangeListener ,但它仍然无法改变。 - SuperThomasLab

是它 - 但我知道为什么it't是不是在你的情况下工作的原因??? - 在我的例子中,我们检查,如果视图项目布局而不是改变,如果菜单视图布局,然后检查是否菜单包含项目

这里工作代码为你学习:

https://github.com/c3ph3us/LongClickOnMenuItem

在回答其他评论:

@ SuperThomasLab这不是对我来说显而易见的,为什么setOnLongClickListener(...)方法不适用于你。 - 侯赛因Seifi

@HosseinSeifi - 看android.view.MenuItem界面 - 它并没有提供这样的方法 - 所以对于优秀的程序员,这应该是显而易见的:)为什么他不能达到实现类方法。

+0

谢谢你的回答。但是,长按听众在哪里?我该如何将它设置为'MenuItem'? –

+0

@SuperThomasLab - 请参阅我编辑的答案 – ceph3us

+0

但是,您只将长单击侦听器设置为操作视图,而不是整个项目。 –

0

您可以通过这样实现:

action_menu.xml 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:support="http://schemas.android.com/apk/res-auto" > 

<item 
    android:id="@+id/item1" 
    support:showAsAction="always"> 
</item> 

custom_action_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:paddingRight="5dp" > 

<ImageButton 
    android:id="@+id/customActionItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:background="@drawable/abc_item_background_holo_dark" 
    android:src="@drawable/bulb_icon" /> 

</RelativeLayout> 

和菜单吹气代码如下:

public boolean onCreateOptionsMenu(Menu menu) { 
    // TODO Auto-generated method stub 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_menu, menu);  

    final MenuItem item1= menu.findItem(R.id.item1); 
    MenuItemCompat.setActionView(item1, R.layout.custom_action_view); 
    View vItem1= MenuItemCompat.getActionView(item1); 

    final ImageButton customActionItem= (ImageButton) vItem1.findViewById(R.id.customActionItem); 
    customActionItem.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // do something here 
     } 
    }); 

    return super.onCreateOptionsMenu(menu); 
} 
+0

这与其他答案一样。我不想要一个自定义的ActionView,我想将这个长的点击监听器设置为普通的项目,而不需要自定义视图。 –

相关问题