SDK Only: Fix search interaction with various samples.
These were broken where pressing back would not clear the search filter. Change-Id: Iafd4a497a31a9689efabe7d92f44618a5e8dc80c
This commit is contained in:
@@ -20,6 +20,7 @@ import android.app.Activity;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.ListFragment;
|
||||
import android.app.LoaderManager;
|
||||
import android.content.Context;
|
||||
import android.content.CursorLoader;
|
||||
import android.content.Loader;
|
||||
import android.database.Cursor;
|
||||
@@ -34,6 +35,7 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.SearchView.OnCloseListener;
|
||||
import android.widget.SimpleCursorAdapter;
|
||||
import android.widget.SearchView.OnQueryTextListener;
|
||||
|
||||
@@ -58,11 +60,15 @@ public class LoaderCursor extends Activity {
|
||||
|
||||
//BEGIN_INCLUDE(fragment_cursor)
|
||||
public static class CursorLoaderListFragment extends ListFragment
|
||||
implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> {
|
||||
implements OnQueryTextListener, OnCloseListener,
|
||||
LoaderManager.LoaderCallbacks<Cursor> {
|
||||
|
||||
// This is the Adapter being used to display the list's data.
|
||||
SimpleCursorAdapter mAdapter;
|
||||
|
||||
// The SearchView for doing filtering.
|
||||
SearchView mSearchView;
|
||||
|
||||
// If non-null, this is the current filter the user has provided.
|
||||
String mCurFilter;
|
||||
|
||||
@@ -91,15 +97,31 @@ public class LoaderCursor extends Activity {
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
public static class MySearchView extends SearchView {
|
||||
public MySearchView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
// The normal SearchView doesn't clear its search text when
|
||||
// collapsed, so we will do this for it.
|
||||
@Override
|
||||
public void onActionViewCollapsed() {
|
||||
setQuery("", false);
|
||||
super.onActionViewCollapsed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
// Place an action bar item for searching.
|
||||
MenuItem item = menu.add("Search");
|
||||
item.setIcon(android.R.drawable.ic_menu_search);
|
||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
|
||||
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
|
||||
SearchView sv = new SearchView(getActivity());
|
||||
sv.setOnQueryTextListener(this);
|
||||
item.setActionView(sv);
|
||||
mSearchView = new MySearchView(getActivity());
|
||||
mSearchView.setOnQueryTextListener(this);
|
||||
mSearchView.setOnCloseListener(this);
|
||||
mSearchView.setIconifiedByDefault(true);
|
||||
item.setActionView(mSearchView);
|
||||
}
|
||||
|
||||
public boolean onQueryTextChange(String newText) {
|
||||
@@ -125,6 +147,14 @@ public class LoaderCursor extends Activity {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onClose() {
|
||||
if (!TextUtils.isEmpty(mSearchView.getQuery())) {
|
||||
mSearchView.setQuery(null, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
// Insert desired behavior here.
|
||||
Log.i("FragmentComplexList", "Item clicked: " + id);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.example.android.apis.app;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
import com.example.android.apis.app.LoaderCursor.CursorLoaderListFragment.MySearchView;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.Collator;
|
||||
@@ -55,6 +56,7 @@ import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.SearchView.OnCloseListener;
|
||||
import android.widget.SearchView.OnQueryTextListener;
|
||||
|
||||
/**
|
||||
@@ -397,11 +399,15 @@ public class LoaderCustom extends Activity {
|
||||
}
|
||||
|
||||
public static class AppListFragment extends ListFragment
|
||||
implements OnQueryTextListener, LoaderManager.LoaderCallbacks<List<AppEntry>> {
|
||||
implements OnQueryTextListener, OnCloseListener,
|
||||
LoaderManager.LoaderCallbacks<List<AppEntry>> {
|
||||
|
||||
// This is the Adapter being used to display the list's data.
|
||||
AppListAdapter mAdapter;
|
||||
|
||||
// The SearchView for doing filtering.
|
||||
SearchView mSearchView;
|
||||
|
||||
// If non-null, this is the current filter the user has provided.
|
||||
String mCurFilter;
|
||||
|
||||
@@ -427,15 +433,31 @@ public class LoaderCustom extends Activity {
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
public static class MySearchView extends SearchView {
|
||||
public MySearchView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
// The normal SearchView doesn't clear its search text when
|
||||
// collapsed, so we will do this for it.
|
||||
@Override
|
||||
public void onActionViewCollapsed() {
|
||||
setQuery("", false);
|
||||
super.onActionViewCollapsed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
// Place an action bar item for searching.
|
||||
MenuItem item = menu.add("Search");
|
||||
item.setIcon(android.R.drawable.ic_menu_search);
|
||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
|
||||
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
|
||||
SearchView sv = new SearchView(getActivity());
|
||||
sv.setOnQueryTextListener(this);
|
||||
item.setActionView(sv);
|
||||
mSearchView = new MySearchView(getActivity());
|
||||
mSearchView.setOnQueryTextListener(this);
|
||||
mSearchView.setOnCloseListener(this);
|
||||
mSearchView.setIconifiedByDefault(true);
|
||||
item.setActionView(mSearchView);
|
||||
}
|
||||
|
||||
@Override public boolean onQueryTextChange(String newText) {
|
||||
@@ -451,6 +473,14 @@ public class LoaderCustom extends Activity {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onClose() {
|
||||
if (!TextUtils.isEmpty(mSearchView.getQuery())) {
|
||||
mSearchView.setQuery(null, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
// Insert desired behavior here.
|
||||
Log.i("LoaderCustom", "Item clicked: " + id);
|
||||
|
||||
@@ -16,10 +16,13 @@
|
||||
|
||||
package com.example.android.apis.app;
|
||||
|
||||
import com.example.android.apis.app.LoaderCursor.CursorLoaderListFragment.MySearchView;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.ListFragment;
|
||||
import android.app.LoaderManager;
|
||||
import android.content.Context;
|
||||
import android.content.CursorLoader;
|
||||
import android.content.Loader;
|
||||
import android.database.Cursor;
|
||||
@@ -35,6 +38,7 @@ import android.view.View;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.SimpleCursorAdapter;
|
||||
import android.widget.SearchView.OnCloseListener;
|
||||
import android.widget.SearchView.OnQueryTextListener;
|
||||
|
||||
/**
|
||||
@@ -58,11 +62,15 @@ public class LoaderRetained extends Activity {
|
||||
|
||||
//BEGIN_INCLUDE(fragment_cursor)
|
||||
public static class CursorLoaderListFragment extends ListFragment
|
||||
implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> {
|
||||
implements OnQueryTextListener, OnCloseListener,
|
||||
LoaderManager.LoaderCallbacks<Cursor> {
|
||||
|
||||
// This is the Adapter being used to display the list's data.
|
||||
SimpleCursorAdapter mAdapter;
|
||||
|
||||
// The SearchView for doing filtering.
|
||||
SearchView mSearchView;
|
||||
|
||||
// If non-null, this is the current filter the user has provided.
|
||||
String mCurFilter;
|
||||
|
||||
@@ -94,15 +102,31 @@ public class LoaderRetained extends Activity {
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
public static class MySearchView extends SearchView {
|
||||
public MySearchView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
// The normal SearchView doesn't clear its search text when
|
||||
// collapsed, so we will do this for it.
|
||||
@Override
|
||||
public void onActionViewCollapsed() {
|
||||
setQuery("", false);
|
||||
super.onActionViewCollapsed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
// Place an action bar item for searching.
|
||||
MenuItem item = menu.add("Search");
|
||||
item.setIcon(android.R.drawable.ic_menu_search);
|
||||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
|
||||
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
|
||||
SearchView sv = new SearchView(getActivity());
|
||||
sv.setOnQueryTextListener(this);
|
||||
item.setActionView(sv);
|
||||
mSearchView = new MySearchView(getActivity());
|
||||
mSearchView.setOnQueryTextListener(this);
|
||||
mSearchView.setOnCloseListener(this);
|
||||
mSearchView.setIconifiedByDefault(true);
|
||||
item.setActionView(mSearchView);
|
||||
}
|
||||
|
||||
public boolean onQueryTextChange(String newText) {
|
||||
@@ -128,6 +152,14 @@ public class LoaderRetained extends Activity {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onClose() {
|
||||
if (!TextUtils.isEmpty(mSearchView.getQuery())) {
|
||||
mSearchView.setQuery(null, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
// Insert desired behavior here.
|
||||
Log.i("FragmentComplexList", "Item clicked: " + id);
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.support.v4.widget.SearchViewCompat;
|
||||
import android.support.v4.widget.SearchViewCompat.OnCloseListenerCompat;
|
||||
import android.support.v4.widget.SearchViewCompat.OnQueryTextListenerCompat;
|
||||
import android.support.v4.widget.SimpleCursorAdapter;
|
||||
|
||||
@@ -101,7 +102,7 @@ public class LoaderCursorSupport extends FragmentActivity {
|
||||
item.setIcon(android.R.drawable.ic_menu_search);
|
||||
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS
|
||||
| MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
|
||||
View searchView = SearchViewCompat.newSearchView(getActivity());
|
||||
final View searchView = SearchViewCompat.newSearchView(getActivity());
|
||||
if (searchView != null) {
|
||||
SearchViewCompat.setOnQueryTextListener(searchView,
|
||||
new OnQueryTextListenerCompat() {
|
||||
@@ -124,6 +125,17 @@ public class LoaderCursorSupport extends FragmentActivity {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
SearchViewCompat.setOnCloseListener(searchView,
|
||||
new OnCloseListenerCompat() {
|
||||
@Override
|
||||
public boolean onClose() {
|
||||
if (!TextUtils.isEmpty(SearchViewCompat.getQuery(searchView))) {
|
||||
SearchViewCompat.setQuery(searchView, null, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
MenuItemCompat.setActionView(item, searchView);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import android.support.v4.content.Loader;
|
||||
import android.support.v4.content.pm.ActivityInfoCompat;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.support.v4.widget.SearchViewCompat;
|
||||
import android.support.v4.widget.SearchViewCompat.OnCloseListenerCompat;
|
||||
import android.support.v4.widget.SearchViewCompat.OnQueryTextListenerCompat;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
@@ -440,7 +441,7 @@ public class LoaderCustomSupport extends FragmentActivity {
|
||||
item.setIcon(android.R.drawable.ic_menu_search);
|
||||
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM
|
||||
| MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
|
||||
View searchView = SearchViewCompat.newSearchView(getActivity());
|
||||
final View searchView = SearchViewCompat.newSearchView(getActivity());
|
||||
if (searchView != null) {
|
||||
SearchViewCompat.setOnQueryTextListener(searchView,
|
||||
new OnQueryTextListenerCompat() {
|
||||
@@ -453,6 +454,17 @@ public class LoaderCustomSupport extends FragmentActivity {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
SearchViewCompat.setOnCloseListener(searchView,
|
||||
new OnCloseListenerCompat() {
|
||||
@Override
|
||||
public boolean onClose() {
|
||||
if (!TextUtils.isEmpty(SearchViewCompat.getQuery(searchView))) {
|
||||
SearchViewCompat.setQuery(searchView, null, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
MenuItemCompat.setActionView(item, searchView);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user