package com.tejia.lijin.app.ui.trends;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.text.TextUtils;
|
import android.view.KeyEvent;
|
import android.view.View;
|
import android.view.WindowManager;
|
import android.view.inputmethod.EditorInfo;
|
import android.widget.EditText;
|
import android.widget.FrameLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.umeng.analytics.MobclickAgent;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.util.FlowLayout;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONArray;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/2/21.
|
* 搜索
|
*/
|
|
public class CollegeSearchActivity extends BaseActivity
|
implements View.OnClickListener {
|
|
public EditText et_search;
|
private TextView tv_search;
|
public static CollegeSearchActivity instance = null;
|
private FlowLayout fl_hot_search;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_college_search);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
init();
|
getHotKey();
|
}
|
|
private void init() {
|
findViewById(R.id.iv_back).setOnClickListener(this);
|
et_search = findViewById(R.id.et_search);
|
tv_search = findViewById(R.id.tv_search);
|
fl_hot_search = findViewById(R.id.fl_hot_search);
|
et_search.setFocusable(true);
|
et_search.setFocusableInTouchMode(true);
|
et_search.requestFocus();
|
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
|
tv_search.setOnClickListener(this);
|
|
//编辑完之后点击软键盘上的回车键
|
et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
@Override
|
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
|
//点击搜索 或者 完成 并且值不为空
|
if ((arg1 == EditorInfo.IME_ACTION_SEARCH || arg1 == EditorInfo.IME_ACTION_DONE)) {
|
return search(null);
|
}
|
return false;
|
}
|
});
|
|
String key = getIntent().getStringExtra("key");
|
if (!StringUtils.isEmpty(key)) {
|
et_search.setHint(key);
|
}
|
}
|
|
/**
|
* 搜索
|
*
|
* @return
|
*/
|
private boolean search(String key) {
|
if (StringUtils.isEmpty(key)) {
|
key = et_search.getText() == null ? "" : et_search.getText().toString();
|
if (StringUtils.isEmpty(key))
|
key = et_search.getHint() == null ? "" : et_search.getHint().toString();
|
if (!StringUtils.isEmpty(key.toString())) {
|
startActivity(new Intent(this, CollegeSearchResultActivity.class).putExtra("key", key));
|
//跳转搜索
|
return true;
|
}
|
return false;
|
} else {
|
startActivity(new Intent(this, CollegeSearchResultActivity.class).putExtra("key", key));
|
return true;
|
}
|
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
}
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
}
|
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_search://取消
|
if (!search(null)) {
|
Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show();
|
}
|
break;
|
case R.id.iv_back:
|
finish();
|
break;
|
default:
|
break;
|
}
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
|
}
|
|
|
List<String> wordsList = new ArrayList<>();
|
|
private void getHotKey() {
|
ShoppingApi.getCollegeArticleHotKey(this, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (jsonObject.optInt("code") == 0) {
|
JSONArray array = jsonObject.optJSONObject("data").optJSONArray("words");
|
if (array != null) {
|
wordsList.clear();
|
for (int i = 0; i < array.length(); i++) {
|
wordsList.add(array.optString(i));
|
}
|
if (wordsList.size() > 0)
|
et_search.setHint(wordsList.get((int) (Math.random() * wordsList.size())));
|
setHotSearchData(wordsList);
|
}
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
}
|
|
/**
|
* 设置热门搜索的值
|
*
|
* @param wordsList
|
*/
|
private void setHotSearchData(List<String> wordsList) {
|
if (wordsList != null) {
|
fl_hot_search.removeAllViews();
|
for (String key : wordsList) {
|
setHotSearch(this, key);
|
}
|
}
|
}
|
|
/**
|
* 热门搜索设置
|
*/
|
private void setHotSearch(Context mContext, String key) {
|
final TextView tv = new TextView(mContext);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
params.setMargins(0, DimenUtils.dip2px(mContext, 8), DimenUtils.dip2px(mContext, 12), 0);
|
tv.setLayoutParams(params);
|
tv.setLines(1);
|
tv.setTextColor(mContext.getResources().getColor(R.color.text_black_1));
|
tv.setEllipsize(TextUtils.TruncateAt.END);
|
tv.setBackgroundResource(R.drawable.shape_search1);
|
tv.setPadding(DimenUtils.dip2px(mContext, 20), DimenUtils.dip2px(mContext, 5), DimenUtils.dip2px(mContext, 20), DimenUtils.dip2px(mContext, 5));
|
tv.setText(key);
|
tv.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
search(tv.getText() + "");
|
}
|
});
|
fl_hot_search.addView(tv);
|
}
|
}
|