Hi i am trying to add "search" functionality to my multiple tabs fragment. it is working fine with one tab, i am able to search and filter data no problem. So i use the same code for my other tabs, however, now the action bar show two search icon instead of one. I tried many different ways by hiding the searchview during detach/ attach action when switching tab, but still two search icons still pop up. One of the search icon will work for my current tab, and the other does not.
here is my code:
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>
fragment 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);
}
}
Fragment 2 is a cloned from Fragment 1 except the view is now:
final View view = inflater.inflate(R.layout.event_tab_past, container, false);
and menu is
inflater.inflate(R.menu.past_event_menu, menu);
Please let me know how should i correct this problem.