动态处理android中的子菜单项
由于我动态创建子菜单项,因此显然 MenuItems 的索引只是动态的。这就是我面临的问题。
到目前为止,我已经成功在 onCreateOptionsMenu 函数内动态创建菜单项
SubMenu switchMenu = menu.addSubMenu("My Menu");
for(int i=0;i<myList.getListSize();i++){
switchMenu.add(FILE, NEW_MENU_ITEM+i, 0, myList.get(i).data);
}
现在,当在 onOptionsItemSelected 内选择菜单项时,问题就出现了 每当父
//get the the selected index
int selectedMenuIndex = item.getItemId();
// Pass it to a function in another activity
myList.myActivity.switch(selectedMenuIndex);
//Finishing the current activity and loads the previously selected
finish();
菜单加载并且我点击它来获取子菜单时,android会自动选择列表中的第一个子菜单,即索引= 0并立即触发上面的代码,并且它不会等待子菜单的其余部分加载并允许用户从我动态创建的子菜单项中进行选择。
As I am creating sub menu items dynamically , so its obvious that the MenuItems's index will be dynamic only. so here what i facing the problem.
so far i have successfully create the menu items dynamically inside onCreateOptionsMenu function
SubMenu switchMenu = menu.addSubMenu("My Menu");
for(int i=0;i<myList.getListSize();i++){
switchMenu.add(FILE, NEW_MENU_ITEM+i, 0, myList.get(i).data);
}
Now the problem comes when Menu Items are selected inside onOptionsItemSelected
//get the the selected index
int selectedMenuIndex = item.getItemId();
// Pass it to a function in another activity
myList.myActivity.switch(selectedMenuIndex);
//Finishing the current activity and loads the previously selected
finish();
Whenever the parent menu loads and i click on it to get the submenu , android automatically selects first submenu in the list i.e. index =0 and immediately fires the above code and it doesn't wait for rest of the submenu to load and allow the user to select from the submens item that i have created dynamically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 OnOptionsItemSelected 中执行此操作
Do this in your OnOptionsItemSelected