【问题标题】:Programatically fire action bar menu item in Android在 Android 中以编程方式触发操作栏菜单项
【发布时间】:2014-09-24 11:09:50
【问题描述】:

我的操作栏上有菜单项。当我点击刷新图标时,我有显示进度条的方法。

我想在加载此活动时进行。因此,我尝试以编程方式调用刷新图标项单击:

onOptionsItemSelected(menu.findItem(R.id.action_Refresh)); 

我在创建菜单后调用上述内容。

但这会在加载我的数据时出现空指针异常。如果我点击刷新按钮,它工作正常,但如果我以编程方式调用它,我会收到错误。

这是我所拥有的:

 @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    this.optionsMenu = menu;
    getMenuInflater().inflate(R.menu.main, menu);

    onOptionsItemSelected(menu.findItem(R.id.action_Refresh));

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.action_about:
        aboutapp();
        return true;

    case R.id.action_Refresh:
        Log.e("REfressh","Clicked");
        Mapsthree.refreshValue = 0;
        timer = new Timer();
        timerMethod();
        setRefreshActionButtonState(true);
        displayView(1);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

private void timerMethod()
{
    timer.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() 
        {
            updateProgressBar();
        }

    }, 0, 800);
}

private void updateProgressBar() 
{
    runOnUiThread(new Runnable() 
    {
        public void run() 
        {
            if (Maps.refreshValue == 1)
            {
                setRefreshActionButtonState(false);
                timer.purge();
                timer.cancel();
            }
        }
    });
}

public void setRefreshActionButtonState(final boolean refreshing) 
{
    if (optionsMenu != null) 
    {
        final MenuItem refreshItem = optionsMenu.findItem(R.id.action_Refresh);
        if (refreshItem != null) 
        {
            if (refreshing) 
            {
                refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
            } 
            else 
            {
                refreshItem.setActionView(null);
            }
        }
    }
}

是否可以通过编程方式调用菜单项?如果有怎么办?

谢谢!

【问题讨论】:

  • 将您为刷新操作执行的代码包装在一个新方法中,然后为操作项调用它以及在活动中需要调用它的任何位置。

标签: android android-actionbar selection android-actionmode actionmode


【解决方案1】:

只要findViewById(R.id.action_Refresh).callOnClick();

findViewById(R.id.action_Refresh).performClick();

【讨论】:

  • 这需要 API 15...我支持 API 14 的应用程序。
  • 好吧,改成 performClick() 吧.. 很简单。
  • 好吧,我可以向你保证,这是可行的。我刚试过。如果您调用它,它将单击 R.id.action_Refresh 视图。除非它被禁用或其他原因,否则onOptionsItemSelected 将被解雇。
  • 哦...您只是想单击一个尚未创建的菜单项吗?菜单将只能在 onCreateOptionsMenu 之后点击。
  • 我在里面尝试这个:onCreateOptionsMenu,在创建我刚刚调用的菜单后:findViewById(R.id.action_Refresh).performClick();
【解决方案2】:

你为什么用onCreateOptionsMenu打电话?

您可以根据需要在onCreateonResume方法中编写加载代码:

@Override
protected void onCreate(Bundle arg0) {

    super.onCreate(arg0);

    //whatever you are doing

    //now code for refresh

    Log.e("REfressh","First time");
    Mapsthree.refreshValue = 0;
    timer = new Timer();
    timerMethod();
    setRefreshActionButtonState(true);
    displayView(1);
}

【讨论】:

  • 我不能,我只是在单击刷新图标时才将其更改为进度条。
  • 那么,你想让它在第一次加载时也变成进度条吗?
  • 没错——因为我从云端加载的东西很少,而且需要时间。
  • 使用了你的逻辑,只做了很少的改动。工作正常!非常感谢!
【解决方案3】:

你可以试试这个:
在此处获取 Activity 中 Menu 类 opMenu 的引用:

Menu opMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    opMenu= menu;
    getMenuInflater().inflate(R.menu.main, menu);

    onOptionsItemSelected(opMenu.findItem(R.id.action_Refresh));

    return super.onCreateOptionsMenu(menu);;
}  

使用opMenu.findItem()触发点击

【讨论】:

  • @TheDevMan 只需用我的代码替换你的代码并检查,你使用了menu.findItem(),我得到了正确的结果。
  • 我尝试了一种可行的方法,但我使用的是 micro.pravi 逻辑。与该方法完美配合。谢谢!感谢您的逻辑 +1!
【解决方案4】:
// check is assignable to menu item implementation
if(menuItem !=null 
    && MenuItemImpl.class.isAssignableFrom(menuItem.getClass())){
  // if so cast 
  MenuItemImpl impl = (MenuItemImpl)menuItem;
  // invoke listener 
  impl.invoke();
}

等于实现;

public boolean invoke() {
    if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
        return true;
    }

    if (mIntent != null) {
        mContext.startActivity(mIntent);
        return true;
    }
    return false;
}

你总是可以使用反射:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多