| | |
| | | package com.weikou.beibeivideo.ui.recommend; |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.SharedPreferences; |
| | | import android.graphics.Color; |
| | | import android.graphics.drawable.Drawable; |
| | | 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.Filter; |
| | | import android.widget.Filterable; |
| | | import android.widget.ImageView; |
| | | import android.widget.LinearLayout; |
| | | import android.widget.LinearLayout.LayoutParams; |
| | | import android.widget.ProgressBar; |
| | | import android.widget.TextView; |
| | | import android.widget.Toast; |
| | | |
| | | import com.google.gson.FieldNamingPolicy; |
| | | import com.google.gson.Gson; |
| | |
| | | import com.weikou.beibeivideo.BasicTextHttpResponseHandler; |
| | | import com.weikou.beibeivideo.BeibeiVideoAPI; |
| | | 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.ui.video.SuggestKeysAdapter; |
| | | import com.weikou.beibeivideo.ui.video.VideoCloumn1Adapter; |
| | | import com.weikou.beibeivideo.util.DimenUtils; |
| | | import com.weikou.beibeivideo.util.ad.ExpressAdManager; |
| | | import com.weikou.beibeivideo.util.ad.manager.SearchResultAdManager; |
| | | import com.weikou.beibeivideo.util.ui.DividerItemDecoration; |
| | | import com.yeshi.buwanshequ.R; |
| | | import com.weikou.beibeivideo.R; |
| | | |
| | | import org.apache.http.Header; |
| | | import org.json.JSONObject; |
| | |
| | | |
| | | private AutoCompleteTextView et_search_key; |
| | | |
| | | private TextView tv_back; |
| | | |
| | | private ImageView tv_search_cancel; |
| | | private TextView tv_search_cancel; |
| | | |
| | | private RecyclerView rv_video_search; |
| | | |
| | | private List<VideoContent> mVideoInfos; |
| | | |
| | | private VideoCloumn1Adapter adapter; |
| | | private SearchResultAdapter adapter; |
| | | |
| | | |
| | | private int mCurrentPage = 1; |
| | |
| | | |
| | | private View loading; |
| | | |
| | | private void initExpressAd() { |
| | | if (expressAdManager == null) |
| | | expressAdManager = new ExpressAdManager(ExpressAdManager.SOURCE_GDT, getApplicationContext()); |
| | | } |
| | | private LinearLayout ll_top_bar; |
| | | |
| | | List<VideoType> typeList; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.search_result_activity); |
| | | loadAd(); |
| | | |
| | | /* |
| | | * 计算状态栏高度并设置 |
| | | */ |
| | |
| | | |
| | | rl_search_result = findViewById(R.id.rl_search_result); |
| | | et_search_key = findViewById(R.id.et_search_key); |
| | | tv_back = findViewById(R.id.tv_top_bar_left); |
| | | tv_back.setOnClickListener(this); |
| | | tv_search_cancel = findViewById(R.id.tv_search_cancel); |
| | | rv_video_search = findViewById(R.id.rv_video_search); |
| | | findViewById(R.id.tv_moive_all).setOnClickListener(this); |
| | | findViewById(R.id.tv_moive).setOnClickListener(this); |
| | | findViewById(R.id.tv_moive_special).setOnClickListener(this); |
| | | ll_top_bar = findViewById(R.id.ll_top_bar); |
| | | |
| | | tv_search_cancel.setOnClickListener(this); |
| | | et_search_key.addTextChangedListener(new TextWatcher() { |
| | | |
| | |
| | | mCurrentKey = parent.getItemAtPosition(position).toString(); |
| | | SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); |
| | | mCurrentPage = 1; |
| | | //清除顶部分类 |
| | | typeList = null; |
| | | search(mCurrentKey, mCurrentType); |
| | | } |
| | | }); |
| | |
| | | }); |
| | | |
| | | |
| | | 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() |
| | | .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); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | }); |
| | | |
| | | |
| | | mCurrentKey = getIntent().getStringExtra("key"); |
| | | et_search_key.setText(mCurrentKey); |
| | | et_search_key.setSelection(mCurrentKey.length()); |
| | |
| | | } |
| | | }); |
| | | |
| | | rv_video_search.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | rv_video_search.setLayoutManager(new MyLinearLayoutManager(getApplicationContext())); |
| | | DividerItemDecoration decoration = new DividerItemDecoration(); |
| | | decoration.setSize(DimenUtils.dip2px(rv_video_search.getContext(), 6)); |
| | | rv_video_search.addItemDecoration(decoration); |
| | | |
| | | adapter = new VideoCloumn1Adapter(this, mVideoInfos); |
| | | loading = LayoutInflater.from(this).inflate(R.layout.item_loading,null); |
| | | loading.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); |
| | | adapter = new SearchResultAdapter(this, mVideoInfos); |
| | | 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); |
| | | |
| | |
| | | int total = manager.getItemCount(); |
| | | if (newState == RecyclerView.SCROLL_STATE_IDLE) { |
| | | if ((!isLoad) && last == total - 1 && mVideoInfos.size() < 1000) { |
| | | mCurrentPage++; |
| | | isLoad = true; |
| | | loading.setVisibility(View.VISIBLE); |
| | | search(mCurrentKey, mCurrentType); |
| | | if (mVideoInfos != null && mVideoInfos.size() > 0 && !rl_search_result.isRefreshing()) {//列表中有数据才能加载更多 |
| | | mCurrentPage++; |
| | | isLoad = true; |
| | | loading.setVisibility(View.VISIBLE); |
| | | search(mCurrentKey, mCurrentType); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | }); |
| | | |
| | | |
| | | ProgressBar pb = new ProgressBar(SearchResultActivity.this); |
| | | rl_search_result.setRefreshing(true); |
| | | search(mCurrentKey, mCurrentType); |
| | | // loadAd(); |
| | | } |
| | | |
| | | private void initTopBar(List<VideoType> videoTypeList) { |
| | | int p = 0; |
| | | ll_top_bar.removeAllViews(); |
| | | LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); |
| | | for (int i = 0; i < videoTypeList.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(videoTypeList.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() + ""); |
| | | selectTopBar(p); |
| | | VideoType vt = videoTypeList.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(17); |
| | | tv_name.setTextColor(Color.parseColor("#FFFFFF")); |
| | | tv_name.setPadding(0, 0, 0, DimenUtils.dip2px(getApplicationContext(), 5)); |
| | | } else { |
| | | tv_name.setBackground(null); |
| | | tv_name.setTextSize(12); |
| | | tv_name.setTextColor(Color.parseColor("#999999")); |
| | | tv_name.setPadding(0, 0, 0, 0); |
| | | } |
| | | } |
| | | |
| | | 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 suggestSearch(String key) { |
| | |
| | | .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); |
| | | SuggestKeysAdapter adapter = new SuggestKeysAdapter(getApplicationContext(), results); |
| | | et_search_key.setAdapter(adapter); |
| | | adapter.notifyDataSetChanged(); |
| | | } |
| | |
| | | }); |
| | | } |
| | | |
| | | private void loadAd() { |
| | | initExpressAd(); |
| | | expressAdManager.loadSearchResultSmallAd(2, new ExpressAdManager.IAdLoadListener() { |
| | | @Override |
| | | public void onSuccess(List<ExpressAdContainer> adList) { |
| | | if (adList != null) { |
| | | SearchResultActivity.this.adList.addAll(adList); |
| | | } |
| | | private void fillAD(List<ExpressAdContainer> adList) { |
| | | if (adList != null) { |
| | | SearchResultActivity.this.adList.addAll(adList); |
| | | } |
| | | |
| | | if (adList.size() > 0) { |
| | | boolean hasAd = false; |
| | | if (mVideoInfos != null && mVideoInfos.size() > 0) |
| | | for (VideoContent vc : mVideoInfos) { |
| | | if (vc.getType() == VideoContent.TYPE_AD) { |
| | | hasAd = true; |
| | | break; |
| | | } |
| | | } |
| | | //不存在广告 |
| | | if (!hasAd) { |
| | | mVideoInfos.add(0, VideoContent.createAdContent(SearchResultActivity.this.adList.get(0))); |
| | | SearchResultActivity.this.adList.remove(0); |
| | | adapter.notifyDataSetChanged(); |
| | | if (adList.size() > 0) { |
| | | boolean hasAd = false; |
| | | if (mVideoInfos != null && mVideoInfos.size() > 0) |
| | | for (VideoContent vc : mVideoInfos) { |
| | | if (vc.getType() == VideoContent.TYPE_AD) { |
| | | hasAd = true; |
| | | break; |
| | | } |
| | | } |
| | | //不存在广告 |
| | | if (!hasAd) { |
| | | mVideoInfos.add(0, VideoContent.createAdContent(SearchResultActivity.this.adList.get(0))); |
| | | SearchResultActivity.this.adList.remove(0); |
| | | adapter.notifyDataSetChanged(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |
| | | |
| | | 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 List<ExpressAdContainer> adList = new ArrayList<>(); |
| | |
| | | Gson gson = new GsonBuilder().setFieldNamingPolicy( |
| | | FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | JSONObject rootData = jsonObject.getJSONObject("Data"); |
| | | List<VideoInfo> videoInfos = gson.fromJson( |
| | | jsonObject.getJSONObject("Data") |
| | | .getJSONArray("data").toString(), |
| | | |
| | | rootData.getJSONArray("data").toString(), |
| | | new TypeToken<List<VideoInfo>>() { |
| | | }.getType()); |
| | | |
| | | //设置导航栏 |
| | | if (rootData.opt("typeList") != null) { |
| | | if (typeList == null) { |
| | | typeList = gson.fromJson( |
| | | rootData |
| | | .getJSONArray("typeList").toString(), |
| | | new TypeToken<List<VideoType>>() { |
| | | }.getType()); |
| | | initTopBar(typeList); |
| | | } |
| | | } |
| | | |
| | | List<VideoContent> videoContentList = new ArrayList<>(); |
| | | for (VideoInfo video : videoInfos) { |
| | |
| | | videoContentList.add(adPosition, VideoContent.createAdContent(adList.get(0))); |
| | | adList.remove(0); |
| | | } |
| | | if (mCurrentPage <= 1) |
| | | if (mCurrentPage <= 1) { |
| | | mVideoInfos.clear(); |
| | | } |
| | | mVideoInfos.addAll(videoContentList); |
| | | adapter.notifyDataSetChanged(); |
| | | |
| | |
| | | public void onClick(View v) { |
| | | switch (v.getId()) { |
| | | case R.id.tv_search_cancel: { |
| | | if (StringUtils.isEmpty(et_search_key.getEditableText() |
| | | .toString())) { |
| | | mCurrentKey = et_search_key.getText().toString(); |
| | | } else { |
| | | mCurrentKey = et_search_key.getEditableText().toString(); |
| | | } |
| | | SoftKeyboardUtils.hideSoftInput(this); |
| | | mCurrentPage = 1; |
| | | finish(); |
| | | } |
| | | break; |
| | | case R.id.tv_top_bar_left: |
| | | finish(); |
| | | break; |
| | | case R.id.tv_moive_all: |
| | | rl_search_result.setRefreshing(true); |
| | | mVideoInfos.clear(); |
| | | mCurrentPage = 1; |
| | | mCurrentType = "0"; |
| | | findViewById(R.id.tv_moive_all).setBackgroundResource(R.drawable.ic_select2); |
| | | findViewById(R.id.tv_moive).setBackgroundResource(R.drawable.ic_select1); |
| | | findViewById(R.id.tv_moive_special).setBackgroundResource(R.drawable.ic_select1); |
| | | search(mCurrentKey, mCurrentType); |
| | | break; |
| | | case R.id.tv_moive: |
| | | mVideoInfos.clear(); |
| | | rl_search_result.setRefreshing(true); |
| | | mCurrentPage = 1; |
| | | mCurrentType = "1"; |
| | | findViewById(R.id.tv_moive_all).setBackgroundResource(R.drawable.ic_select1); |
| | | findViewById(R.id.tv_moive).setBackgroundResource(R.drawable.ic_select2); |
| | | findViewById(R.id.tv_moive_special).setBackgroundResource(R.drawable.ic_select1); |
| | | search(mCurrentKey, mCurrentType); |
| | | break; |
| | | case R.id.tv_moive_special: |
| | | mVideoInfos.clear(); |
| | | rl_search_result.setRefreshing(true); |
| | | mCurrentPage = 1; |
| | | mCurrentType = "2"; |
| | | findViewById(R.id.tv_moive_all).setBackgroundResource(R.drawable.ic_select1); |
| | | findViewById(R.id.tv_moive).setBackgroundResource(R.drawable.ic_select1); |
| | | findViewById(R.id.tv_moive_special).setBackgroundResource(R.drawable.ic_select2); |
| | | search(mCurrentKey, mCurrentType); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | .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(); |
| | | } |
| | | } |
| | | } |
| | | } |