| | |
| | | |
| | | private SearchSuggestFragment suggestFragment; |
| | | |
| | | private FrameLayout fl_suggest; |
| | | |
| | | private String value = ""; |
| | | |
| | | private boolean touchSearchInput = false; |
| | |
| | | gv_history_search = findViewById(R.id.gv_history_search); |
| | | tv_clear = findViewById(R.id.tv_search_clear_his); |
| | | ll_search_history = findViewById(R.id.ll_search_history); |
| | | fl_suggest = findViewById(R.id.fl_suggest); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public void afterTextChanged(Editable s) { |
| | | if (!TextUtils.isEmpty(s.toString()) |
| | | || !TextUtils.isEmpty(et_search_key.getHint())) { |
| | | |
| | | |
| | | suggestSearch(s.toString()); |
| | | if (s != null && !TextUtils.isEmpty(s.toString())) { |
| | | et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0, |
| | | R.drawable.ic_clear, 0); |
| | | // tv_search_cancel.setText(R.string.search); |
| | | } else { |
| | | et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0, |
| | | 0, 0); |
| | | // tv_search_cancel.setText(R.string.cancel); |
| | | } |
| | | suggestSearch(s != null ? s.toString() : null); |
| | | } |
| | | }); |
| | | |
| | |
| | | // getCompoundDrawables() 可以获取一个长度为4的数组, |
| | | // 存放drawableLeft,Right,Top,Bottom四个图片资源对象 |
| | | // index=2 表示的是 drawableRight 图片资源对象 |
| | | if (et_search_key.isFocused()) { |
| | | findViewById(R.id.fl_suggest).setVisibility(View.VISIBLE); |
| | | getSupportFragmentManager().beginTransaction().replace(R.id.fl_suggest, suggestFragment).commitAllowingStateLoss(); |
| | | } else { |
| | | findViewById(R.id.fl_suggest).setVisibility(View.GONE); |
| | | getSupportFragmentManager().beginTransaction().remove(suggestFragment).commitAllowingStateLoss(); |
| | | } |
| | | |
| | | Drawable drawable = et_search_key.getCompoundDrawables()[2]; |
| | | if (drawable == null) |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | private void hiddenSuggestFragment() { |
| | | fl_suggest.setVisibility(View.GONE); |
| | | getSupportFragmentManager().beginTransaction().remove(suggestFragment).commitAllowingStateLoss(); |
| | | } |
| | | |
| | | private void showSuggestFragment(String key, List<String> list) { |
| | | Bundle bundle = new Bundle(); |
| | | bundle.putString("key", key); |
| | | bundle.putString("list", new Gson().toJson(list)); |
| | | fl_suggest.setVisibility(View.VISIBLE); |
| | | // if (suggestFragment.isAdded()) { |
| | | // return; |
| | | // } |
| | | suggestFragment.setItemClickListener(new SearchSuggestFragment.OnItemClickListener() { |
| | | @Override |
| | | public void onClick(String st) { |
| | | Intent intent = new Intent(SearchActivity.this, |
| | | SearchResultActivity.class); |
| | | intent.putExtra("key", st); |
| | | startActivity(intent); |
| | | hiddenSuggestFragment(); |
| | | } |
| | | }); |
| | | |
| | | suggestFragment.setArguments(bundle); |
| | | getSupportFragmentManager().beginTransaction().replace(R.id.fl_suggest, suggestFragment).commitAllowingStateLoss(); |
| | | } |
| | | |
| | | private void suggestSearch(String key) { |
| | | if (StringUtils.isEmpty(key)) { |
| | | hiddenSuggestFragment(); |
| | | } |
| | | |
| | | |
| | | SharedPreferences preferences = getSharedPreferences("user", |
| | | Context.MODE_PRIVATE); |
| | | String uid = preferences.getString("uid", ""); |
| | |
| | | public void onSuccessPerfect(int statusCode, |
| | | Header[] headers, JSONObject jsonObject) |
| | | throws Exception { |
| | | List<String> results = null; |
| | | if (jsonObject.getBoolean("IsPost")) { |
| | | Gson gson = new GsonBuilder().setFieldNamingPolicy( |
| | | FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | List<String> results = gson.fromJson(jsonObject |
| | | results = gson.fromJson(jsonObject |
| | | .getJSONObject("Data").getJSONArray("data") |
| | | .toString(), new TypeToken<List<String>>() { |
| | | }.getType()); |
| | | suggestFragment.setData(key, results); |
| | | |
| | | |
| | | // SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results); |
| | | // et_search_key.setAdapter(adapter); |
| | | // adapter.notifyDataSetChanged(); |
| | | } |
| | | |
| | | if (results != null && results.size() > 0) { |
| | | showSuggestFragment(key, results); |
| | | suggestFragment.setData(key, results); |
| | | } else { |
| | | hiddenSuggestFragment(); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |