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

问题描述 投票:0回答:1

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

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);

请让我知道如何纠正这个问题。

android searchview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.