【问题标题】:Androidx customize navigationItem selected actionAndroidx自定义navigationItem选择动作
【发布时间】:2020-07-27 01:41:30
【问题描述】:

我已经使用 android studio 模板(导航抽屉活动)创建了一个导航抽屉活动。现在在 androidx & android studio 3.5.3 他们已经改变了导航抽屉活动的实现。在java类中的实现看起来像

DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

onCreateOptionsMenu 采用ContextMenuViewContextMenu.ContextMenuInfo 而不仅仅是Menu

@Override
    public void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.nav_drawer_main, menu);
    }

但是没有onOptionsItemSelectedonContextItemSelected 方法。所以我的问题是如果我想自定义抽屉项点击事件,我该怎么做?假设我想将 intentExtra 传递给片段,或者我必须检查特定项目点击的变量值。

提前致谢。

【问题讨论】:

    标签: android android-studio android-fragments navigation-drawer


    【解决方案1】:

    我已经完成了 addOnDestinationChangedListener 的工作。在此方法中,您可以切换 id,您在 AppBarConfiguration.Builder 方法中给出。在我的实现中,我隐藏或显示基于点击事件的布局。

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
       @Override
       public void onDestinationChanged(@NonNull NavController controller,
                                                 @NonNull NavDestination destination, @Nullable Bundle arguments) {
            if (destination.getId() == R.id.nav_home) {
                //item with id nav_home found do your work
                main_activity_layout.setVisibility(View.VISIBLE);
                Toast.makeText(getApplicationContext(), "Nav Home Pressed!", Toast.LENGTH_SHORT).show();
             } else {
                main_activity_layout.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(), "onDestChangeElse!", Toast.LENGTH_SHORT).show();
             }
         }
      });
    

    【讨论】:

      【解决方案2】:

      AndroidX Drawer Navigation 实现和以前一样简单

          @Override
          protected void onCreate(Bundle savedInstanceState) {
              ...
              ...
              drawerLayout = findViewById(R.id.drawer_layout);
              navigationView = findViewById(R.id.nav_view);
              setupNavigation();
          }
          private void setupNavigation() {
              // link Toggle button with Drawer
              final ActionBar actionBar = getSupportActionBar();
              if (actionBar != null) {
                  actionBar.setDisplayHomeAsUpEnabled(true);
                  drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
                  {
      
                      public void onDrawerClosed(View view)
                      {
                          supportInvalidateOptionsMenu();
                          //drawerOpened = false;
                      }
      
                      public void onDrawerOpened(View drawerView)
                      {
                          supportInvalidateOptionsMenu();
                          //drawerOpened = true;
                      }
                  };
                  drawerToggle.setDrawerIndicatorEnabled(true);
                  drawerLayout.setDrawerListener(drawerToggle);
                  drawerToggle.syncState();
              }
      
              // Create Menu for NavigationView
              Menu menu = navigationView.getMenu();
              menu.add(category.getName());
              menu.getItem(0).setIcon(category.getIcon());
              menu.getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                      @Override
                      public boolean onMenuItemClick(MenuItem menuItem) {
                          drawerLayout.closeDrawers();
      
                          // TODO; Here's your sexy looking codes ;P must return false if you don't know what `true` do
      
                          return false;
                      }
                  });
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-29
        • 1970-01-01
        • 2015-07-16
        • 2021-10-09
        • 2012-01-20
        • 1970-01-01
        相关资源
        最近更新 更多