package com.weikou.beibeivideo.ui.recommend;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
import android.graphics.Typeface;
|
import android.graphics.drawable.Drawable;
|
import android.os.Bundle;
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
|
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.EditText;
|
import android.widget.FrameLayout;
|
import android.widget.LinearLayout;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
|
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.common.SoftKeyboardUtils;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.NativeADUnifiedListener;
|
import com.qq.e.ads.nativ.NativeUnifiedAD;
|
import com.qq.e.ads.nativ.NativeUnifiedADData;
|
import com.qq.e.comm.util.AdError;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.VideoInfo;
|
import com.weikou.beibeivideo.entity.VideoType;
|
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
|
import com.weikou.beibeivideo.entity.video.VideoContent;
|
import com.weikou.beibeivideo.ui.BaseActivity;
|
import com.weikou.beibeivideo.ui.video.SearchResultAdapter;
|
import com.weikou.beibeivideo.util.DimenUtils;
|
import com.weikou.beibeivideo.util.JsonUtil;
|
import com.weikou.beibeivideo.util.UmengEventUtil;
|
import com.weikou.beibeivideo.util.ad.ExpressAdManager;
|
import com.weikou.beibeivideo.util.ad.GDTConstant;
|
import com.weikou.beibeivideo.util.goldcorn.GoldCornUtil;
|
import com.weikou.beibeivideo.util.ui.DividerItemDecoration;
|
import com.weikou.beibeivideo.util.ui.TopStatusSettings;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class SearchResultActivity extends BaseActivity implements
|
OnClickListener {
|
|
private EditText et_search_key;
|
|
private TextView tv_search_cancel;
|
|
private RecyclerView rv_video_search;
|
|
private List<VideoContent> mVideoInfos;
|
|
private SearchResultAdapter adapter;
|
|
|
private int mCurrentPage = 1;
|
|
private String mCurrentType = "0";
|
|
private String mCurrentKey;
|
|
private boolean mFirst = true;
|
|
private SwipeRefreshLayout rl_search_result;
|
|
private ExpressAdManager expressAdManager;
|
|
private boolean isLoad;
|
|
private View loading;
|
|
private LinearLayout ll_top_bar;
|
|
List<VideoType> typeList;
|
|
private LinearLayout ll_empty;
|
|
private SearchSuggestFragment suggestFragment;
|
|
private FrameLayout fl_suggest;
|
|
//赚影视豆
|
private Runnable makeGoldCornRunnable;
|
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.search_result_activity);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
|
suggestFragment = new SearchSuggestFragment();
|
|
rl_search_result = findViewById(R.id.rl_search_result);
|
et_search_key = findViewById(R.id.et_search_key);
|
tv_search_cancel = findViewById(R.id.tv_search_cancel);
|
rv_video_search = findViewById(R.id.rv_video_search);
|
ll_top_bar = findViewById(R.id.ll_top_bar);
|
ll_empty = findViewById(R.id.ll_empty);
|
fl_suggest = findViewById(R.id.fl_suggest);
|
|
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())) {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
R.drawable.ic_clear, 0);
|
} else {
|
et_search_key.setCompoundDrawablesWithIntrinsicBounds(0, 0,
|
0, 0);
|
}
|
if (mFirst) {
|
mFirst = false;
|
} else {
|
suggestSearch(s != null ? s.toString() : null);
|
}
|
}
|
});
|
|
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;
|
}
|
});
|
|
|
et_search_key.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
@Override
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
startSearch();
|
return true;
|
}
|
return false;
|
}
|
});
|
|
|
mCurrentKey = getIntent().getStringExtra("key");
|
et_search_key.setText(mCurrentKey);
|
et_search_key.setSelection(mCurrentKey.length());
|
// et_search_key.setText(mCurrentKey);
|
|
mVideoInfos = new ArrayList<>();
|
rl_search_result.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
mCurrentPage = 1;
|
search(mCurrentKey, mCurrentType);
|
}
|
});
|
|
|
rv_video_search.setLayoutManager(new MyLinearLayoutManager(getApplicationContext()));
|
DividerItemDecoration decoration = new DividerItemDecoration();
|
decoration.setSize(DimenUtils.dip2px(rv_video_search.getContext(), 15));
|
rv_video_search.addItemDecoration(decoration);
|
|
adapter = new SearchResultAdapter(this, mVideoInfos, new SearchResultAdapter.VideoAlbumNavClickListener() {
|
@Override
|
public void onNav(int p) {
|
navClick(p);
|
}
|
});
|
loading = LayoutInflater.from(this).inflate(R.layout.item_loading, null);
|
loading.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
loading.setVisibility(View.GONE);
|
adapter.setFooterView(loading);
|
|
rv_video_search.setAdapter(adapter);
|
rv_video_search.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
@Override
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
super.onScrollStateChanged(recyclerView, newState);
|
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
int first = manager.findFirstVisibleItemPosition();
|
int last = manager.findLastVisibleItemPosition();
|
int total = manager.getItemCount();
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
if ((!isLoad) && last == total - 1 && mVideoInfos.size() < 1000) {
|
if (mVideoInfos != null && mVideoInfos.size() > 0 && !rl_search_result.isRefreshing()) {//列表中有数据才能加载更多
|
mCurrentPage++;
|
isLoad = true;
|
loading.setVisibility(View.VISIBLE);
|
search(mCurrentKey, mCurrentType);
|
}
|
}
|
}
|
}
|
|
@Override
|
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
|
super.onScrolled(recyclerView, dx, dy);
|
}
|
});
|
|
ProgressBar pb = new ProgressBar(SearchResultActivity.this);
|
rl_search_result.setRefreshing(true);
|
search(mCurrentKey, mCurrentType);
|
// loadAd();
|
}
|
|
private void startSearch() {
|
if (StringUtils.isEmpty(et_search_key.getEditableText()
|
.toString())) {
|
mCurrentKey = et_search_key.getText().toString();
|
} else {
|
mCurrentKey = et_search_key.getEditableText().toString();
|
}
|
|
SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this);
|
mCurrentPage = 1;
|
rl_search_result.setRefreshing(true);
|
//清除顶部分类
|
typeList = null;
|
search(mCurrentKey, mCurrentType);
|
}
|
|
|
private void initTopBar() {
|
int p = 0;
|
ll_top_bar.removeAllViews();
|
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
|
for (int i = 0; i < typeList.size(); i++) {
|
final View view = inflater.inflate(R.layout.item_search_result_top_bar_content, null);
|
TextView tv_name = view.findViewById(R.id.tv_name);
|
tv_name.setText(typeList.get(i).getName());
|
setTopBarSelect(tv_name, i == p);
|
ll_top_bar.addView(view);
|
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
|
params.rightMargin = DimenUtils.dip2px(getApplicationContext(), 17);
|
params.leftMargin = DimenUtils.dip2px(getApplicationContext(), 10);
|
view.setTag(i);
|
view.setOnClickListener(new OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
int p = Integer.parseInt(view.getTag() + "");
|
navClick(p);
|
}
|
});
|
}
|
}
|
|
private void navClick(int p) {
|
if (typeList == null || typeList.size() <= p)
|
return;
|
selectTopBar(p);
|
VideoType vt = typeList.get(p);
|
|
if (StringUtils.isBlank(mCurrentType) || !mCurrentType.equalsIgnoreCase(vt.getId())) {
|
mCurrentPage = 1;
|
mCurrentType = vt.getId();
|
rl_search_result.setRefreshing(true);
|
search(mCurrentKey, mCurrentType);
|
}
|
}
|
|
private void setTopBarSelect(TextView tv_name, boolean selected) {
|
if (selected) {
|
tv_name.setBackgroundResource(R.drawable.vpi__tab_selected_focused_holo);
|
tv_name.setTextSize(19);
|
tv_name.setTextColor(getResources().getColor(R.color.search_nav_highlight_text_color));
|
tv_name.setPadding(0, 0, 0, DimenUtils.dip2px(getApplicationContext(), 5));
|
tv_name.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
|
} else {
|
tv_name.setBackground(null);
|
tv_name.setTextSize(14);
|
tv_name.setTextColor(getResources().getColor(R.color.nav_text_color));
|
tv_name.setPadding(0, 0, 0, 0);
|
tv_name.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
|
}
|
}
|
|
private void selectTopBar(int p) {
|
for (int i = 0; i < ll_top_bar.getChildCount(); i++) {
|
TextView tv = ll_top_bar.getChildAt(i).findViewById(R.id.tv_name);
|
setTopBarSelect(tv, i == p);
|
}
|
}
|
|
|
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);
|
suggestFragment.setItemClickListener(new SearchSuggestFragment.OnItemClickListener() {
|
@Override
|
public void onClick(String st) {
|
mFirst = true;
|
hiddenSuggestFragment();
|
et_search_key.setText(st);
|
startSearch();
|
}
|
});
|
|
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 synchronized void fillAD(List<ExpressAdContainer> adList) {
|
|
//判断当前页面是否需要广告
|
int albumCount = 0;
|
if (mVideoInfos != null && mVideoInfos.size() > 0)
|
for (int i = 0; i < mVideoInfos.size(); i++) {
|
VideoContent vc = mVideoInfos.get(i);
|
if (vc.getType() == VideoContent.TYPE_AD) {
|
//更新广告
|
vc.setAd(adList.get(0));
|
adapter.notifyItemChanged(i);
|
return;
|
} else if (vc.getVideo().getShowType() == 1) {
|
albumCount++;
|
}
|
}
|
|
if (albumCount >= 2) {
|
VideoContent videoContent = VideoContent.createAdContent(adList.get(0));
|
mVideoInfos.add(2, videoContent);
|
adapter.notifyItemInserted(2);
|
}
|
|
}
|
|
//拉取2.0的自渲染广告
|
private void loadAd() {
|
String pid = GDTConstant.PID_2_SEARCH_RESULT_ALBUM;
|
NativeUnifiedAD mAdManager = new NativeUnifiedAD(this, pid, new NativeADUnifiedListener() {
|
|
@Override
|
public void onNoAD(AdError adError) {
|
|
}
|
|
@Override
|
public void onADLoaded(List<NativeUnifiedADData> list) {
|
if (list != null && list.size() > 0) {
|
List<ExpressAdContainer> adList = new ArrayList<>();
|
adList.add(new ExpressAdContainer(list.get(0)));
|
fillAD(adList);
|
}
|
}
|
});
|
mAdManager.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO);
|
mAdManager.loadData(1);
|
}
|
|
// private void loadAd() {
|
// ExpressAdContainer ad = SearchResultAdManager.getInstance(getApplicationContext()).consumeAD();
|
// if (ad == null) {
|
// SearchResultAdManager.getInstance(getApplicationContext()).loadAD(1, new ExpressAdManager.IAdLoadListener() {
|
// @Override
|
// public void onSuccess(List<ExpressAdContainer> adList) {
|
// fillAD(adList);
|
// }
|
// });
|
// } else {
|
// List<ExpressAdContainer> adList = new ArrayList<>();
|
// adList.add(ad);
|
// fillAD(adList);
|
// }
|
// //加载下一个需要的广告
|
// SearchResultAdManager.getInstance(getApplicationContext()).autoLoadAd();
|
// }
|
|
|
private void search(String key, String videoType) {
|
|
|
|
|
|
UmengEventUtil.search(getApplicationContext(), videoType);
|
|
SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
BeibeiVideoAPI.search(this, uid, key, videoType,
|
String.valueOf(mCurrentPage),
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
hiddenSuggestFragment();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
|
//挣影视豆
|
if (makeGoldCornRunnable == null)
|
makeGoldCornRunnable = GoldCornUtil.makeGoldCorn(getIntent(), et_search_key, null, null, 2, null);
|
|
if (jsonObject.getBoolean("IsPost")) {
|
|
JSONObject rootData = jsonObject.getJSONObject("Data");
|
List<VideoInfo> videoInfos = JsonUtil.videoGson.fromJson(
|
|
rootData.getJSONArray("data").toString(),
|
new TypeToken<List<VideoInfo>>() {
|
}.getType());
|
|
//设置导航栏
|
if (rootData.opt("typeList") != null) {
|
if (typeList == null) {
|
typeList = JsonUtil.videoGson.fromJson(
|
rootData
|
.getJSONArray("typeList").toString(),
|
new TypeToken<List<VideoType>>() {
|
}.getType());
|
initTopBar();
|
}
|
}
|
|
List<VideoContent> videoContentList = new ArrayList<>();
|
for (VideoInfo video : videoInfos) {
|
videoContentList.add(VideoContent.createVideoContent(video));
|
}
|
|
|
//记录当前的广告信息
|
VideoContent adContent = null;
|
int adContentP = -1;
|
if (mCurrentPage <= 1) {
|
for (int i = 0; i < mVideoInfos.size(); i++) {
|
if (mVideoInfos.get(i).getType() == VideoContent.TYPE_AD) {
|
adContent = mVideoInfos.get(i);
|
adContentP = i;
|
}
|
}
|
mVideoInfos.clear();
|
}
|
|
mVideoInfos.addAll(videoContentList);
|
if (adContent != null && mVideoInfos.size() >= adContentP) {
|
mVideoInfos.add(adContentP, adContent);
|
}
|
adapter.notifyDataSetChanged();
|
|
if (mVideoInfos != null && mVideoInfos.size() > 0) {
|
//有值
|
ll_empty.setVisibility(View.GONE);
|
rl_search_result.setVisibility(View.VISIBLE);
|
} else {
|
//无值
|
ll_empty.setVisibility(View.VISIBLE);
|
rl_search_result.setVisibility(View.GONE);
|
}
|
|
if (mCurrentPage == 1) {
|
loadAd();
|
}
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
isLoad = false;
|
rl_search_result.setRefreshing(false);
|
loading.setVisibility(View.GONE);
|
}
|
});
|
|
saveHistory(key);
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
public void onClick(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_top_bar_left:
|
finish();
|
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();
|
}
|
|
|
class MyLinearLayoutManager extends LinearLayoutManager {
|
|
public MyLinearLayoutManager(Context context) {
|
super(context);
|
}
|
|
@Override
|
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
|
try {
|
super.onLayoutChildren(recycler, state);
|
} catch (IndexOutOfBoundsException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
GoldCornUtil.removeCallbacks(et_search_key, makeGoldCornRunnable);
|
}
|
}
|