gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:03:27 25 4
gpt4 key购买 nike

我有一个包含 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;
}

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

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

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

最佳答案

你可以使用这个技巧

小改动 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% 有效...试试吧..祝您好运。

关于android - 如何在 Android 中以编程方式触发 onNavigationItemSelected()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com