Overriding onOptionsItemSelected from SherlockFragmentActivity

落爺英雄遲暮 提交于 2019-12-20 17:05:07

问题


Yesterday, I found a great library that allowed me to have a "facebook menu" with a button on the top left of an action bar which, when pressed, would slide in a menu of items from the left.

The problem is that I wish to make use of the ActionBarSherlock library as well to make sure that my application is backwards compatible with the action bar. When using the library I, among other things, need to override onOptionsItemSelected as such :

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    int id = item.getItemId();

    if (id == android.R.id.home) {

        rbmView.toggleMenu();

        return true;

    } else {
        return super.onOptionsItemSelected(item);
    }
}

Now I went into the library and saw that the developer had made onOptionsItemSelected final. I removed it and tried overriding it again only to find that the product was that whenever I press the button nothing happens. Nothing at all.

Any idea on how I would go about using the darvds_ribbonmenu library along with actionbarsherlock?


回答1:


Turns out that when using ABS you will need to specify the namespace of MenuItem to make sure that you're overriding the correct method. My solution was as follows :

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) 
{
    int id = item.getItemId();
    Log.d("item ID : ", "onOptionsItemSelected Item ID" + id);
    if (id == android.R.id.home) {
        rbmView.toggleMenu();

        return true;

    } else {
        return super.onOptionsItemSelected(item);
    }
}



回答2:


Change import android.view.MenuItem; to import com.actionbarsherlock.view.MenuItem;. Otherwise, you're just using an entirely different MenuItem than the one you're importing.



来源:https://stackoverflow.com/questions/10909358/overriding-onoptionsitemselected-from-sherlockfragmentactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!