工具栏中的选项卡式活动和菜单

时间:2018-01-04 18:18:37

标签: java android android-fragments menu

我有一个带有3个不同标签的Tabbed Activity。 我也有一个工具栏,在这个工具栏中我有1个静态菜单对象和1个动态对象。

我将静态对象(蓝牙连接)放入MainActivity中,其中包含加载到容器中的Fragment,并将动态按钮放入需要该按钮的Fragment中。

问题是,如果我将菜单方法声明为MainActivity,声明为片段的方法是无用的,当我按下图标时没有任何事情发生.... 但是,如果我将管理菜单的方法删除到MainActivity中,那么将按钮分割成片段...

围绕这件事有一些方法吗? 或者唯一的方法是重复每个片段静态按钮和每个片段添加其他片段不需要的特定按钮?

MainActivity菜单方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_bluetooth:

            if (mBluetoothService == null) {
                Log.d(TAG, "menu Bluetooth button");

                setupComunication();
            } else {

                mBluetoothService = null;

                setupComunication();
            }
            // Launch the DeviceListActivity to see devices and do scan
            Intent serverIntent = new Intent(this, DeviceListActivity.class);
            startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
            //findDevice();
            return true;

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

片段菜单方法:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.menu_dashboard, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_commands:
            // User chose the "Settings" item, show the app settings UI...
            Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
            startActivityForResult(settingsIntent, RESULT_SETTINGS);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

Fragment中覆盖这两种方法,将menu_dashboard的项目添加到menu_main,并默认将menuItem的可见性设置为false:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.action_commands).setVisible(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_commands:
            // User chose the "Settings" item, show the app settings UI...
            Intent settingsIntent = new Intent(getActivity(),     SettingsActivity.class);
            startActivityForResult(settingsIntent, RESULT_SETTINGS);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}