如何在MenuItem(在NavigationView上)上设置长按侦听器? [英] How to set a long click listener on a MenuItem (on a NavigationView)?
问题描述
如何在MenuItem
上设置长按监听器?
How can I set a long click listener on a MenuItem
?
我尝试了此答案,但是该方法对我而言并不存在.有解决方案吗?
I tried this answer, but the method doesn't exist for me. Any solutions?
代码:
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,而是要在没有自定义View的情况下长按监听器来收听整个MenuItem
.
I don't want to set a custom ActionView, I want the long click listener to the whole MenuItem
, without a custom View.
推荐答案
多种方式之一(假设我们使用工具栏)-此示例应为您提供如何实现长按工具栏按钮的想法:
one of many ways (assuming we use Toolbar) - this example should give you the idea how to implement long click on toolbar button :
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):
- 创建菜单/例如通过充气
- 获取菜单项
- 创建操作视图,例如ImageButton
- 在动作视图上设置长按监听器
- 在菜单项上设置操作视图
上面我设置了一个动作视图,然后从菜单项中将其取回并设置了侦听器(顺序也可以是这样):
above i set an action view then i get it back from menu item and set listener (the order is no matter it could be also in such way):
ImageButton imageButton = new ImageButton(Context);
imageButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
item.setActionView(imageButton);
ps.您还可以将xml中的图片视图设置为菜单项上的属性:
ps. you can also set image view in xml as attribute on menu item:
<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小部件)-动作视图可以是简单视图,也可以是复杂视图类型
-- no you are setting an action view on single element in this case you change a default view (to ImageButton widget) for an menu item - action view could be simple or complex view type
但是,如果您不想更改视图,而是保留默认视图怎么办? – SuperThomasLab
示例(这是许多使用布局树观察器/设置布局更改侦听器的方法):
example (this is one way of many by using a layout tree observer / by setting a layout change listener):
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
是的-但我知道在您的情况下它不起作用的原因??? -在我的示例中,我们检查是否布置了视图项目,而不是如果布置了菜单视图,则检查该更改,然后检查菜单是否包含项目
YES IT DOES - but i know the reason why it't is not working in your case ??? - in my example we check if view item is laid out instead of that change if menu view is laid out and then check if menu contain item
这是供您学习的工作代码:
here is working code for you to study:
https://github.com/c3ph3us/LongClickOnMenuItem
回复其他评论:
@ SuperThomasLab对于我来说,为什么setOnLongClickListener(...)方法不可用对我来说并不明显. –侯赛因·塞菲(Hossein Seifi)
@HosseinSeifi-看看android.view.MenuItem接口-它没有提供这样的方法-因此对于优秀的程序员来说,这应该很明显:)为什么他无法实现实现类方法.
@HosseinSeifi - look at android.view.MenuItem interface - it doesn't provide a such method - so for good programmer this should be obvious :) why he can't reach implementing class method.
这篇关于如何在MenuItem(在NavigationView上)上设置长按侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!