1515import android .view .animation .Animation ;
1616import android .view .animation .AnimationUtils ;
1717import android .view .inputmethod .EditorInfo ;
18+ import android .widget .Button ;
1819import android .widget .EditText ;
1920import android .widget .ListView ;
2021import android .widget .ProgressBar ;
2122import android .widget .TextView ;
2223
2324import com .brian .codeblog .Env ;
25+ import com .brian .codeblog .datacenter .preference .SearchPreference ;
2426import com .brian .codeblog .manager .UsageStatsManager ;
2527import com .brian .codeblog .model .Bloger ;
2628import com .brian .codeblog .model .SearchResult ;
2729import com .brian .codeblog .parser .CSDNHtmlParser ;
2830import com .brian .codeblog .proctocol .HttpGetSearchBlogRequest ;
2931import com .brian .codeblog .proctocol .base .IResponseCallback ;
3032import com .brian .common .tools .CommonAdapter ;
33+ import com .brian .common .tools .GsonHelper ;
3134import com .brian .common .utils .LogUtil ;
3235import com .brian .common .utils .ResourceUtil ;
36+ import com .brian .common .utils .SDKUtil ;
3337import com .brian .common .utils .ToastUtil ;
3438import com .brian .common .utils .UIUtil ;
3539import com .brian .common .view .RefreshLayout ;
3640import com .brian .common .view .TitleBar ;
3741import com .brian .csdnblog .R ;
3842
43+ import java .util .List ;
44+
3945import butterknife .BindView ;
4046import butterknife .ButterKnife ;
4147
4248public class SearchActivity extends BaseActivity {
4349 private static final String TAG = SearchActivity .class .getSimpleName ();
4450
51+ private static final int MAX_HISTORY_COUNT = 8 ;
52+
4553 @ 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 ;
4957 @ 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 ;
5162 private View mFooterLayout ;
5263
53- private CommonAdapter <SearchResult > mAdapter = null ;
64+ private CommonAdapter <SearchResult > mSearchResultAdapter = null ;
65+ private CommonAdapter <String > mHistoryAdapter = null ;
5466
5567 private int mCurrentPage = 1 ;
5668 private String mInputText = "" ;
@@ -77,10 +89,27 @@ private void initUI() {
7789 mResultListView .addFooterView (mFooterLayout );
7890 mRefreshLayout .setChildView (mResultListView );
7991
92+ SDKUtil .showSoftInputOnfocus (mSearchInput , true );
93+
8094 mTitleBar .setTitle ("CSDN搜索" );
8195 mTitleBar .setRightImageVisible (View .INVISIBLE );
8296
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 ) {
84113 private ForegroundColorSpan mColorSpanName = new ForegroundColorSpan (ResourceUtil .getColor (R .color .light_blue ));
85114 @ Override
86115 public void convert (ViewHolder holder , final SearchResult item ) {
@@ -131,7 +160,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
131160 return view ;
132161 }
133162 };
134- mResultListView .setAdapter (mAdapter );
163+ mResultListView .setAdapter (mSearchResultAdapter );
135164
136165 mResultListView .setVisibility (View .INVISIBLE );
137166 }
@@ -148,18 +177,25 @@ public void onClick(View v) {
148177 @ Override
149178 public boolean onEditorAction (TextView v , int actionId , KeyEvent event ) {
150179 if (actionId == EditorInfo .IME_ACTION_SEARCH || (event !=null && event .getKeyCode () == KeyEvent .KEYCODE_ENTER )) {
151- onSendMsg ();
180+ doSearch ();
152181 return true ;
153182 }
154183 return false ;
155184 }
156185 });
157186
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+
158194 mSearchBtn .setOnClickListener (new OnClickListener () {
159195
160196 @ Override
161197 public void onClick (View v ) {
162- onSendMsg ();
198+ doSearch ();
163199 }
164200 });
165201
@@ -171,9 +207,17 @@ public void onLoad() {
171207 }
172208 }
173209 });
210+
211+ mClearHistoryBtn .setOnClickListener (new OnClickListener () {
212+ @ Override
213+ public void onClick (View v ) {
214+ clearHistoryList ();
215+ mClearHistoryBtn .setVisibility (View .GONE );
216+ }
217+ });
174218 }
175219
176- private void onSendMsg () {
220+ private void doSearch () {
177221 UIUtil .hideKeyboard (mSearchInput );
178222 mInputText = mSearchInput .getText ().toString ()
179223 .replaceAll (
@@ -184,6 +228,8 @@ private void onSendMsg() {
184228 String url = getSearchUrl (mInputText , 1 );
185229 mCurrentPage = 1 ;
186230 loadListData (url );
231+
232+ addSearchHistory (mInputText );
187233 UsageStatsManager .sendUsageData (UsageStatsManager .USAGE_SEARCH , mInputText );
188234 } else {
189235 ToastUtil .showMsg ("请输入适当关键字" );
@@ -197,7 +243,7 @@ private String getSearchUrl(String keyWord, int page) {
197243 }
198244
199245 private void loadListData (String loadUrl ) {
200- if (mAdapter .isEmpty ()) {
246+ if (mSearchResultAdapter .isEmpty ()) {
201247 mProgressBar .setVisibility (View .VISIBLE );
202248 } else {
203249 mFooterLayout .setVisibility (View .VISIBLE );
@@ -209,20 +255,21 @@ private void loadListData(String loadUrl) {
209255 @ Override
210256 public void onSuccess (HttpGetSearchBlogRequest .ResultData resultData ) {
211257 mRefreshLayout .setLoading (false );
258+ mHistoryLy .setVisibility (View .GONE );
212259
213260 if (resultData .blogInfoList == null || resultData .blogInfoList .isEmpty ()) {
214- if (mAdapter .isEmpty ()) {
261+ if (mSearchResultAdapter .isEmpty ()) {
215262 // 没有搜索到结果的提示
216263 UsageStatsManager .sendUsageData (UsageStatsManager .EXP_EMPTY_SEARCH , mInputText );
217264 }
218265 } else {
219266 if (mCurrentPage <= 1 ) {
220- mAdapter .removeAllDatas ();
267+ mSearchResultAdapter .removeAllDatas ();
221268 }
222269 mResultListView .setVisibility (View .VISIBLE );
223270 mProgressBar .setVisibility (View .INVISIBLE );
224271 mCurrentPage ++;
225- mAdapter .addDatas (resultData .blogInfoList );
272+ mSearchResultAdapter .addDatas (resultData .blogInfoList );
226273 }
227274 }
228275
@@ -237,4 +284,33 @@ public void onFailure(int errorCode, String msg) {
237284 }
238285 });
239286 }
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+ }
240316}
0 commit comments