切换标签页时出现两个搜索视图

时间:2019-02-21 05:09:55

标签: android searchview

嗨,我正尝试向我的多个标签片段添加“搜索”功能。它与一个选项卡工作正常,我能够搜索和过滤数据没有问题。因此,我在其他标签上使用了相同的代码,但是,现在操作栏上显示的是两个搜索图标,而不是一个。我尝试了多种方法,在切换选项卡时在分离/附加操作期间隐藏了搜索视图,但是仍然弹出两个搜索图标。搜索图标之一将适用于我当前的标签,而另一个图标则无效。

enter image description here

这是我的代码:

upcoming_event_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
        android:id="@+id/action_upcoming_event_search"
        android:title="search event"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>
</menu>

past_event_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
        android:id="@+id/action_past_event_search"
        android:title="search event"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>
</menu>

片段1:

public class EventTabUpcoming extends Fragment implements SearchView.OnQueryTextListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.event_tab_upcoming, container, false);

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listUpcomingEvent);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

    recyclerView.setAdapter(sectionAdapter);

    setHasOptionsMenu(true);

    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.upcoming_event_menu, menu);
    MenuItem item = menu.findItem(R.id.action_upcoming_event_search);

    searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}

@Override
public boolean onQueryTextChange(String searchText) {
    Log.d(logTag, "EventTabUpcoming -> onQueryTextChange w/ searchText: " + searchText);
    for (Section section : sectionAdapter.getCopyOfSectionsMap().values()) {
        if (section instanceof FilterableEventSection) {
            ((FilterableEventSection) section).filter(searchText);
        }
    }

    sectionAdapter.notifyDataSetChanged();
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public void onDetach() {
    super.onDetach();
    Log.d(logTag, "onDetach is called");

    if (searchView != null) {
        searchView.clearFocus();
        searchView.setVisibility(View.GONE);
    }

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Log.d(logTag, "onAttach is called");

    if (searchView != null) {
        searchView.setVisibility(View.VISIBLE);
    }


}

片段2是从片段1克隆的,但现在视图是:

final View view = inflater.inflate(R.layout.event_tab_past, container, false);

菜单为

inflater.inflate(R.menu.past_event_menu, menu);

请让我知道如何解决此问题。

1 个答案:

答案 0 :(得分:0)

这是我的解决方法:

我没有在两个选项卡的活动中都添加菜单,而是仅在viewpager活动中添加了菜单,并且在每个选项卡片段的onCreateOptionMenu中都获取了searchview和setQueryTextListener。

ViewPager活动:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    /**
     * Inflate menu with searchview item
     */
    inflater.inflate(R.menu.event_menu, menu);
}

标签1

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    # get the searchview and set listener
    MenuItem item = menu.findItem(R.id.action_event_search);
    searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}

标签2

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    # get the searchview and set listener
    MenuItem item = menu.findItem(R.id.action_event_search);
    searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}