SearchView 不显示在带有嵌套片段的工具栏上

SearchView is not displayed on toolbar with nested Fragments

我一定已经阅读了几乎所有与此相关的问题和答案,但仍然无法正常工作。下面是应用程序外观的粗略草图。 在下面添加代码以提供布局结构以及我可能做错的地方。

activity_main.xml

<android.support.constraint.ConstraintLayout >

<android.support.design.widget.BottomNavigationView>
</android.support.design.widget.BottomNavigationView>

<FrameLayout>
</FrameLayout>

bottom_nav_fragment.xml

<android.support.design.widget.CoordinatorLayout >

<android.support.design.widget.AppBarLayout

    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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



    <android.support.design.widget.TabLayout
        android:id="@+id/liquor_type_tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:tabMode="scrollable">

        <android.support.design.widget.TabItem
           />

        <android.support.design.widget.TabItem
          />

        <android.support.design.widget.TabItem
          />

        <android.support.design.widget.TabItem
           />

        <android.support.design.widget.TabItem
           />

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/price_list_viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

对于每个选项卡项,我都使用了一个公共片段,它在 recyclerView 中具有不同的数据。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.nandu.oru10ml.price_list.CommonPriceFragment">

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

MainActivity.java

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
   // MenuInflater inflater = getS
    Log.d("Activity Options", "inside onCreateOptionsMenu ");
    getMenuInflater().inflate(R.menu.search_item, menu);

    return true;
}

BottomNavFragment.java

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("Fragment onCreate", "inside onCreate ");
    setHasOptionsMenu(true);

}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d("Fragment Options", "inside onCreateOptionsMenu ");
    inflater.inflate(R.menu.search_item, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

CommonFragment.java

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("Fragment onCreate", "inside onCreate ");
    setHasOptionsMenu(true);

}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d("Fragment Options", "inside onCreateOptionsMenu ");
    inflater.inflate(R.menu.search_item, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

最后,search_item.xml

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/action_search"
    android:title="Search for Brand"
    android:icon="@drawable/ic_search_black_24dp"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView">
</item>
</menu>

它不起作用的原因也可以帮助我理解根本原因。

这个SearhView适用于2个bottom nav Fragment和里面所有嵌套的Fragment,来搜索每个里面的Recyclerview。

setSupportActionBar 应该将参数作为工具栏。所以你应该做的是,在你的片段调用中

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar)