Centralize creation of search icon

This commit is contained in:
ByteHamster 2020-03-15 20:48:43 +01:00
parent bdc786e44d
commit ca09df5b1a
6 changed files with 71 additions and 76 deletions

View File

@ -168,23 +168,7 @@ public abstract class EpisodesListFragment extends Fragment {
}
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.episodes, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
sv.setQueryHint(getString(R.string.search_label));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
sv.clearFocus();
((MainActivity) requireActivity()).loadChildFragment(SearchFragment.newInstance(s));
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), 0);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}

View File

@ -209,41 +209,7 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
optionsMenu = menu;
FeedMenuHandler.onCreateOptionsMenu(inflater, menu);
iconTintManager.updateTint();
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
sv.setQueryHint(getString(R.string.search_label));
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
menu.findItem(R.id.sort_items).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.findItem(R.id.filter_items).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.findItem(R.id.episode_actions).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.findItem(R.id.refresh_item).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
getActivity().invalidateOptionsMenu();
return true;
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
sv.clearFocus();
if (feed != null) {
((MainActivity) getActivity()).loadChildFragment(SearchFragment.newInstance(s, feed.getId()));
}
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), feedID);
if (feed == null || feed.getLink() == null) {
menu.findItem(R.id.share_link_item).setVisible(false);
menu.findItem(R.id.visit_website_item).setVisible(false);

View File

@ -284,24 +284,7 @@ public class QueueFragment extends Fragment {
super.onCreateOptionsMenu(menu, inflater);
if (queue != null) {
inflater.inflate(R.menu.queue, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) MenuItemCompat.getActionView(searchItem);
sv.setQueryHint(getString(R.string.search_label));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
sv.clearFocus();
((MainActivity) getActivity()).loadChildFragment(SearchFragment.newInstance(s));
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
MenuItemUtils.setupSearchItem(menu, (MainActivity) getActivity(), 0);
MenuItemUtils.refreshLockItem(getActivity(), menu);
// Show Lock Item only if queue is sorted manually

View File

@ -16,7 +16,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
@ -139,10 +138,12 @@ public class SearchFragment extends Fragment implements AdapterView.OnItemClickL
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuItem item = menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label);
item.setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
final SearchView sv = new SearchView(getActivity());
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
item.expandActionView();
final SearchView sv = (SearchView) item.getActionView();
sv.setQueryHint(getString(R.string.search_label));
sv.clearFocus();
sv.setQuery(getArguments().getString(ARG_QUERY), false);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
@ -158,8 +159,18 @@ public class SearchFragment extends Fragment implements AdapterView.OnItemClickL
return false;
}
});
sv.setIconifiedByDefault(false);
item.setActionView(sv);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
getFragmentManager().popBackStack();
return true;
}
});
}
@Subscribe

View File

@ -4,7 +4,11 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.widget.SearchView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.fragment.SearchFragment;
/**
* Utilities for menu items
@ -26,4 +30,40 @@ public class MenuItemUtils extends de.danoeh.antennapod.core.menuhandler.MenuIte
ta.recycle();
}
public static void setupSearchItem(Menu menu, MainActivity activity, long feedId) {
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) searchItem.getActionView();
sv.setQueryHint(activity.getString(R.string.search_label));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
sv.clearFocus();
activity.loadChildFragment(SearchFragment.newInstance(s, feedId));
searchItem.collapseActionView();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() != searchItem.getItemId()) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
}
}
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
activity.invalidateOptionsMenu();
return true;
}
});
}
}

View File

@ -0,0 +1,11 @@
<?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_search"
android:icon="?attr/action_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="@string/search_label"/>
</menu>