Keep Search View visible after search completed

Improve Search View behavior
Add Search View background color

Bug:26143355
Change-Id: Ifdc4bfff56fc6244c571dfbeff4179e1515d53b7
This commit is contained in:
Aga Wronska
2016-01-08 17:29:24 -08:00
parent e8ff81c1b1
commit 97831485e7
4 changed files with 65 additions and 35 deletions

View File

@@ -15,11 +15,19 @@
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- showAsAction flag impacts the behavior of SearchView.
When set to collapseActionView, collapsing SearchView to icon is the
default behavior. It would fit UX, however after expanding SearchView is
shown on the left site of the toolbar (replacing title). Since no way to
prevent this behavior was found, the flag is set to always. SearchView is
always visible by default and it is being collapse manually by calling
setIconified() method
-->
<item
android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:imeOptions="actionSearch" />
<item

View File

@@ -33,4 +33,6 @@
<color name="item_doc_background">#fffafafa</color>
<color name="item_doc_background_selected">#ffe0f2f1</color>
<color name="menu_search_background">#ff676f74</color>
</resources>

View File

@@ -38,13 +38,15 @@ import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Root;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnActionExpandListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@@ -214,6 +216,7 @@ public abstract class BaseActivity extends Activity {
case R.id.menu_advanced:
case R.id.menu_file_size:
case R.id.menu_new_window:
case R.id.menu_search:
break;
default:
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
@@ -315,6 +318,8 @@ public abstract class BaseActivity extends Activity {
* the (abstract) directoryChanged method will be called.
* @param anim
*/
// TODO: Refactor the usage of the method - now it is called not only when the directory
// changed, but also to refresh the content of the directory while searching
final void onCurrentDirectoryChanged(int anim) {
mDirectoryContainer.setDrawDisappearingFirst(anim == ANIM_DOWN);
onDirectoryChanged(anim);
@@ -325,7 +330,11 @@ public abstract class BaseActivity extends Activity {
}
updateActionBar();
invalidateOptionsMenu();
// Prevents searchView from being recreated while searching
if (!mSearchManager.isSearching()) {
invalidateOptionsMenu();
}
}
final List<String> getExcludedAuthorities() {
@@ -682,7 +691,7 @@ public abstract class BaseActivity extends Activity {
* Facade over the various search parts in the menu.
*/
final class SearchManager implements
SearchView.OnCloseListener, OnActionExpandListener, OnQueryTextListener,
SearchView.OnCloseListener, OnQueryTextListener, OnClickListener, OnFocusChangeListener,
DocumentsToolBar.OnActionViewCollapsedListener {
private boolean mSearchExpanded;
@@ -700,9 +709,10 @@ public abstract class BaseActivity extends Activity {
mView = (SearchView) mMenu.getActionView();
mActionBar.setOnActionViewCollapsedListener(this);
mMenu.setOnActionExpandListener(this);
mView.setOnQueryTextListener(this);
mView.setOnCloseListener(this);
mView.setOnSearchClickListener(this);
mView.setOnQueryTextFocusChangeListener(this);
}
/**
@@ -755,19 +765,13 @@ public abstract class BaseActivity extends Activity {
* search currently.
*/
boolean cancelSearch() {
boolean collapsed = false;
boolean closed = false;
if (mActionBar.hasExpandedActionView()) {
mActionBar.collapseActionView();
collapsed = true;
}
if (isExpanded() || isSearching()) {
onClose();
closed = true;
// If the query string is not empty search view won't get iconified
mView.setQuery("", false);
mView.setIconified(true);
return true;
}
return collapsed || closed;
return false;
}
boolean isSearching() {
@@ -778,6 +782,11 @@ public abstract class BaseActivity extends Activity {
return mSearchExpanded;
}
/**
* Clears the search.
* @return True if the default behavior of clearing/dismissing SearchView should be
* overridden. False otherwise.
*/
@Override
public boolean onClose() {
mSearchExpanded = false;
@@ -786,33 +795,33 @@ public abstract class BaseActivity extends Activity {
return false;
}
mState.currentSearch = null;
onCurrentDirectoryChanged(ANIM_NONE);
mView.setBackgroundColor(
getResources().getColor(android.R.color.transparent, null));
// Refresh the directory if a search was done
if(mState.currentSearch != null) {
mState.currentSearch = null;
onCurrentDirectoryChanged(ANIM_NONE);
}
return false;
}
/**
* Sets mSearchExpanded.
* Called when search icon is clicked to start search.
* Used to detect when the view expanded instead of onMenuItemActionExpand, because
* SearchView has showAsAction set to always and onMenuItemAction* methods are not called.
*/
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
public void onClick (View v) {
mSearchExpanded = true;
updateActionBar();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mSearchExpanded = false;
if (mIgnoreNextCollapse) {
mIgnoreNextCollapse = false;
return true;
}
mState.currentSearch = null;
onCurrentDirectoryChanged(ANIM_NONE);
return true;
mView.setBackgroundColor(
getResources().getColor(R.color.menu_search_background, null));
}
@Override
public boolean onQueryTextSubmit(String query) {
mSearchExpanded = true;
mState.currentSearch = query;
mView.clearFocus();
onCurrentDirectoryChanged(ANIM_NONE);
@@ -824,6 +833,18 @@ public abstract class BaseActivity extends Activity {
return false;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
if(mState.currentSearch == null) {
mView.setIconified(true);
}
else if(TextUtils.isEmpty(mView.getQuery())) {
cancelSearch();
}
}
}
@Override
public void onActionViewCollapsed() {
updateActionBar();

View File

@@ -403,7 +403,6 @@ public class DirectoryFragment extends Fragment {
state.derivedMode = result.mode;
}
state.derivedSortOrder = result.sortOrder;
((BaseActivity) context).onStateChanged();
updateDisplayState();