package com.mugua.mgvideo.ui.recommend;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.drawable.Drawable;
|
import android.os.Bundle;
|
import android.support.v4.widget.SwipeRefreshLayout;
|
import android.text.Editable;
|
import android.text.TextUtils;
|
import android.text.TextWatcher;
|
import android.view.Gravity;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.View.OnTouchListener;
|
import android.view.WindowManager;
|
import android.widget.AdapterView;
|
import android.widget.AdapterView.OnItemClickListener;
|
import android.widget.ArrayAdapter;
|
import android.widget.AutoCompleteTextView;
|
import android.widget.FrameLayout;
|
import android.widget.LinearLayout;
|
import android.widget.ListView;
|
import android.widget.ProgressBar;
|
import android.widget.RadioButton;
|
import android.widget.RadioGroup;
|
import android.widget.RadioGroup.OnCheckedChangeListener;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
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.RefreshLayout;
|
import com.lcjian.library.util.common.DimenUtils;
|
import com.lcjian.library.util.common.SoftKeyboardUtils;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.mugua.mgvideo.R;
|
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.mugua.mgvideo.BasicTextHttpResponseHandler;
|
import com.mugua.mgvideo.MGVideoAPI;
|
import com.mugua.mgvideo.entity.VideoInfo;
|
import com.mugua.mgvideo.ui.BaseActivity;
|
import com.mugua.mgvideo.ui.category.CategoryListAdapter;
|
import com.mugua.mgvideo.ui.media.VideoDetailActivity;
|
import com.mugua.mgvideo.util.BeibeiConstant;
|
|
public class SearchResultActivity extends BaseActivity implements
|
OnClickListener {
|
|
private AutoCompleteTextView et_search_key;
|
|
private TextView tv_search_cancel, tv_back;
|
|
private RadioGroup rg_search;
|
|
private RefreshLayout rl_video_search;
|
|
private ListView gv_video_search;
|
|
private CategoryListAdapter mGridVideoAdapter;
|
|
private List<VideoInfo> mVideoInfos;
|
|
private int mCurrentPage = 1;
|
|
private String mCurrentType;
|
|
private String mCurrentKey;
|
|
private boolean mFirst = true;
|
|
// private ImageView iv_search_result_ad;// 添加聚效广告图片
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.search_result_activity);
|
|
fl_advertisement = (FrameLayout) findViewById(R.id.fl_advertisement);
|
et_search_key = (AutoCompleteTextView) findViewById(R.id.et_search_key);
|
tv_back = (TextView) findViewById(R.id.tv_top_bar_left);
|
tv_back.setOnClickListener(this);
|
tv_search_cancel = (TextView) findViewById(R.id.tv_search_cancel);
|
rg_search = (RadioGroup) findViewById(R.id.rg_search);
|
rl_video_search = (RefreshLayout) findViewById(R.id.rl_search_result);
|
gv_video_search = (ListView) findViewById(R.id.gv_video_search);
|
// iv_search_result_ad = (ImageView)
|
// findViewById(R.id.iv_search_result_ad);// 聚效广告
|
// iv_search_result_ad.setOnClickListener(this);// 聚效广告点击事件监听
|
|
// WindowManager adwm = (WindowManager) SearchResultActivity.this
|
// .getSystemService(Context.WINDOW_SERVICE);
|
// int adWidth = adwm.getDefaultDisplay().getWidth();
|
// int adHeight = (int) (adWidth * (9.0f / 15.56f));
|
// LayoutParams adParams = iv_search_result_ad.getLayoutParams();
|
// adParams.width = adWidth;
|
// adParams.height = adHeight;
|
// iv_search_result_ad.setLayoutParams(adParams);
|
|
int width = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
|
.getDefaultDisplay().getWidth();
|
LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams(
|
width / 5, 3);
|
params0.gravity = Gravity.CENTER_HORIZONTAL;
|
findViewById(R.id.s_v_0).setLayoutParams(params0);
|
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
|
width / 5, 3);
|
params1.gravity = Gravity.CENTER_HORIZONTAL;
|
findViewById(R.id.s_v_1).setLayoutParams(params1);
|
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
|
width / 5, 3);
|
params2.gravity = Gravity.CENTER_HORIZONTAL;
|
findViewById(R.id.s_v_2).setLayoutParams(params2);
|
|
tv_search_cancel.setOnClickListener(this);
|
|
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 (!TextUtils.isEmpty(s.toString())
|
|| !TextUtils.isEmpty(et_search_key.getText())) {
|
if (mFirst) {
|
mFirst = false;
|
} else {
|
suggestSearch(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,
|
R.drawable.ic_search, 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) {
|
mFirst = true;
|
mCurrentKey = parent.getItemAtPosition(position).toString();
|
SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this);
|
rl_video_search.setRefreshing(true);
|
if (BeibeiConstant.ADSHOW) {
|
loadAD();
|
} else {
|
search(mCurrentKey, mCurrentType);
|
}
|
|
}
|
});
|
|
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;
|
}
|
});
|
|
rg_search.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
@Override
|
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
switch (checkedId) {
|
case R.id.rb_all: {
|
mCurrentType = "0";
|
findViewById(R.id.s_v_0).setVisibility(View.VISIBLE);
|
findViewById(R.id.s_v_1).setVisibility(View.INVISIBLE);
|
findViewById(R.id.s_v_2).setVisibility(View.INVISIBLE);
|
}
|
break;
|
case R.id.rb_movie: {
|
mCurrentType = "1";
|
findViewById(R.id.s_v_0).setVisibility(View.INVISIBLE);
|
findViewById(R.id.s_v_1).setVisibility(View.VISIBLE);
|
findViewById(R.id.s_v_2).setVisibility(View.INVISIBLE);
|
}
|
break;
|
case R.id.rb_tvshow: {
|
mCurrentType = "2";
|
findViewById(R.id.s_v_0).setVisibility(View.INVISIBLE);
|
findViewById(R.id.s_v_1).setVisibility(View.INVISIBLE);
|
findViewById(R.id.s_v_2).setVisibility(View.VISIBLE);
|
}
|
break;
|
default:
|
mCurrentType = "0";
|
break;
|
}
|
gv_video_search.postDelayed(new Runnable() {
|
|
@Override
|
public void run() {
|
rl_video_search.setRefreshing(true);
|
}
|
}, 200);
|
}
|
});
|
|
mCurrentKey = getIntent().getStringExtra("key");
|
et_search_key.setText(mCurrentKey);
|
et_search_key.setSelection(mCurrentKey.length());
|
// et_search_key.setText(mCurrentKey);
|
|
int padding = DimenUtils.dipToPixels(8, this);
|
gv_video_search.setPadding(padding, padding,
|
padding, padding);
|
|
gv_video_search.addHeaderView(new View(this));
|
rl_video_search.setFooter(new ProgressBar(this));
|
|
mVideoInfos = new ArrayList<VideoInfo>();
|
mGridVideoAdapter = new CategoryListAdapter(mVideoInfos,
|
SearchResultActivity.this);
|
gv_video_search.setAdapter(mGridVideoAdapter);
|
|
rl_video_search.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
mCurrentPage = 1;
|
// search(mCurrentKey, mCurrentType);
|
if (BeibeiConstant.ADSHOW) {
|
loadAD();
|
} else {
|
search(mCurrentKey, mCurrentType);
|
}
|
}
|
});
|
|
rl_video_search.setOnLoadListener(new RefreshLayout.OnLoadListener() {
|
@Override
|
public void onLoad() {
|
mCurrentPage++;
|
search(mCurrentKey, mCurrentType);
|
}
|
});
|
|
gv_video_search.setOnItemClickListener(new OnItemClickListener() {
|
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view,
|
int position, long id) {
|
VideoInfo videoInfo = (VideoInfo) parent
|
.getItemAtPosition(position);
|
Intent intent = new Intent(SearchResultActivity.this,
|
VideoDetailActivity.class);
|
intent.putExtra("video_info", videoInfo);
|
startActivity(intent);
|
}
|
});
|
|
((RadioButton) findViewById(R.id.rb_all)).setChecked(true);
|
if (BeibeiConstant.ADSHOW) {
|
loadAD();
|
} else {
|
search(mCurrentKey, mCurrentType);
|
}
|
// loadAD();
|
// if (BeibeiConstant.ADSHOW) {
|
// initAdvertisement();// 广点通广告
|
// }
|
}
|
|
private void suggestSearch(String key) {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
MGVideoAPI.suggestSearch(this, uid, key,
|
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> results = gson.fromJson(jsonObject
|
.getJSONObject("Data").getJSONArray("data")
|
.toString(), new TypeToken<List<String>>() {
|
}.getType());
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
|
SearchResultActivity.this,
|
android.R.layout.simple_list_item_1,
|
results);
|
et_search_key.setAdapter(adapter);
|
adapter.notifyDataSetChanged();
|
}
|
}
|
});
|
}
|
|
private void search(String key, String videoType) {
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
MGVideoAPI.search(this, uid, key, videoType,
|
String.valueOf(mCurrentPage),
|
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<VideoInfo> videoInfos = gson.fromJson(
|
jsonObject.getJSONObject("Data")
|
.getJSONArray("data").toString(),
|
new TypeToken<List<VideoInfo>>() {
|
}.getType());
|
if (mCurrentPage == 1) {
|
VideoInfo adViewInfo = null;
|
if (mVideoInfos.size() > 0 && mVideoInfos.get(0).getAdView() != null) {
|
adViewInfo = mVideoInfos.get(0);
|
}
|
|
mVideoInfos.clear();
|
if (adViewInfo != null)
|
mVideoInfos.add(adViewInfo);
|
}
|
mVideoInfos.addAll(videoInfos);
|
mGridVideoAdapter.notifyDataSetChanged();
|
if ((videoInfos == null || videoInfos.size() == 0)
|
&& mCurrentPage > 1) {
|
Toast.makeText(SearchResultActivity.this, "没有更多了",
|
Toast.LENGTH_SHORT).show();
|
mCurrentPage--;
|
}
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
rl_video_search.setRefreshing(false);
|
rl_video_search.setLoading(false);
|
}
|
});
|
|
saveHistory(key);
|
}
|
|
VideoInfo adInfo;
|
|
private void loadAD() {
|
if (mCurrentPage == 1) {
|
mVideoInfos.clear();
|
}
|
NativeExpressAD nativeExpressAD = new NativeExpressAD(SearchResultActivity.this, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
|
BeibeiConstant.GDT_APP_ID, BeibeiConstant.GDT_TEMPLATE_SEARCH_RESULT, new NativeExpressAD.NativeExpressADListener() {
|
@Override
|
public void onADLoaded(final List<NativeExpressADView> list) {
|
tv_back.post(new Runnable() {
|
@Override
|
public void run() {
|
if (null != list && list.size() > 0) {
|
VideoInfo info = new VideoInfo();
|
info.setAdView(list.get(0));
|
mVideoInfos.add(info);
|
}
|
search(mCurrentKey, mCurrentType);
|
}
|
});
|
|
}
|
|
@Override
|
public void onRenderFail(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADExposure(NativeExpressADView nativeExpressADView) {
|
}
|
|
@Override
|
public void onADClicked(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADClosed(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onNoAD(AdError adError) {
|
tv_back.post(new Runnable() {
|
@Override
|
public void run() {
|
search(mCurrentKey, mCurrentType);
|
}
|
});
|
|
}
|
}); // 传入Activity
|
// 注意:如果您在联盟平台上新建原生模板广告位时,选择了支持视频,那么可以进行个性化设置(可选)
|
nativeExpressAD.setVideoOption(new VideoOption.Builder()
|
.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
|
.setAutoPlayMuted(true) // 自动播放时为静音
|
.build()); //
|
nativeExpressAD.loadAD(2);
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
MobclickAgent.onPageStart("搜索结果页");
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
MobclickAgent.onPageEnd("搜索结果页");
|
}
|
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_search_cancel: {
|
if (tv_search_cancel.getText().toString()
|
.equals(getResources().getString(R.string.search))) {
|
if (StringUtils.isEmpty(et_search_key.getEditableText()
|
.toString())) {
|
mCurrentKey = et_search_key.getText().toString();
|
} else {
|
mCurrentKey = et_search_key.getEditableText().toString();
|
}
|
SoftKeyboardUtils.hideSoftInput(this);
|
rl_video_search.setRefreshing(true);
|
mCurrentPage = 1;
|
search(mCurrentKey, mCurrentType);
|
} else {
|
finish();
|
}
|
}
|
break;
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
// case R.id.iv_search_result_ad:// 聚效广告点击事件处理
|
// if (list.size() > 0) {
|
// list.get(0).onAdClicked();
|
// }
|
// break;
|
default:
|
break;
|
}
|
}
|
|
public void saveHistory(String key) {
|
// 获取搜索框信息
|
SharedPreferences historySp = getSharedPreferences("search_history",
|
Context.MODE_PRIVATE);
|
String history = historySp.getString("history", "");
|
|
key = key.replace(",", "");
|
String[] historyArray = history.split(",");
|
List<String> temps = new ArrayList<String>();
|
for (int i = 0; i < historyArray.length && i < 20; i++) {
|
if (!TextUtils.isEmpty(historyArray[i])
|
&& !historyArray[i].equals(key)) {
|
temps.add(historyArray[i]);
|
}
|
}
|
temps.add(key);
|
StringBuilder builder = new StringBuilder();
|
for (String str : temps) {
|
builder.append(str).append(",");
|
}
|
historySp
|
.edit()
|
.putString("history",
|
builder.substring(0, builder.length() - 1)).commit();
|
}
|
|
@Override
|
protected void onDestroy() {
|
// if (mAdView != null) {
|
// mAdView.clearThread();
|
// }
|
super.onDestroy();
|
}
|
|
/**
|
* 广点通广告
|
*/
|
private FrameLayout fl_advertisement;
|
|
// private void initAdvertisement() {
|
// GDTBanner.initAdvertisement(SearchResultActivity.this,
|
// BeibeiConstant.GDT_APP_ID, BeibeiConstant.GDT_BANNER_ID,
|
// fl_advertisement);
|
// }
|
|
/**
|
* 聚效广告
|
*/
|
// private IMvNativeAdLoader mAdView;
|
//
|
// private List<IMvNativeAd> list = new ArrayList<IMvNativeAd>();
|
//
|
// private void initAdvertisement() {
|
// mAdView = Mvad.initNativeAdLoader(SearchResultActivity.this,
|
// BeibeiConstant.JXSR_ID, new IMvNativeAdListener() {
|
//
|
// @Override
|
// public void onNativeAdLoadSucceeded(
|
// ArrayList<IMvNativeAd> nativeads) {
|
// if (list != null || list.size() > 0) {
|
// list.clear();
|
// }
|
// list.addAll(nativeads);
|
// String adPath = list.get(0).getContent()
|
// .optString("contentimg");
|
// if (!StringUtils.isEmpty(adPath)) {
|
// ImageLoader.getInstance().displayImage(adPath,
|
// iv_search_result_ad);
|
// }
|
// list.get(0).onAdShowed();
|
// }
|
//
|
// @Override
|
// public void onNativeAdLoadFailed() {
|
// }
|
// }, false);
|
// }
|
}
|