工具栏在更多按钮的菜单中加倍选项

Toolbar doubles option in menu in more button

我的 Activity 中有一个 Toolbar,然后我在我的片段中膨胀了一个菜单,问题是它甚至在 more 按钮中添加了相同的选项,这应该'甚至不存在,layoutManager 和 adapterTables 上的功能甚至都没有执行(但图标已更改)

Activity:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:navigationIconTint="#737373"
        app:navigationIcon="@drawable/ic_baseline_close_white"
        app:title="Tavolo"
        app:subtitle="Seleziona tavolo"
        app:subtitleTextColor="#737373"
        app:titleTextColor="#737373" />


</com.google.android.material.appbar.AppBarLayout>

菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/tableMap"
        android:title="@string/mappa_tavoli"
        android:contentDescription="@string/mappa_tavoli"
        android:icon="@drawable/ic_table_restaurant_24"
        app:showAsAction="ifRoom" />


    <item
        android:id="@+id/changeLayout"
        android:title="@string/modifica_layout"
        android:contentDescription="@string/modifica_layout"
        android:icon="@drawable/ic_grid_view"
        app:showAsAction="ifRoom" />
</menu>

片段:

public class TablesFragment extends Fragment {
    @Nullable
    private Listener mListener;

    GridLayoutManager layoutManager;
    AdapterTables adapterTables;

    public void setListener(@Nullable Listener mListener) {
        this.mListener = mListener;
    }

    public interface Listener {
        void onClickTable(String table);
    }


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

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tables, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initUI(view);
    }

    private void initUI(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.recyclerViewTables);
        layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();

        ArrayList<Tables> listTables = new ArrayList<>();
        listTables.add(new Tables("1", "F", "", "50m", ""));
        listTables.add(new Tables("2", "O", "", "30m", "2,50"));
        listTables.add(new Tables("3", "F", "", "30m", ""));
        listTables.add(new Tables("4", "F", "Veranda", "50m", ""));
        listTables.add(new Tables("5", "O", "", "", "5,00"));
        listTables.add(new Tables("6", "F", "", "50m", ""));
        listTables.add(new Tables("7", "O", "", "30m", "9,00"));
        adapterTables = new AdapterTables(layoutManager, listTables);
        adapterTables.setListener(idTable -> mListener.onClickTable(listTables.get(idTable).getId()));
        recyclerView.setAdapter(adapterTables);

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.table, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.changeLayout) {
            if (layoutManager.getSpanCount() == 1) {
                item.setIcon(R.drawable.ic_format_list_bulleted);
                layoutManager.setSpanCount(4);
            } else {
                item.setIcon(R.drawable.ic_grid_view);
                layoutManager.setSpanCount(1);
            }
            adapterTables.notifyItemRangeChanged(0, adapterTables.getItemCount());
            return true;
        }else if (item.getItemId() == R.id.tableMap) {
            getParentFragmentManager().beginTransaction()
                    .setReorderingAllowed(true)
                    .replace(R.id.fragment_container_view, TablesMapFragment.class, null)
                    .commit();
            return true;
        }
        return false;
    }
}

设备上的工具栏如下所示:

编辑:

当我更改片段时,菜单 returns 必须工作:

正如您所看到的,只有在我按下 table 选项后菜单才开始工作,该选项执行片段替换,然后通过按下列表选项将片段替换为之前的片段并且菜单设置正确, 但不是在第一次开始时..

问题是在 activity 开始时我正在调用:

   getSupportFragmentManager().beginTransaction()
            .setReorderingAllowed(true)
            .add(R.id.fragment_container_view, TablesFragment.class, null)
            .commit();

通过将 .add 更改为 .replace 它开始按预期工作。