| | |
| | | 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.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.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.yeshi.buwanshequ.R; |
| | | 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 AutoCompleteTextView et_search_key; |
| | | private EditText et_search_key; |
| | | |
| | | private TextView tv_clear, tv_back; |
| | | private TextView tv_clear; |
| | | |
| | | private ImageView tv_search_cancel; |
| | | private TextView tv_search_cancel; |
| | | |
| | | private GridView gv_hot_search; |
| | | |
| | | private GridView gv_history_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); |
| | | /* |
| | | * 计算状态栏高度并设置 |
| | | */ |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| | | int result = 0; |
| | | int resourceId = getResources().getIdentifier("status_bar_height", |
| | | "dimen", "android"); |
| | | if (resourceId > 0) { |
| | | result = getResources().getDimensionPixelSize(resourceId); |
| | | } |
| | | LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, |
| | | result); |
| | | findViewById(R.id.v_status_bar).setLayoutParams(params); |
| | | } else { |
| | | findViewById(R.id.v_status_bar).setVisibility(View.GONE); |
| | | } |
| | | // findViewById(R.id.gv_video_guess_like).setVisibility(View.GONE); |
| | | 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); |
| | | fl_advertisement.setOnClickListener(this); |
| | | tv_search_cancel = (ImageView) findViewById(R.id.tv_search_cancel); |
| | | gv_hot_search = (GridView) findViewById(R.id.gv_hot_search); |
| | | gv_history_search = (GridView) findViewById(R.id.gv_history_search); |
| | | // gv_video_guess_like = (GridView) findViewById(R.id.gv_video_guess_like); |
| | | tv_clear = (TextView) (TextView) findViewById(R.id.tv_search_clear_his); |
| | | // iv_search_ad = (ImageView) findViewById(R.id.iv_search_ad);// 聚效广告 |
| | | // WindowManager wm = (WindowManager) SearchActivity.this |
| | | // .getSystemService(Context.WINDOW_SERVICE); |
| | | // int width = wm.getDefaultDisplay().getWidth(); |
| | | // int height = (int) (width * (9.0f / 15.56f)); |
| | | // LayoutParams params = iv_search_ad.getLayoutParams(); |
| | | // params.width = width; |
| | | // params.height = height; |
| | | // iv_search_ad.setLayoutParams(params); |
| | | 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) { |
| | | Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class); |
| | | |
| | | if (StringUtils.isEmpty(et_search_key.getEditableText() |
| | | .toString())) { |
| | | + "") && StringUtils.isEmpty(et_search_key.getHint() + "")) { |
| | | Toast.makeText(SearchActivity.this, "请输入搜索内容!", Toast.LENGTH_LONG).show(); |
| | | return false; |
| | | } else { |
| | | intent.putExtra("key", et_search_key.getEditableText() |
| | | .toString()); |
| | | if (!StringUtils.isEmpty(et_search_key.getEditableText() |
| | | + "")) { |
| | | jumpToSearch(et_search_key.getEditableText() |
| | | + ""); |
| | | } else { |
| | | jumpToSearch(et_search_key.getHint() + ""); |
| | | } |
| | | } |
| | | startActivity(intent); |
| | | |
| | | |
| | | return true; |
| | | } |
| | |
| | | |
| | | @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; |
| | |
| | | } |
| | | }); |
| | | |
| | | |
| | | gv_hot_search.setOnItemClickListener(new OnItemClickListener() { |
| | | |
| | | @Override |
| | | public void onItemClick(AdapterView<?> parent, View view, |
| | | int position, long id) { |
| | | Intent intent = new Intent(SearchActivity.this, |
| | | SearchResultActivity.class); |
| | | intent.putExtra("key", |
| | | (String) parent.getItemAtPosition(position)); |
| | | startActivity(intent); |
| | | } |
| | | }); |
| | | gv_history_search.setOnItemClickListener(new OnItemClickListener() { |
| | | |
| | | @Override |
| | | public void onItemClick(AdapterView<?> parent, View view, |
| | | int position, long id) { |
| | | Intent intent = new Intent(SearchActivity.this, |
| | | SearchResultActivity.class); |
| | | intent.putExtra("key", |
| | | (String) parent.getItemAtPosition(position)); |
| | | startActivity(intent); |
| | | 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(); |
| | | MobclickAgent.onPageStart("搜索页"); |
| | | getHistorySearch(); |
| | | } |
| | | |
| | | @Override |
| | | public void onPause() { |
| | | super.onPause(); |
| | | MobclickAgent.onPageEnd("搜索页"); |
| | | } |
| | | |
| | | @Override |
| | | public void onClick(final View v) { |
| | | switch (v.getId()) { |
| | | case R.id.tv_search_cancel: { |
| | | // if (tv_search_cancel.getText().toString() |
| | | // .equals(getResources().getString(R.string.search))) { |
| | | Intent intent = new Intent(this, SearchResultActivity.class); |
| | | if (StringUtils.isEmpty(et_search_key.getEditableText() |
| | | .toString()) && (et_search_key.getHint() == null || StringUtils.isEmpty(et_search_key.getHint().toString()))) { |
| | | Toast.makeText(SearchActivity.this, "请输入搜索内容!", Toast.LENGTH_LONG).show(); |
| | | return; |
| | | } else { |
| | | intent.putExtra("key", StringUtils.isEmpty(et_search_key.getEditableText() |
| | | .toString()) ? et_search_key.getHint().toString() : et_search_key.getEditableText() |
| | | .toString()); |
| | | } |
| | | startActivity(intent); |
| | | // } else { |
| | | // finish(); |
| | | // } |
| | | } |
| | | break; |
| | | 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; |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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", ""); |
| | |
| | | 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()); |
| | | ArrayAdapter<String> adapter = new ArrayAdapter<String>( |
| | | SearchActivity.this, |
| | | android.R.layout.simple_list_item_1, |
| | | 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(); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | |
| | | SharedPreferences historySp = getSharedPreferences("search_history", |
| | | Context.MODE_PRIVATE); |
| | | String historyStr = historySp.getString("history", ""); |
| | | String[] historyArray = historyStr.split(","); |
| | | List<String> history = new ArrayList<String>(); |
| | | for (int i = 0; i < 10 && i < historyArray.length; i++) { |
| | | if (!TextUtils.isEmpty(historyArray[historyArray.length - 1 - i])) { |
| | | history.add(historyArray[historyArray.length - 1 - i]); |
| | | 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); |
| | | } |
| | | gv_history_search.setAdapter(new GridSuggestionAdapter(history, false)); |
| | | } |
| | | |
| | | private void clearHistorySearch() { |
| | |
| | | .getJSONObject("Data").getJSONArray("data") |
| | | .toString(), new TypeToken<List<String>>() { |
| | | }.getType()); |
| | | gv_hot_search.setAdapter(new GridSuggestionAdapter( |
| | | 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); |
| | | } |
| | | } |
| | | }); |
| | |
| | | |
| | | // 1.加载广告,先设置加载上下文环境和条件 |
| | | private void initAdvertisement() { |
| | | NativeExpressAD nativeExpressAD = new NativeExpressAD(SearchActivity.this, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT), |
| | | BeibeiConstant.GDT_ID, BeibeiConstant.GDT_SEARCH_NATIVE, new NativeExpressAD.NativeExpressADListener() { |
| | | //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 onADLoaded(final List<NativeExpressADView> list) { |
| | | tv_back.post(new Runnable() { |
| | | public void run() { |
| | | 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 run() { |
| | | if (null != list && list.size() > 0) { |
| | | NativeExpressADView adView = list.get(0); |
| | | adView.render(); |
| | | FrameLayout parent = (FrameLayout) adView.getParent(); |
| | | if (parent != null) { |
| | | parent.removeAllViews(); |
| | | } |
| | | fl_advertisement.addView(adView); |
| | | public void onSuccess(List<ExpressAdContainer> adList) { |
| | | if (adList != null && adList.size() > 0) { |
| | | ExpressAdManager.renderAndFillAd(SearchActivity.this, adList.get(0), fl_advertisement, 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; |
| | | |
| | | new ExpressAdManager(newAdType, 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, new ExpressAdManager.IAdEventListener() { |
| | | @Override |
| | | public void closeAd(ExpressAdContainer ad) { |
| | | fl_advertisement.removeAllViews(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | @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) { |
| | | } |
| | | }); // 传入Activity |
| | | // 注意:如果您在联盟平台上新建原生模板广告位时,选择了支持视频,那么可以进行个性化设置(可选) |
| | | nativeExpressAD.setVideoOption(new VideoOption.Builder() |
| | | .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频 |
| | | .setAutoPlayMuted(true) // 自动播放时为静音 |
| | | .build()); // |
| | | nativeExpressAD.loadAD(2); |
| | | }); |
| | | } |
| | | |
| | | 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(); |
| | | |
| | | } |
| | | } |