-
Notifications
You must be signed in to change notification settings - Fork 668
/
ScrollingSearchExampleFragment.java
263 lines (206 loc) · 10.4 KB
/
ScrollingSearchExampleFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.arlib.floatingsearchviewdemo.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.res.ResourcesCompat;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.arlib.floatingsearchview.util.Util;
import com.arlib.floatingsearchviewdemo.R;
import com.arlib.floatingsearchviewdemo.data.ColorSuggestion;
import com.arlib.floatingsearchviewdemo.data.ColorWrapper;
import com.arlib.floatingsearchviewdemo.data.DataHelper;
import java.util.List;
public class ScrollingSearchExampleFragment extends BaseExampleFragment implements AppBarLayout.OnOffsetChangedListener {
private final String TAG = "BlankFragment";
public static final long FIND_SUGGESTION_SIMULATED_DELAY = 250;
private FloatingSearchView mSearchView;
private AppBarLayout mAppBar;
private boolean mIsDarkSearchTheme = false;
private String mLastQuery = "";
public ScrollingSearchExampleFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_scrolling_search_example, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSearchView = (FloatingSearchView) view.findViewById(R.id.floating_search_view);
mAppBar = (AppBarLayout) view.findViewById(R.id.appbar);
mAppBar.addOnOffsetChangedListener(this);
setupDrawer();
setupSearchBar();
}
private void setupSearchBar() {
mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
@Override
public void onSearchTextChanged(String oldQuery, final String newQuery) {
if (!oldQuery.equals("") && newQuery.equals("")) {
mSearchView.clearSuggestions();
} else {
//this shows the top left circular progress
//you can call it where ever you want, but
//it makes sense to do it when loading something in
//the background.
mSearchView.showProgress();
//simulates a query call to a data source
//with a new query.
DataHelper.findSuggestions(getActivity(), newQuery, 5,
FIND_SUGGESTION_SIMULATED_DELAY, new DataHelper.OnFindSuggestionsListener() {
@Override
public void onResults(List<ColorSuggestion> results) {
//this will swap the data and
//render the collapse/expand animations as necessary
mSearchView.swapSuggestions(results);
//let the users know that the background
//process has completed
mSearchView.hideProgress();
}
});
}
Log.d(TAG, "onSearchTextChanged()");
}
});
mSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
@Override
public void onSuggestionClicked(final SearchSuggestion searchSuggestion) {
ColorSuggestion colorSuggestion = (ColorSuggestion) searchSuggestion;
DataHelper.findColors(getActivity(), colorSuggestion.getBody(),
new DataHelper.OnFindColorsListener() {
@Override
public void onResults(List<ColorWrapper> results) {
//show search results
}
});
Log.d(TAG, "onSuggestionClicked()");
mLastQuery = searchSuggestion.getBody();
}
@Override
public void onSearchAction(String query) {
mLastQuery = query;
DataHelper.findColors(getActivity(), query,
new DataHelper.OnFindColorsListener() {
@Override
public void onResults(List<ColorWrapper> results) {
//show search results
}
});
Log.d(TAG, "onSearchAction()");
}
});
mSearchView.setOnFocusChangeListener(new FloatingSearchView.OnFocusChangeListener() {
@Override
public void onFocus() {
//show suggestions when search bar gains focus (typically history suggestions)
mSearchView.swapSuggestions(DataHelper.getHistory(getActivity(), 3));
Log.d(TAG, "onFocus()");
}
@Override
public void onFocusCleared() {
//set the title of the bar so that when focus is returned a new query begins
mSearchView.setSearchBarTitle(mLastQuery);
//you can also set setSearchText(...) to make keep the query there when not focused and when focus returns
//mSearchView.setSearchText(searchSuggestion.getBody());
Log.d(TAG, "onFocusCleared()");
}
});
//handle menu clicks the same way as you would
//in a regular activity
mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() {
@Override
public void onActionMenuItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_colors) {
mIsDarkSearchTheme = true;
//demonstrate setting colors for items
mSearchView.setBackgroundColor(Color.parseColor("#787878"));
mSearchView.setViewTextColor(Color.parseColor("#e9e9e9"));
mSearchView.setHintTextColor(Color.parseColor("#e9e9e9"));
mSearchView.setActionMenuOverflowColor(Color.parseColor("#e9e9e9"));
mSearchView.setMenuItemIconColor(Color.parseColor("#e9e9e9"));
mSearchView.setLeftActionIconColor(Color.parseColor("#e9e9e9"));
mSearchView.setClearBtnColor(Color.parseColor("#e9e9e9"));
mSearchView.setDividerColor(Color.parseColor("#BEBEBE"));
mSearchView.setLeftActionIconColor(Color.parseColor("#e9e9e9"));
} else {
//just print action
Toast.makeText(getActivity().getApplicationContext(), item.getTitle(),
Toast.LENGTH_SHORT).show();
}
}
});
//use this listener to listen to menu clicks when app:floatingSearch_leftAction="showHome"
mSearchView.setOnHomeActionClickListener(new FloatingSearchView.OnHomeActionClickListener() {
@Override
public void onHomeClicked() {
Log.d(TAG, "onHomeClicked()");
}
});
/*
* Here you have access to the left icon and the text of a given suggestion
* item after as it is bound to the suggestion list. You can utilize this
* callback to change some properties of the left icon and the text. For example, you
* can load the left icon images using your favorite image loading library, or change text color.
*
*
* Important:
* Keep in mind that the suggestion list is a RecyclerView, so views are reused for different
* items in the list.
*/
mSearchView.setOnBindSuggestionCallback(new SearchSuggestionsAdapter.OnBindSuggestionCallback() {
@Override
public void onBindSuggestion(View suggestionView, ImageView leftIcon,
TextView textView, SearchSuggestion item, int itemPosition) {
ColorSuggestion colorSuggestion = (ColorSuggestion) item;
String textColor = mIsDarkSearchTheme ? "#ffffff" : "#000000";
String textLight = mIsDarkSearchTheme ? "#bfbfbf" : "#787878";
if (colorSuggestion.getIsHistory()) {
leftIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(),
R.drawable.ic_history_black_24dp, null));
Util.setIconColor(leftIcon, Color.parseColor(textColor));
leftIcon.setAlpha(.36f);
} else {
leftIcon.setAlpha(0.0f);
leftIcon.setImageDrawable(null);
}
textView.setTextColor(Color.parseColor(textColor));
String text = colorSuggestion.getBody()
.replaceFirst(mSearchView.getQuery(),
"<font color=\"" + textLight + "\">" + mSearchView.getQuery() + "</font>");
textView.setText(Html.fromHtml(text));
}
});
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mSearchView.setTranslationY(verticalOffset);
}
@Override
public boolean onActivityBackPress() {
//if mSearchView.setSearchFocused(false) causes the focused search
//to close, then we don't want to close the activity. if mSearchView.setSearchFocused(false)
//returns false, we know that the search was already closed so the call didn't change the focus
//state and it makes sense to call supper onBackPressed() and close the activity
if (!mSearchView.setSearchFocused(false)) {
return false;
}
return true;
}
private void setupDrawer() {
attachSearchViewActivityDrawer(mSearchView);
}
}