| | |
| | | import android.widget.AdapterView.OnItemClickListener; |
| | | import android.widget.ArrayAdapter; |
| | | import android.widget.AutoCompleteTextView; |
| | | import android.widget.EditText; |
| | | import android.widget.FrameLayout; |
| | | import android.widget.GridView; |
| | | import android.widget.ImageView; |
| | |
| | | import com.weikou.beibeivideo.util.DimenUtils; |
| | | import com.weikou.beibeivideo.util.ad.AdUtil; |
| | | import com.weikou.beibeivideo.util.ad.ExpressAdManager; |
| | | import com.weikou.beibeivideo.util.ui.TopStatusSettings; |
| | | |
| | | import org.apache.http.Header; |
| | | import org.json.JSONObject; |
| | |
| | | |
| | | public class SearchActivity extends BaseActivity implements OnClickListener { |
| | | |
| | | private AutoCompleteTextView et_search_key; |
| | | private EditText et_search_key; |
| | | |
| | | private TextView tv_clear; |
| | | |
| | |
| | | |
| | | private LinearLayout ll_search_history; |
| | | |
| | | private SearchSuggestFragment suggestFragment; |
| | | |
| | | private FrameLayout fl_suggest; |
| | | |
| | | private String value = ""; |
| | | |
| | | private boolean touchSearchInput = false; |
| | | |
| | | private void initView() { |
| | | fl_advertisement = findViewById(R.id.fl_advertisement); |
| | |
| | | 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 |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.search_actvity); |
| | | initStatusBar(); |
| | | TopStatusSettings.setStatusViewAndDeepColor(this); |
| | | initView(); |
| | | suggestFragment = new SearchSuggestFragment(); |
| | | |
| | | et_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
| | | @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); |
| | | } |
| | | }); |
| | | |
| | | if (!TextUtils.isEmpty(value)) { |
| | | 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); |
| | | } |
| | | |
| | | et_search_key.setOnItemClickListener(new OnItemClickListener() { |
| | | |
| | | @Override |
| | | public void onItemClick(AdapterView<?> parent, View view, |
| | | int position, long id) { |
| | | if (parent.getItemAtPosition(position) != null) { |
| | | Intent intent = new Intent(SearchActivity.this, |
| | | SearchResultActivity.class); |
| | | intent.putExtra("key", parent.getItemAtPosition(position) |
| | | .toString()); |
| | | startActivity(intent); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | et_search_key.setOnTouchListener(new OnTouchListener() { |
| | | |
| | |
| | | // getCompoundDrawables() 可以获取一个长度为4的数组, |
| | | // 存放drawableLeft,Right,Top,Bottom四个图片资源对象 |
| | | // index=2 表示的是 drawableRight 图片资源对象 |
| | | |
| | | Drawable drawable = et_search_key.getCompoundDrawables()[2]; |
| | | if (drawable == null) |
| | | return false; |
| | |
| | | return false; |
| | | } |
| | | }); |
| | | |
| | | |
| | | gv_hot_search.setOnItemClickListener(new OnItemClickListener() { |
| | | |
| | |
| | | public void onResume() { |
| | | super.onResume(); |
| | | getHistorySearch(); |
| | | MobclickAgent.onPageStart("搜索页"); |
| | | } |
| | | |
| | | @Override |
| | | public void onPause() { |
| | | super.onPause(); |
| | | MobclickAgent.onPageEnd("搜索页"); |
| | | } |
| | | |
| | | @Override |
| | | public void onClick(final View v) { |
| | | switch (v.getId()) { |
| | | case R.id.tv_search_cancel: |
| | | finish(); |
| | | if (fl_suggest.getVisibility() == View.VISIBLE) { |
| | | hiddenSuggestFragment(); |
| | | } else |
| | | finish(); |
| | | break; |
| | | case R.id.tv_search_clear_his: |
| | | clearHistorySearch(); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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()); |
| | | |
| | | SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results); |
| | | et_search_key.setAdapter(adapter); |
| | | adapter.notifyDataSetChanged(); |
| | | |
| | | // 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(); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | fl_advertisement.post(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | int width = fl_advertisement.getWidth()-fl_advertisement.getPaddingLeft()-fl_advertisement.getPaddingRight(); |
| | | int width = fl_advertisement.getWidth() - fl_advertisement.getPaddingLeft() - fl_advertisement.getPaddingRight(); |
| | | new ExpressAdManager(adType, getApplicationContext()).loadSearchAd(DimenUtils.px2dip(getApplicationContext(), width), new ExpressAdManager.IAdLoadListener() { |
| | | @Override |
| | | public void onSuccess(List<ExpressAdContainer> adList) { |