15
15
import android .view .animation .Animation ;
16
16
import android .view .animation .AnimationUtils ;
17
17
import android .view .inputmethod .EditorInfo ;
18
+ import android .widget .Button ;
18
19
import android .widget .EditText ;
19
20
import android .widget .ListView ;
20
21
import android .widget .ProgressBar ;
21
22
import android .widget .TextView ;
22
23
23
24
import com .brian .codeblog .Env ;
25
+ import com .brian .codeblog .datacenter .preference .SearchPreference ;
24
26
import com .brian .codeblog .manager .UsageStatsManager ;
25
27
import com .brian .codeblog .model .Bloger ;
26
28
import com .brian .codeblog .model .SearchResult ;
27
29
import com .brian .codeblog .parser .CSDNHtmlParser ;
28
30
import com .brian .codeblog .proctocol .HttpGetSearchBlogRequest ;
29
31
import com .brian .codeblog .proctocol .base .IResponseCallback ;
30
32
import com .brian .common .tools .CommonAdapter ;
33
+ import com .brian .common .tools .GsonHelper ;
31
34
import com .brian .common .utils .LogUtil ;
32
35
import com .brian .common .utils .ResourceUtil ;
36
+ import com .brian .common .utils .SDKUtil ;
33
37
import com .brian .common .utils .ToastUtil ;
34
38
import com .brian .common .utils .UIUtil ;
35
39
import com .brian .common .view .RefreshLayout ;
36
40
import com .brian .common .view .TitleBar ;
37
41
import com .brian .csdnblog .R ;
38
42
43
+ import java .util .List ;
44
+
39
45
import butterknife .BindView ;
40
46
import butterknife .ButterKnife ;
41
47
42
48
public class SearchActivity extends BaseActivity {
43
49
private static final String TAG = SearchActivity .class .getSimpleName ();
44
50
51
+ private static final int MAX_HISTORY_COUNT = 8 ;
52
+
45
53
@ BindView (R .id .title_bar ) TitleBar mTitleBar ;
46
- @ BindView (R .id .et_search ) EditText mSearchInput = null ;
47
- @ BindView (R .id .bt_search ) TextView mSearchBtn = null ;
48
- @ BindView (R .id .lv_result ) ListView mResultListView = null ;
54
+ @ BindView (R .id .et_search ) EditText mSearchInput ;
55
+ @ BindView (R .id .bt_search ) Button mSearchBtn ;
56
+ @ BindView (R .id .lv_result ) ListView mResultListView ;
49
57
@ BindView (R .id .swipe_container ) RefreshLayout mRefreshLayout ;
50
- @ BindView (R .id .progressbar ) ProgressBar mProgressBar = null ;
58
+ @ BindView (R .id .progressbar ) ProgressBar mProgressBar ;
59
+ @ BindView (R .id .search_history_ll ) View mHistoryLy ;
60
+ @ BindView (R .id .search_history_lv ) ListView mHistoryList ;
61
+ @ BindView (R .id .clear_history_btn ) Button mClearHistoryBtn ;
51
62
private View mFooterLayout ;
52
63
53
- private CommonAdapter <SearchResult > mAdapter = null ;
64
+ private CommonAdapter <SearchResult > mSearchResultAdapter = null ;
65
+ private CommonAdapter <String > mHistoryAdapter = null ;
54
66
55
67
private int mCurrentPage = 1 ;
56
68
private String mInputText = "" ;
@@ -77,10 +89,27 @@ private void initUI() {
77
89
mResultListView .addFooterView (mFooterLayout );
78
90
mRefreshLayout .setChildView (mResultListView );
79
91
92
+ SDKUtil .showSoftInputOnfocus (mSearchInput , true );
93
+
80
94
mTitleBar .setTitle ("CSDN搜索" );
81
95
mTitleBar .setRightImageVisible (View .INVISIBLE );
82
96
83
- mAdapter = new CommonAdapter <SearchResult >(Env .getContext (), null , R .layout .item_list_search ) {
97
+ mHistoryAdapter = new CommonAdapter <String >(Env .getContext (), getSearchHistory (), R .layout .item_search_history ) {
98
+ @ Override
99
+ public void convert (ViewHolder holder , final String item ) {
100
+ holder .setText (R .id .contentTextView , item );
101
+ holder .setOnClickListener (new OnClickListener () {
102
+ @ Override
103
+ public void onClick (View v ) {
104
+ mSearchInput .setText (item );
105
+ doSearch ();
106
+ }
107
+ });
108
+ }
109
+ };
110
+ mHistoryList .setAdapter (mHistoryAdapter );
111
+
112
+ mSearchResultAdapter = new CommonAdapter <SearchResult >(Env .getContext (), null , R .layout .item_list_search ) {
84
113
private ForegroundColorSpan mColorSpanName = new ForegroundColorSpan (ResourceUtil .getColor (R .color .light_blue ));
85
114
@ Override
86
115
public void convert (ViewHolder holder , final SearchResult item ) {
@@ -131,7 +160,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
131
160
return view ;
132
161
}
133
162
};
134
- mResultListView .setAdapter (mAdapter );
163
+ mResultListView .setAdapter (mSearchResultAdapter );
135
164
136
165
mResultListView .setVisibility (View .INVISIBLE );
137
166
}
@@ -148,18 +177,25 @@ public void onClick(View v) {
148
177
@ Override
149
178
public boolean onEditorAction (TextView v , int actionId , KeyEvent event ) {
150
179
if (actionId == EditorInfo .IME_ACTION_SEARCH || (event !=null && event .getKeyCode () == KeyEvent .KEYCODE_ENTER )) {
151
- onSendMsg ();
180
+ doSearch ();
152
181
return true ;
153
182
}
154
183
return false ;
155
184
}
156
185
});
157
186
187
+ mSearchInput .setOnFocusChangeListener (new View .OnFocusChangeListener () {
188
+ @ Override
189
+ public void onFocusChange (View v , boolean hasFocus ) {
190
+ mHistoryLy .setVisibility (hasFocus ? View .VISIBLE : View .GONE );
191
+ }
192
+ });
193
+
158
194
mSearchBtn .setOnClickListener (new OnClickListener () {
159
195
160
196
@ Override
161
197
public void onClick (View v ) {
162
- onSendMsg ();
198
+ doSearch ();
163
199
}
164
200
});
165
201
@@ -171,9 +207,17 @@ public void onLoad() {
171
207
}
172
208
}
173
209
});
210
+
211
+ mClearHistoryBtn .setOnClickListener (new OnClickListener () {
212
+ @ Override
213
+ public void onClick (View v ) {
214
+ clearHistoryList ();
215
+ mClearHistoryBtn .setVisibility (View .GONE );
216
+ }
217
+ });
174
218
}
175
219
176
- private void onSendMsg () {
220
+ private void doSearch () {
177
221
UIUtil .hideKeyboard (mSearchInput );
178
222
mInputText = mSearchInput .getText ().toString ()
179
223
.replaceAll (
@@ -184,6 +228,8 @@ private void onSendMsg() {
184
228
String url = getSearchUrl (mInputText , 1 );
185
229
mCurrentPage = 1 ;
186
230
loadListData (url );
231
+
232
+ addSearchHistory (mInputText );
187
233
UsageStatsManager .sendUsageData (UsageStatsManager .USAGE_SEARCH , mInputText );
188
234
} else {
189
235
ToastUtil .showMsg ("请输入适当关键字" );
@@ -197,7 +243,7 @@ private String getSearchUrl(String keyWord, int page) {
197
243
}
198
244
199
245
private void loadListData (String loadUrl ) {
200
- if (mAdapter .isEmpty ()) {
246
+ if (mSearchResultAdapter .isEmpty ()) {
201
247
mProgressBar .setVisibility (View .VISIBLE );
202
248
} else {
203
249
mFooterLayout .setVisibility (View .VISIBLE );
@@ -209,20 +255,21 @@ private void loadListData(String loadUrl) {
209
255
@ Override
210
256
public void onSuccess (HttpGetSearchBlogRequest .ResultData resultData ) {
211
257
mRefreshLayout .setLoading (false );
258
+ mHistoryLy .setVisibility (View .GONE );
212
259
213
260
if (resultData .blogInfoList == null || resultData .blogInfoList .isEmpty ()) {
214
- if (mAdapter .isEmpty ()) {
261
+ if (mSearchResultAdapter .isEmpty ()) {
215
262
// 没有搜索到结果的提示
216
263
UsageStatsManager .sendUsageData (UsageStatsManager .EXP_EMPTY_SEARCH , mInputText );
217
264
}
218
265
} else {
219
266
if (mCurrentPage <= 1 ) {
220
- mAdapter .removeAllDatas ();
267
+ mSearchResultAdapter .removeAllDatas ();
221
268
}
222
269
mResultListView .setVisibility (View .VISIBLE );
223
270
mProgressBar .setVisibility (View .INVISIBLE );
224
271
mCurrentPage ++;
225
- mAdapter .addDatas (resultData .blogInfoList );
272
+ mSearchResultAdapter .addDatas (resultData .blogInfoList );
226
273
}
227
274
}
228
275
@@ -237,4 +284,33 @@ public void onFailure(int errorCode, String msg) {
237
284
}
238
285
});
239
286
}
287
+
288
+ private List <String > getSearchHistory () {
289
+ List historyList = new GsonHelper <String >().fromJson (SearchPreference .getInstance ().getHistoryListJson ());
290
+ if (historyList == null || historyList .size () <= 0 ) {
291
+ mClearHistoryBtn .setVisibility (View .GONE );
292
+ } else {
293
+ mClearHistoryBtn .setVisibility (View .VISIBLE );
294
+ }
295
+ return historyList ;
296
+ }
297
+
298
+ private void addSearchHistory (String keyWord ) {
299
+ if (mHistoryAdapter .containData (keyWord )) {
300
+ return ;
301
+ }
302
+ if (mHistoryAdapter .getCount () >= MAX_HISTORY_COUNT ) {
303
+ mHistoryAdapter .removeDataAt (mHistoryAdapter .getCount ()-1 );
304
+ }
305
+ mHistoryAdapter .addData (0 , keyWord );
306
+ SearchPreference .getInstance ().setHistoryListJson (new GsonHelper <String >().convert2String (mHistoryAdapter .getDatas ()));
307
+ if (!mHistoryAdapter .isEmpty ()) {
308
+ mClearHistoryBtn .setVisibility (View .VISIBLE );
309
+ }
310
+ }
311
+
312
+ private void clearHistoryList () {
313
+ mHistoryAdapter .removeAllDatas ();
314
+ SearchPreference .getInstance ().setHistoryListJson (new GsonHelper <String >().convert2String (mHistoryAdapter .getDatas ()));
315
+ }
240
316
}
0 commit comments