Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ Maybe it would be useful to take a look into the new official approach
http://www.materialdoc.com/search-filter/

# Usage
**Add the dependencies to your gradle file:**
```javascript
dependencies {
compile 'com.miguelcatalan:materialsearchview:1.4.0'
}
```
**Add MaterialSearchView to your layout file along with the Toolbar** *(Add this block at the bottom of your layout, in order to display it over the rest of the view)*:

```xml
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class MaterialSearchView extends FrameLayout implements Filter.FilterList

private MenuItem mMenuItem;
private boolean mIsSearchOpen = false;
private boolean mIsSearchCovered = false;
private int mAnimationDuration;
private boolean mClearingFocus;

Expand Down Expand Up @@ -185,6 +186,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mUserQuery = s;
mIsSearchCovered = false;
startFilter(s);
MaterialSearchView.this.onTextChanged(s);
}
Expand All @@ -199,7 +201,9 @@ public void afterTextChanged(Editable s) {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mIsSearchCovered = false;
showKeyboard(mSearchSrcTextView);
updateSearchCoveredState();
showSuggestions();
}
}
Expand All @@ -224,7 +228,7 @@ public void onClick(View v) {
} else if (v == mSearchSrcTextView) {
showSuggestions();
} else if (v == mTintView) {
closeSearch();
coverSearch();
}
}
};
Expand Down Expand Up @@ -261,8 +265,7 @@ private void onSubmitQuery() {
CharSequence query = mSearchSrcTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
if (mOnQueryChangeListener == null || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
closeSearch();
mSearchSrcTextView.setText(null);
coverSearch();
}
}
}
Expand Down Expand Up @@ -364,7 +367,12 @@ public void setVoiceSearch(boolean voiceSearch) {
* Call this method to show suggestions list. This shows up when adapter is set. Call {@link #setAdapter(ListAdapter)} before calling this.
*/
public void showSuggestions() {
if (mAdapter != null && mAdapter.getCount() > 0 && mSuggestionsListView.getVisibility() == GONE) {
if (mAdapter != null && mAdapter.getCount() > 0
&& mSuggestionsListView.getVisibility() == GONE && !isSearchCovered()) {
if(mAdapter instanceof SearchAdapter){
((SearchAdapter)mAdapter).notifyDataSetChanged();
}
mTintView.setVisibility(VISIBLE);
mSuggestionsListView.setVisibility(VISIBLE);
}
}
Expand Down Expand Up @@ -405,21 +413,41 @@ public void setAdapter(ListAdapter adapter) {
*/
public void setSuggestions(String[] suggestions) {
if (suggestions != null && suggestions.length > 0) {
mTintView.setVisibility(VISIBLE);
final SearchAdapter adapter = new SearchAdapter(mContext, suggestions, suggestionIcon, ellipsize);
setAdapter(adapter);

setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setQuery((String) adapter.getItem(position), submit);
}
});
initAdapter(suggestions, null);
} else {
mTintView.setVisibility(GONE);
}
}

private void initAdapter(String[] suggestions, String[] emptySuggestions){
mTintView.setVisibility(VISIBLE);
SearchAdapter adapter = new SearchAdapter(mContext, suggestions, suggestionIcon, ellipsize);
if(emptySuggestions != null){
adapter.setDefaultSuggestions(emptySuggestions);
}
setAdapter(adapter);

setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setQuery((String) mAdapter.getItem(position), submit);
}
});
}

/**
* Set Adapter for suggestions list with the given suggestion array and default suggestion array
* @param suggestions array of suggestions
* @param emptySuggestions array of default suggestions - shows when input text is empty
*/
public void setSuggestionsWithDefault(String[] suggestions, String[] emptySuggestions) {
if (emptySuggestions != null && emptySuggestions.length > 0) {
initAdapter(suggestions, emptySuggestions);
} else {
setSuggestions(suggestions);
}
}

/**
* Dismiss the suggestions list.
*/
Expand Down Expand Up @@ -485,6 +513,15 @@ public boolean isSearchOpen() {
return mIsSearchOpen;
}

/**
* Return true if search is covered. I.e. show toolbar edittext while suggestions and keyboard stays hidden
*
* @return
*/
public boolean isSearchCovered() {
return mIsSearchCovered;
}

/**
* Sets animation duration. ONLY FOR PRE-LOLLIPOP!!
*
Expand Down Expand Up @@ -512,19 +549,20 @@ public void showSearch(boolean animate) {
}

//Request Focus
mSearchSrcTextView.setText(null);
// mSearchSrcTextView.setText(null);
mSearchSrcTextView.requestFocus();

if (animate) {
setVisibleWithAnimation();

} else {
mSearchLayout.setVisibility(VISIBLE);
if (mSearchViewListener != null) {
mSearchViewListener.onSearchViewShown();
}
}
mIsSearchOpen = true;
mIsSearchCovered = false;
updateSearchCoveredState();
}

private void setVisibleWithAnimation() {
Expand Down Expand Up @@ -561,7 +599,7 @@ public boolean onAnimationCancel(View view) {
* Close search view.
*/
public void closeSearch() {
if (!isSearchOpen()) {
if (!isSearchOpen() && !isSearchCovered()) {
return;
}

Expand All @@ -574,7 +612,35 @@ public void closeSearch() {
mSearchViewListener.onSearchViewClosed();
}
mIsSearchOpen = false;
mIsSearchCovered = false;
updateSearchCoveredState();
}

/**
* notify adapter about searchView state
*/
private void updateSearchCoveredState(){
if(mAdapter instanceof SearchAdapter){
((SearchAdapter)mAdapter).setSearchCovered(mIsSearchCovered);
((SearchAdapter)mAdapter).notifyDataSetChanged();
}
}

/**
* Cover search view. I.e. show toolbar edittext while suggestionListView and keyboard stays hidden
*/
public void coverSearch() {
dismissSuggestions();
clearFocus();
if (mSearchViewListener != null) {
mSearchViewListener.onSearchViewCovered();
}
mIsSearchOpen = false;
mIsSearchCovered = true;
updateSearchCoveredState();
mTintView.setVisibility(GONE);
mSuggestionsListView.setVisibility(GONE);
mSearchLayout.setVisibility(VISIBLE);
}

/**
Expand Down Expand Up @@ -722,6 +788,8 @@ public interface SearchViewListener {
void onSearchViewShown();

void onSearchViewClosed();

void onSearchViewCovered();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public class SearchAdapter extends BaseAdapter implements Filterable {

private ArrayList<String> data;
private String[] suggestions;
private String[] defaultSuggestions;
private Drawable suggestionIcon;
private LayoutInflater inflater;
private boolean ellipsize;
private boolean isSearchCovered = false;

public SearchAdapter(Context context, String[] suggestions) {
inflater = LayoutInflater.from(context);
Expand All @@ -42,6 +44,23 @@ public SearchAdapter(Context context, String[] suggestions, Drawable suggestionI
this.ellipsize = ellipsize;
}

public SearchAdapter(Context context, String[] suggestions, String[] defaultSuggestions, Drawable suggestionIcon, boolean ellipsize) {
inflater = LayoutInflater.from(context);
data = new ArrayList<>();
this.suggestions = suggestions;
this.defaultSuggestions = defaultSuggestions;
this.suggestionIcon = suggestionIcon;
this.ellipsize = ellipsize;
}

public void setDefaultSuggestions(String[] suggestions){
this.defaultSuggestions = suggestions;
}

public void setSearchCovered(boolean searchCovered) {
isSearchCovered = searchCovered;
}

@Override
public Filter getFilter() {
Filter filter = new Filter() {
Expand All @@ -62,6 +81,13 @@ protected FilterResults performFiltering(CharSequence constraint) {
// Assign the data to the FilterResults
filterResults.values = searchData;
filterResults.count = searchData.size();
}else if(defaultSuggestions!=null && !isSearchCovered){
List<String> searchData = new ArrayList<>();
for (String string : defaultSuggestions) {
searchData.add(string);
}
filterResults.values = searchData;
filterResults.count = searchData.size();
}
return filterResults;
}
Expand Down
Loading