Reputation: 582
I have a problem: I have a listView
in a Fragment
, I implemented a searchView (actionView) to filter the list in the listView
with the searchView onQueryTextChange
event. I don't have problems in this part and everything works fine.
adapter = new ListaEmpleadosAdapter(getActivity(), empleadoItems, context);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_buscar).getActionView();
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
try {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
} catch (Exception e) {
System.err.println("ERROR FragmentEmpleado -> " + e.toString());
}
return true;
}
};
When I search something and press back button in the first time, the soft keyboard is closed. That's normal. When I press back button again, the searchView is closed, that's normal, but the list doesn't return to have the initial items.
How I can get the event when searchView is collapsed (because searchView.setOnCloseListener
doesn't work) to restore the initial list items? Or any way to restore the initial list items deleting the adapter filter...
HISTORY:
Thanks a lot!
Upvotes: 3
Views: 1766
Reputation: 43023
There is a method on SearchView
that gets fired when the search view collapses (onActionViewCollapsed
) but there is no event for it. To get around it, I created my own class inheriting from SearchView
and defining 2 events to capture collapsing and expanding. The class also provides methods to hook up your event handlers.
public class MySearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
@Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
Upvotes: 6