【问题标题】:Tabbed Activity and Menu in Toolbar工具栏中的选项卡式活动和菜单
【发布时间】:2018-01-05 13:03:32
【问题描述】:

我有一个带有 3 个带有片段的不同选项卡的选项卡式活动。 我也有一个工具栏,在这个工具栏中我有 1 个静态菜单对象和 1 个动态对象。

我将静态对象(蓝牙连接)放入包含加载到容器中的 Fragment 的 MainActivity 中,并将动态按钮放入需要该按钮的 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);
    }
}

【问题讨论】:

    标签: java android android-fragments menu


    【解决方案1】:

    覆盖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);
        }
    }
    

    【讨论】:

    • 您的意思是将 menu_dashboard 的可见性默认设置为 false ?所以这个 item action_commands 只对 Fragment 可见?
    • 否,将两个菜单项合并到单个menu_main.xml 中,并将action_commands 的可见性默认设置为false。
    • 好的,我会尽力让您知道!...是唯一可行的方法吗?
    • 看看github.com/jaisonfdo/WhatsAppViewPager。自己没有尝试过,但他在FragmentonOptionsItemSelected() 里面膨胀了menu Activity
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2012-03-08
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多