onCreateOptionsMenu 只调用视图寻呼机内的选定片段(项目),(不调用视图寻呼机的左右项目)

onCreateOptionsMenu called only for the selected fragment(item) inside view pager,( not called for left and right items of view pager)

有一个视图寻呼机和片段作为视图寻呼机项目。每个片段都有自己的工具栏。在使用 ArrayList 调用 set view pager adapter 时,onCreateOptionsMenu(onCreateOptionsMenu is inside fragment) 仅针对选定的(view pager 的当前选定项目)项目片段调用,而选定的 viewpager 项目(片段)的左右项目也被加载,但 onCreateOptionsMenu 是未调用,因此菜单项不会为这些片段膨胀。 - 有没有办法为每个片段触发 onCreateOptionsMenu? - 是否可以在视图寻呼机中一次只加载一个项目?

提前致谢

在pager中添加一个ViewPager.OnPageChangeListener,在onPageSelected函数中,调用invalidateOptionsMenu.

我能够通过在工具栏中添加图标而不是展开为菜单来解决这个问题。

 <android.support.v7.widget.Toolbar
                android:id="@+id/news_details_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right">

                    <ImageView
                        android:id="@+id/icon_comment"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_centerVertical="true"
                        android:layout_gravity="right"
                        android:layout_marginRight="8dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_comment" />

                    <ImageView
                        android:id="@+id/icon_share"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_centerVertical="true"
                        android:layout_gravity="right"
                        android:layout_marginRight="8dp"
                        android:layout_toRightOf="@id/icon_comment"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_share" />
                </RelativeLayout>

            </android.support.v7.widget.Toolbar>