Android - Fragment 修改 MenuItem

摘要:Android - Fragment 修改 MenuItem

在Fragment 的類別裡


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.refresh, menu);
        MenuItem refreshItem = menu.findItem(R.id.action_refresh);
        refreshItem.setOnMenuItemClickListener(mOnMenuItemClickListener);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.menu_id:
            //TODO Something
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}