onNavigationItemSelected

onNavigationItemSelected

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

导航抽屉

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

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

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

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


我也有一个自定义通知对话框。当出现“未找到数据”通知对话框时,我有“下载”按钮,它将打开抽屉对话框(平铺层)。

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

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

现在..如何在单击“下载”按钮时以编程方式触发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();
        }
    });

最佳答案

您可以使用此技巧

变化不大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%工作...试试吧..祝您好运。

10-08 07:23