I have implemented two fragments with ActionBar tabs. I am using Sherlock fragments. When I start a fragment I create a searchview on the action bar.
The problem is: When I search something on the first fragment and without closing the searchview or deleting the text, I move to the next fragment, I get a collapsed searchview, which is fine. But when I again get back to my first fragment, I see a list sorted like before but the searchview is collapsed. The only way to get back the original list is to click the searchview once and closing it.
How do I reset the searchview on the first fragment when I move to second fragment or how can keep the searchview open when I come to the first fragment?
Any one of the solutions will do.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="@+id/action_search"
android:title="search"
android:icon="@drawable/search"
android:orderInCategory="100"
android:showAsAction="always|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
This is how I am inflating in the fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
SearchView sv = (SearchView)getActivity().findViewById(R.id.action_search);
sv.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//...
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
bindingData.resetData();
bindingData.getFilter().filter(newText.toString());
return false;
}
});
}
For those who are still scratching their heads and are not able to find a solution, here it how you do it. It is pretty simple:
Just assign an ID to your searchviews when you are creating it (by creating the ids.xml and reserving the id there and assigning it on runtime). Now in onTabUnselected() method of the main tab activity, identify the searchviews and use function setIconified(true) twice (one to remove the text and one to collapse the searchview).
Do not forget to add a check for null query string in onQueryTextChange method of your searchview, it created a problem for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With