【问题标题】:How to set long click listener on items in BottomNavigationView?如何在BottomNavigationView中的项目上设置长点击监听器?
【发布时间】:2021-06-10 17:26:21
【问题描述】:

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

我试过这个答案 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;
        }
    });

}

【问题讨论】:

    标签: java android bottomnavigationview


    【解决方案1】:

    有两种方法可以解决这个问题。

    1. 通过 findViewById(int id);

      bottomNavigationView.findViewById(R.id.action_home).setOnLongClickListener(new View.OnLongClickListener() { @覆盖 公共布尔 onLongClick(查看 v){ 返回真; } });

    2. 通过“菜单视图”;

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

      }

    【讨论】:

      【解决方案2】:

      解决办法如下:

      @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 中则不行。如果有人知道原因,请告诉我。

      【讨论】:

        【解决方案3】:

        下面的 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】:
          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
              }
          }
          

          【讨论】:

            【解决方案5】:

            getView() 中的每个项目上设置setLongClickable(true);,这样就变成了,

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

            【讨论】:

            • 这对我不起作用。我在添加侦听器的地方发布了代码的 sn-p。感谢您的帮助。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-27
            • 1970-01-01
            • 1970-01-01
            • 2017-12-21
            • 1970-01-01
            相关资源
            最近更新 更多