diff options
author | ByteHamster <info@bytehamster.com> | 2020-03-15 20:48:43 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-03-15 20:48:43 +0100 |
commit | ca09df5b1a4ee57f86f40da120a7feb284eebf77 (patch) | |
tree | c77d41f7243ed9b77bb110e8794c6953f640e167 | |
parent | bdc786e44d74dc8b8d9f3fce8a9bf5493069d53b (diff) | |
download | AntennaPod-ca09df5b1a4ee57f86f40da120a7feb284eebf77.zip |
Centralize creation of search icon
6 files changed, 71 insertions, 76 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java index 7258aec8e..f33b4e28f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/EpisodesListFragment.java @@ -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); } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java index aadb34e73..befe8757e 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java @@ -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); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 27b105ee8..b038a7ad1 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -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 diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java index f4a5ed299..463df92bb 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/SearchFragment.java @@ -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 diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java index 64eb72ee3..76091327d 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java @@ -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; + } + }); + } } diff --git a/app/src/main/res/menu/search.xml b/app/src/main/res/menu/search.xml new file mode 100644 index 000000000..d3a2477be --- /dev/null +++ b/app/src/main/res/menu/search.xml @@ -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>
\ No newline at end of file |