我肯定已经阅读了几乎所有与此相关的问题和答案,但仍然无法使其正常工作。以下是该应用外观的草图。
android - SearchView未显示在带有嵌套 fragment 的工具栏上-LMLPHP
在下面添加代码以给出布局的结构以及我可能做错的地方。

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个以及内部所有嵌套片段,以搜索每个片段中存在的Recyclerview。

最佳答案

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

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

关于android - SearchView未显示在带有嵌套 fragment 的工具栏上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51465724/

10-12 02:30