开发者

How can I capture a long press on a Menu Item?

开发者 https://www.devze.com 2023-04-10 03:37 出处:网络
I have a typical menu and I\'m wanting to set a onLongClickListener for one of the items. In other words, I want this item to perform it\'s normal onOptionsItemSelected function, as well as, a long pr

I have a typical menu and I'm wanting to set a onLongClickListener for one of the items. In other words, I want this item to perform it's normal onOptionsItemSelected function, as well as, a long press function.

    MenuItem item;
    item = menu.findItem(android.R.id.home);

item.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Context context = getApplicationContext();
            CharSequence text = "Long Press";
            int duration = Toast.LENGTH_SHORT;

            Toast toast开发者_StackOverflow社区 = Toast.makeText(context, text, duration);
            toast.show();
            return true;
        }

    });


I was able to do this by simply using setActionView on menuItem. You can follow this procedure if it helps.

    for(int i = 0; i < menu.size(); i++){
        View v = new View(this);
        v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //Your longclick listener callback logic goes here
                return false;
            }

        });
        menu.getItem(i).setActionView(v);
    }


Use the findItem method on Menu to get your views, and set your long click listener on each view.


This approach is not right, it disturbs entire flow, but here you go:

    private interface OnMenuItemLongClickListener{
        boolean onMenuItemLongClik(MenuItem m);
    }
    private void getMenuItemsView(Activity a, final Menu m, final OnMenuItemLongClickListener listener) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        View homeButton = a.findViewById(android.R.id.home);
        ViewParent parentOfHome = homeButton.getParent().getParent(); //ActionBarView is parent of home ImageView, see layout file in sources

        if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
            parentOfHome = parentOfHome.getParent(); 
            Class absAbv = parentOfHome.getClass().getSuperclass(); //ActionBarView -> AbsActionBarView class
            Field actionMenuPresenterField = absAbv.getDeclaredField("mActionMenuPresenter");
            actionMenuPresenterField.setAccessible(true);
            Object actionMenuPresenter = actionMenuPresenterField.get(parentOfHome);
            Field actionMenuViewField = actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenuView");
            actionMenuViewField.setAccessible(true);
            Object actionMenuView = actionMenuViewField.get(actionMenuPresenter);
            Field childrenField= actionMenuView.getClass().getSuperclass().getSuperclass().getDeclaredField("mChildren");
            childrenField.setAccessible(true);
            Field menuField =actionMenuPresenter.getClass().getSuperclass().getDeclaredField("mMenu");
            menuField.setAccessible(true);
            Object menu = menuField.get(actionMenuPresenter);
            Object[] menuItemsAsViews = (Object[])childrenField.get(actionMenuView);
            View.OnLongClickListener longListener = new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    return listener.onMenuItemLongClik(m.findItem(v.getId()));
                }
            };
            for(Object menuView:menuItemsAsViews ){
                View v = (View)menuView;
                v.setOnLongClickListener(longListener);
            }


    }
 }

Usage:

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
        new Handler().postDelayed(new Runnable(){

            @Override
            public void run() {
            getMenuItemsViews(MainActivity.this, menu);
            }}, 100); //must be called after the menu views are generated, otherwise NPE.
    return super.onPrepareOptionsMenu(menu);
}

MenuItem's in the overflow WILL NOT be considered.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号