如何在BottomNavigationView中的项目上设置长按侦听器?

时间:2017-05-09 02:32:08

标签: java android bottomnavigationview

正如标题所说,我有一个BottomNavigationView包含3个菜单项,并希望为每个菜单项指定一个长按聆听者。
我在OnNavigationItemSelectedListener上设置了BottoNavigationView
我试过这个答案 here 但这对我来说没有用... ...
你们推荐的其他方式吗?
这就是我所拥有的......常规点击工作btw

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;
        }
    });

}

5 个答案:

答案 0 :(得分:0)

setLongClickable(true);中的每个项目设getView(),使其成为

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

答案 1 :(得分:0)

这是解决方案:

@Override
protected void onStart() {
    super.onStart();
    View view = findViewById(R.id.navigation_courses); // BottomNavigationView menu item id
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

就我而言,它在onStart中可以正常工作,而在onCreate中则不能。如果有人明白原因,请告诉我。

答案 2 :(得分:0)

有两种解决方法。

  1. 通过findViewById(int id);

    bottomNavigationView.findViewById(R.id.action_home).setOnLongClickListener(new View.OnLongClickListener(){ @Override public boolean onLongClick(View v){ 返回true; } });

  2. 通过'menuView';

    private void overrideOnMenuItemLongClickListener(BottomNavigationView bottomNavigationView){
    
    if(bottomNavigationView == null){
        return;
    }
    
    int count = bottomNavigationView.getChildCount();
    if(count <= 0){
        return;
    }
    
    ViewGroup menuView = (ViewGroup) bottomNavigationView.getChildAt(0);
    if(menuView == null){
        return;
    }
    
    int menuItemViewSize = menuView.getChildCount();
    if(menuItemViewSize <= 0){
        return;
    }
    
    for(int i=0; i < menuItemViewSize; i++){
        menuView.getChildAt(i).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
    }
    

    }

答案 3 :(得分:0)

下面的 JAVA 代码绝对没问题!!!

private void overrideOnMenuItemLongClickListener(BottomNavigationView bottomNavigationView) {
    if (bottomNavigationView != null && bottomNavigationView.getChildCount() > 0) {
        ViewGroup menuView = (ViewGroup) bottomNavigationView.getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                menuView.getChildAt(i).setOnLongClickListener(v -> true);
            }
        }
    }
}

答案 4 :(得分:0)

fun BottomNavigationView.setLonClkListner() {
    val position = 4 //item index in which u want to set LongClickListner
    val menuView = getChildAt(0) as ViewGroup
    menuView.getChildAt(position).setOnLongClickListener {
        findNavController().navigate(R.id.toSwitchAccount)
        true
    }
}