package com.weikou.beibeivideo.ui.recommend;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.content.SharedPreferences.Editor;
|
import android.graphics.Color;
|
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.GradientDrawable;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.text.Editable;
|
import android.text.TextUtils;
|
import android.text.TextWatcher;
|
import android.view.KeyEvent;
|
import android.view.LayoutInflater;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.View.OnTouchListener;
|
import android.view.ViewGroup;
|
import android.view.inputmethod.EditorInfo;
|
import android.widget.AdapterView;
|
import android.widget.AdapterView.OnItemClickListener;
|
import android.widget.ArrayAdapter;
|
import android.widget.AutoCompleteTextView;
|
import android.widget.BaseAdapter;
|
import android.widget.EditText;
|
import android.widget.FrameLayout;
|
import android.widget.GridView;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.LinearLayout.LayoutParams;
|
import android.widget.ScrollView;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.androidquery.AQuery;
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.lcjian.library.util.ManifestDataUtil;
|
import com.lcjian.library.util.common.AndroidManifestUtil;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.lcjian.library.widget.FlowLayout;
|
import com.lcjian.library.widget.MyGridView;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.ADSize;
|
import com.qq.e.ads.nativ.NativeExpressAD;
|
import com.qq.e.ads.nativ.NativeExpressADView;
|
import com.qq.e.comm.util.AdError;
|
import com.umeng.analytics.MobclickAgent;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.entity.ad.AdPositionEnum;
|
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
|
import com.weikou.beibeivideo.ui.BaseActivity;
|
import com.weikou.beibeivideo.ui.common.CommonFragmentActivity;
|
import com.weikou.beibeivideo.ui.common.GridVideoAdapter1;
|
import com.weikou.beibeivideo.ui.main.MainActivity;
|
import com.weikou.beibeivideo.ui.video.SuggestKeysAdapter;
|
import com.weikou.beibeivideo.util.BeibeiConstant;
|
import com.weikou.beibeivideo.R;
|
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.goldcorn.GoldCornUtil;
|
import com.weikou.beibeivideo.util.ui.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONArray;
|
import org.json.JSONObject;
|
|
import java.lang.reflect.Type;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Iterator;
|
import java.util.List;
|
|
public class SearchActivity extends BaseActivity implements OnClickListener {
|
|
private EditText et_search_key;
|
|
private TextView tv_clear;
|
|
private TextView tv_search_cancel;
|
|
private GridView gv_hot_search;
|
|
private FlowLayout fl_history_search;
|
|
private LinearLayout ll_search_history, ll_search_rank;
|
|
private SearchSuggestFragment suggestFragment;
|
|
private FrameLayout fl_suggest;
|
|
private String value = "";
|
|
private AQuery mAQuery;
|
|
|
private void initView() {
|
fl_advertisement = findViewById(R.id.fl_advertisement);
|
et_search_key = findViewById(R.id.et_search_key);
|
fl_advertisement.setOnClickListener(this);
|
tv_search_cancel = findViewById(R.id.tv_search_cancel);
|
gv_hot_search = findViewById(R.id.gv_hot_search);
|
fl_history_search = findViewById(R.id.fl_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);
|
ll_search_rank = findViewById(R.id.ll_search_rank);
|
mAQuery = new AQuery(this);
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.search_actvity);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
initView();
|
suggestFragment = new SearchSuggestFragment();
|
|
et_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
@Override
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
if (StringUtils.isEmpty(et_search_key.getEditableText()
|
+ "") && StringUtils.isEmpty(et_search_key.getHint() + "")) {
|
Toast.makeText(SearchActivity.this, "请输入搜索内容!", Toast.LENGTH_LONG).show();
|
return false;
|
} else {
|
if (!StringUtils.isEmpty(et_search_key.getEditableText()
|
+ "")) {
|
jumpToSearch(et_search_key.getEditableText()
|
+ "");
|
} else {
|
jumpToSearch(et_search_key.getHint() + "");
|
}
|
}
|
|
|
return true;
|
}
|
return false;
|
}
|
});
|
|
tv_search_cancel.setOnClickListener(this);
|
tv_clear.setOnClickListener(this);
|
|
value = getIntent().getStringExtra("value");
|
if (!StringUtils.isEmpty(value)) {
|
et_search_key.setHint(value);
|
// et_search_key.setSelection(value.length());
|
}
|
|
et_search_key.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
public void onTextChanged(CharSequence s, int start, int before,
|
int count) {
|
|
}
|
|
@Override
|
public void beforeTextChanged(CharSequence s, int start, int count,
|
int after) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable s) {
|
if (s != null && !TextUtils.isEmpty(s.toString())) {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
R.drawable.ic_clear, 0);
|
} else {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
0, 0);
|
}
|
suggestSearch(s != null ? s.toString() : null);
|
}
|
});
|
|
if (!TextUtils.isEmpty(value)) {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
R.drawable.ic_clear, 0);
|
} else {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
0, 0);
|
}
|
|
et_search_key.setOnTouchListener(new OnTouchListener() {
|
|
@Override
|
public boolean onTouch(View v, MotionEvent event) {
|
// getCompoundDrawables() 可以获取一个长度为4的数组,
|
// 存放drawableLeft,Right,Top,Bottom四个图片资源对象
|
// index=2 表示的是 drawableRight 图片资源对象
|
|
Drawable drawable = et_search_key.getCompoundDrawables()[2];
|
if (drawable == null)
|
return false;
|
|
if (event.getAction() != MotionEvent.ACTION_UP)
|
return false;
|
|
// drawable.getIntrinsicWidth() 获取drawable资源图片呈现的宽度
|
if (event.getX() > et_search_key.getWidth()
|
- et_search_key.getPaddingRight()
|
- drawable.getIntrinsicWidth()) {
|
et_search_key.setText("");
|
}
|
return false;
|
}
|
});
|
|
|
gv_hot_search.setOnItemClickListener(new OnItemClickListener() {
|
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view,
|
int position, long id) {
|
jumpToSearch((String) parent.getItemAtPosition(position));
|
}
|
});
|
|
et_search_key.setHint(getIntent().getStringExtra("key"));
|
mAQuery.id(R.id.iv_short_video).clicked(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
startActivity(new Intent(SearchActivity.this, MainActivity.class).putExtra("position", 0+"").putExtra("cposition", 1+""));
|
finish();
|
}
|
});
|
|
getHotSearch();
|
getHistorySearch();
|
initAdvertisement();// 广点通广告
|
getSearchRank();
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
getHistorySearch();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
public void onClick(final View v) {
|
switch (v.getId()) {
|
case R.id.tv_search_cancel:
|
if (fl_suggest.getVisibility() == View.VISIBLE) {
|
hiddenSuggestFragment();
|
} else
|
finish();
|
break;
|
case R.id.tv_search_clear_his:
|
clearHistorySearch();
|
break;
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
default:
|
break;
|
}
|
}
|
|
|
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) {
|
jumpToSearch(st);
|
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", "");
|
BeibeiVideoAPI.suggestSearch(this, uid, key,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
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();
|
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();
|
}
|
|
if (results != null && results.size() > 0) {
|
showSuggestFragment(key, results);
|
suggestFragment.setData(key, results);
|
} else {
|
hiddenSuggestFragment();
|
}
|
|
}
|
});
|
}
|
|
private void getHistorySearch() {
|
SharedPreferences historySp = getSharedPreferences("search_history",
|
Context.MODE_PRIVATE);
|
String historyStr = historySp.getString("history", "");
|
if (!StringUtils.isBlank(historyStr)) {
|
String[] historyArray = historyStr.split(",");
|
ll_search_history.setVisibility(View.VISIBLE);
|
List<String> history = new ArrayList<>();
|
for (int i = 0; i < 10 && i < historyArray.length; i++) {
|
if (!TextUtils.isEmpty(historyArray[historyArray.length - 1 - i])) {
|
String st = historyArray[historyArray.length - 1 - i];
|
if (st != null && !history.contains(st.trim()))
|
history.add(st.trim());
|
}
|
}
|
|
fl_history_search.removeAllViews();
|
for (String st : history) {
|
TextView textView = new TextView(getApplicationContext());
|
textView.setEllipsize(TextUtils.TruncateAt.END);
|
textView.setTextSize(13);
|
textView.setPadding(DimenUtils.dip2px(getApplicationContext(), 20), DimenUtils.dip2px(getApplicationContext(), 5), DimenUtils.dip2px(getApplicationContext(), 20), DimenUtils.dip2px(getApplicationContext(), 5));
|
textView.setText(st);
|
textView.setTextColor(getResources().getColor(R.color.search_item_text_color));
|
textView.setBackgroundResource(R.drawable.shape_search_history_item_bg);
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
params.setMargins(0, DimenUtils.dip2px(getApplicationContext(), 15), DimenUtils.dip2px(getApplicationContext(), 13), 0);
|
textView.setLayoutParams(params);
|
textView.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
jumpToSearch(st);
|
}
|
});
|
fl_history_search.addView(textView);
|
}
|
} else {
|
ll_search_history.setVisibility(View.GONE);
|
}
|
}
|
|
private void clearHistorySearch() {
|
SharedPreferences historySp = getSharedPreferences("search_history",
|
Context.MODE_PRIVATE);
|
Editor ed = historySp.edit();
|
ed.putString("history", "");
|
ed.commit();
|
getHistorySearch();
|
}
|
|
private void getHotSearch() {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.getHotSearch(this, uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
Gson gson = new GsonBuilder().setFieldNamingPolicy(
|
FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
List<String> hotSearchs = gson.fromJson(jsonObject
|
.getJSONObject("Data").getJSONArray("data")
|
.toString(), new TypeToken<List<String>>() {
|
}.getType());
|
if (hotSearchs == null || hotSearchs.size() == 0) {
|
gv_hot_search.setVisibility(View.GONE);
|
} else {
|
gv_hot_search.setVisibility(View.VISIBLE);
|
}
|
|
gv_hot_search.setAdapter(new HotSearchAdapter(
|
hotSearchs, true));
|
} else {
|
gv_hot_search.setVisibility(View.GONE);
|
}
|
}
|
});
|
}
|
|
private void getSearchRank() {
|
BeibeiVideoAPI.getSearchRank(this,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
JSONObject data = jsonObject
|
.getJSONObject("Data").getJSONObject("data");
|
initSearchRank(data);
|
}
|
}
|
});
|
}
|
|
/**
|
* 广点通广告
|
*/
|
private FrameLayout fl_advertisement;
|
|
// 1.加载广告,先设置加载上下文环境和条件
|
private void initAdvertisement() {
|
//vivo无广告
|
// if ("vivo".equalsIgnoreCase(ManifestDataUtil.getAppMetaData(getApplicationContext(), "UMENG_CHANNEL"))) {
|
// return;
|
// }
|
|
AdUtil.AD_TYPE adType1 = AdUtil.getAdType(getApplicationContext(), AdPositionEnum.videoSearch);
|
if (adType1 == AdUtil.AD_TYPE.gdt) {
|
adType1 = AdUtil.AD_TYPE.gdt2;
|
}
|
final AdUtil.AD_TYPE adType =adType1;
|
|
fl_advertisement.post(new Runnable() {
|
@Override
|
public void run() {
|
int width = fl_advertisement.getWidth() - fl_advertisement.getPaddingLeft() - fl_advertisement.getPaddingRight();
|
ExpressAdManager adManager = new ExpressAdManager(adType,ExpressAdManager.POSITION_SEARCH, getApplicationContext());
|
adManager.loadSearchAd(DimenUtils.px2dip(getApplicationContext(), width), new ExpressAdManager.IAdLoadListener() {
|
@Override
|
public void onSuccess(List<ExpressAdContainer> adList) {
|
if (adList != null && adList.size() > 0) {
|
adManager.renderAndFillAd(SearchActivity.this, adList.get(0), fl_advertisement, ExpressAdManager.POSITION_SEARCH, new ExpressAdManager.IAdEventListener() {
|
@Override
|
public void closeAd(ExpressAdContainer ad) {
|
fl_advertisement.removeAllViews();
|
}
|
});
|
} else if (adType != null) {
|
AdUtil.AD_TYPE newAdType = null;
|
if (adType == AdUtil.AD_TYPE.gdt2) {
|
newAdType = AdUtil.AD_TYPE.csj;
|
} else
|
newAdType = AdUtil.AD_TYPE.gdt2;
|
|
// if (AndroidManifestUtil.isHuaWeiChannel(getApplicationContext()))
|
// newAdType = AdUtil.AD_TYPE.hw;
|
|
new ExpressAdManager(newAdType,ExpressAdManager.POSITION_SEARCH, getApplicationContext()).loadSearchAd(DimenUtils.px2dip(getApplicationContext(), width), new ExpressAdManager.IAdLoadListener() {
|
@Override
|
public void onSuccess(List<ExpressAdContainer> adList) {
|
if (adList != null && adList.size() > 0) {
|
ExpressAdManager.renderAndFillAd(SearchActivity.this, adList.get(0), fl_advertisement,ExpressAdManager.POSITION_SEARCH, new ExpressAdManager.IAdEventListener() {
|
@Override
|
public void closeAd(ExpressAdContainer ad) {
|
fl_advertisement.removeAllViews();
|
}
|
});
|
}
|
|
}
|
});
|
}
|
|
}
|
});
|
}
|
});
|
}
|
|
private void initSearchRank(JSONObject data) {
|
ll_search_rank.removeAllViews();
|
Type type = new TypeToken<List<String>>() {
|
}.getType();
|
Gson gson = new Gson();
|
int count = 0;
|
for (Iterator<String> its = data.keys(); its.hasNext(); ) {
|
String key = its.next();
|
JSONArray array = data.optJSONArray(key);
|
List<String> list = gson.fromJson(array.toString(), type);
|
String[] sts = new String[list.size()];
|
int row = list.size() / 2;
|
for (int i = 0; i < list.size(); i++) {
|
if (i < row) {
|
sts[i * 2] = list.get(i);
|
} else {
|
sts[(i % row) * 2 + 1] = list.get(i);
|
}
|
|
}
|
count++;
|
addSearchRankView(key, Arrays.asList(sts), ll_search_rank, count == data.length());
|
}
|
}
|
|
private void addSearchRankView(String title, List<String> contentList, LinearLayout container, boolean last) {
|
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_search_rank, null);
|
MyGridView gv = view.findViewById(R.id.gv_content);
|
TextView tv_title = view.findViewById(R.id.tv_title);
|
tv_title.setText(title);
|
gv.setAdapter(new SearchRankAdapter(contentList, getApplicationContext()));
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DimenUtils.dip2px(getApplicationContext(), 284), LinearLayout.LayoutParams.WRAP_CONTENT);
|
params.rightMargin = DimenUtils.dip2px(getApplicationContext(), !last ? 11 : 0);
|
container.addView(view, params);
|
}
|
|
private void jumpToSearch(String key) {
|
Intent intent = new Intent(SearchActivity.this,
|
SearchResultActivity.class);
|
intent.putExtra("key", key);
|
if (getIntent() != null) {
|
intent.putExtra("goldcorn", getIntent().getStringExtra("goldcorn"));
|
getIntent().removeExtra("goldcorn");
|
}
|
startActivity(intent);
|
}
|
|
|
class SearchRankAdapter extends BaseAdapter {
|
|
private List<String> mList;
|
private LayoutInflater layoutInflater;
|
|
public SearchRankAdapter(List<String> list, Context context) {
|
this.mList = list;
|
layoutInflater = LayoutInflater.from(context);
|
}
|
|
//最多8个
|
@Override
|
public int getCount() {
|
return mList == null ? 0 : (mList.size()>8?8:mList.size());
|
}
|
|
@Override
|
public Object getItem(int position) {
|
return mList.get(position);
|
}
|
|
@Override
|
public long getItemId(int position) {
|
return position;
|
}
|
|
@Override
|
public View getView(int position, View convertView, ViewGroup parent) {
|
ViewHolder viewHolder;
|
if (convertView == null) {
|
viewHolder = new ViewHolder();
|
convertView = LayoutInflater.from(parent.getContext()).inflate(
|
R.layout.item_search_rank_content, parent, false);
|
viewHolder.tv_rank = convertView
|
.findViewById(R.id.tv_rank);
|
viewHolder.tv_content = convertView
|
.findViewById(R.id.tv_content);
|
convertView.setTag(viewHolder);
|
} else {
|
viewHolder = (ViewHolder) convertView.getTag();
|
}
|
if (position % 2 == 0) {
|
viewHolder.tv_rank.setText((position / 2 + 1) + "");
|
} else {
|
viewHolder.tv_rank.setText((getCount() / 2 + (position + 1) / 2) + "");
|
}
|
viewHolder.tv_content.setText(mList.get(position));
|
|
String color = "#C2C2C2";
|
switch (viewHolder.tv_rank.getText() + "") {
|
case "1":
|
color = "#FE3E3C";
|
break;
|
case "2":
|
color = "#FE853C";
|
break;
|
case "3":
|
color = "#FEC03C";
|
break;
|
default:
|
color = "#C2C2C2";
|
}
|
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
|
new int[]{Color.parseColor(color), Color.parseColor(color)});
|
gd.setCornerRadius(DimenUtils.dip2px(convertView.getContext(), 4));
|
viewHolder.tv_rank.setBackground(gd);
|
convertView.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
jumpToSearch(mList.get(position));
|
}
|
});
|
return convertView;
|
}
|
|
class ViewHolder {
|
TextView tv_rank;
|
TextView tv_content;
|
}
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
|
}
|
}
|