如何在Android中以编程方式触发onNavigationItemSelected()?

时间:2017-09-28 04:00:03

标签: android android-navigation-drawer

我有一个带有3个菜单项的导航抽屉。在我的MainActivity.java中,我设置onNavigationItemSelected()侦听器来处理单击的导航项。单击时,将显示自定义抽屉对话框

导航抽屉

Navigation drawer

自定义抽屉对话框(平铺图层)

Custom drawer dialog (Tiled Layers)

MainActivity.java

public boolean onNavigationItemSelected(MenuItem item)
{
    // Create custom drawer dialog
    drawerDialog = new Dialog(MainActivity.this);
    drawerDialog.setContentView(R.layout.dialog_drawer);

    // Handle navigation view item clicks here.
    int selectedItemId = item.getItemId();
    if (selectedItemId == R.id.layerMenu)
    {
        ...
    }
    else if (selectedItemId == R.id.basemapMenu)
    {
        ...
    }
    else if (selectedItemId == R.id.tiledLayerMenu)
    {
        // Setup drawer dialog (Tiled Layers)        
    }

    // Show drawer dialog
    drawerDialog.show();

    ...
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

我还有一个自定义通知对话框。当出现No Data Found通知对话框时,我有下载按钮,它将打开抽屉对话框(Tiled Layers)。

自定义通知对话框(未找到数据)

Custom notice dialog (No Data Found)

现在.. 如何在编辑上触发onNavigationItemSelected() 点击下载按钮?这就是我尝试但未成功的原因:

    downloadButton.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            // Trying to click Tiled Layers navigation item here                
            // 1
            //NavigationView navigationView = (NavigationView) findViewById(R.id.mainNavView);
            //navigationView.getMenu().getChildAt(2).performClick();            
            // 2
            //((NavigationMenuItemView) findViewById(R.id.tiledLayerMenu)).callOnClick();            
            // 3
            //onNavigationItemSelected(navigationView.getMenu().getItem(2))); //

            // Close dialog
            noticeDialog.dismiss();
        }
    });

请帮帮我。谢谢。

更新

好的..我只是简单地做一些像LQ Gioan建议的东西,它有效。我将onNavigationItemSelected()中的所有长代码移动到新函数showDrawerDialog并传递了项目ID。在按钮单击中,我也这样做。无需找出MenuItem,只传递商品ID。谢谢!

MainActivity.java

public boolean onNavigationItemSelected(MenuItem item)
{
    int selectedItemId = item.getItemId();
    showDrawerDialog(selectedItemId);

    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

public void showDrawerDialog(final int selectedItemId)
{        
    // Create custom drawer dialog
    drawerDialog = new Dialog(MainActivity.this);
    drawerDialog.setContentView(R.layout.dialog_drawer);

    // Handle navigation view item clicks here.
    //int selectedItemId = item.getItemId();
    if (selectedItemId == R.id.layerMenu)
    {
        ...
    }
    else if (selectedItemId == R.id.tiledLayerMenu)
    {
        // Setup drawer dialog (Tiled Layers)        
    }

    // Show drawer dialog
    drawerDialog.show();
}

点击下载按钮,

    downloadButton.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            // Trying to click Tiled Layers navigation item here                
            showDialogDrawer(R.id.tiledLayerMenu);

            // Close dialog
            noticeDialog.dismiss();
        }
    });

3 个答案:

答案 0 :(得分:1)

  
    

非常简单,一线回答!

  
onNavigationItemSelected(navigationView.getMenu().findItem(R.id.my_id));

答案 1 :(得分:0)

您可以使用此技巧

变化不大onNavigationItemSelected(MenuItem item)

首先声明一个int类变量

//clicked id holder
int clicked = 0;

现在在onNavigationItemSelected(MenuItem item)

中使用它
public boolean onNavigationItemSelected(MenuItem item)
{

  int selectedItemId = item.getItemId();
  if (selectedItemId == R.id.layerMenu || clicked == R.id.layerMenu)
  {
    //your  action code
  }
  else if (selectedItemId == R.id.tiledLayerMenu || clicked == R.id.tiledLayerMenu)
  {
    // your  action code       
  }
  //clear clicked value
  clicked = 0;

  mDrawerLayout.closeDrawer(GravityCompat.START);
  return true;
}

点击按钮编辑

downloadButton.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        //setting id to clicked variable 
        clicked  = R.id.m_layerMenu;
        //fire the  navigation selection
       onNavigationItemSelected(navigationView.getMenu().getItem(0));
    }
});

我这样做并为你测试......它100%有效......试试吧..祝你好运。

答案 2 :(得分:0)

您可以在 android 中的任何菜单上尝试此方法。

yourMenu.performIdentifierAction(menuItemId, flags)

根据标志,您可以选择是否关闭菜单主机。