如标题所述,我有一个带有3个菜单项的BottomNavigationView,并希望为其分配一个长按监听器。
我在OnNavigationItemSelectedListener上设置了BottoNavigationView

我已经试过这个答案
here
但这对我没有用...

你们推荐其他方式吗?

这就是我所拥有的...常规点击有效

private void setUpNavigationListeners() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.fragment_container, new CalendarView()).commit();
    navigation.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    Fragment fragment;
                    switch (item.getItemId()) {
                        case R.id.navigation_courses:
                            subtitle.setText(R.string.courses);
                            fragment = new CalendarView();
                            break;
                        case R.id.navigation_assignments:
                            subtitle.setText(R.string.assignments);
                            fragment = new AssignmentView();
                            break;
                        case R.id.navigation_professors:
                            subtitle.setText(R.string.professors);
                            return false;
                        default:
                            return false;
                    }

                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.replace(R.id.fragment_container, fragment).commit();
                    return true;
                }

            });
    navigation.setItemIconTintList(null);

    Menu menu = navigation.getMenu();
    MenuItem courses = menu.findItem(R.id.navigation_courses);
    MenuItem assignments = menu.findItem(R.id.navigation_assignments);
    MenuItem prof = menu.findItem(R.id.navigation_professors);

    courses.setActionView(new ImageButton(this));
    courses.getActionView().setLongClickable(true);
    Log.d("Manage", "Is long clickable:" + courses.getActionView().isLongClickable());
    courses.getActionView().setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Log.d("Manage", "Long clicked");
            return true;
        }
    });

}

最佳答案

setLongClickable(true);中的每个项目上加上getView()即可,

item.getActionView().setLongClickable(true);
item.getActionView().setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });

07-24 09:49