From a62905c725c5ffd88f29dfac644591cde18751e1 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期四, 10 九月 2020 13:40:09 +0800 Subject: [PATCH] 广告优化 --- BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java | 157 +++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 112 insertions(+), 45 deletions(-) diff --git a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java index 14b054f..f9ce3da 100644 --- a/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java +++ b/BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java @@ -3,6 +3,7 @@ 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; @@ -24,7 +25,11 @@ 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; @@ -40,10 +45,12 @@ 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; @@ -87,6 +94,10 @@ private View loading; + private LinearLayout ll_top_bar; + + List<VideoType> typeList; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -113,9 +124,8 @@ 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); - 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() { @@ -157,6 +167,8 @@ mCurrentKey = parent.getItemAtPosition(position).toString(); SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); mCurrentPage = 1; + //娓呴櫎椤堕儴鍒嗙被 + typeList = null; search(mCurrentKey, mCurrentType); } }); @@ -199,6 +211,8 @@ SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this); mCurrentPage = 1; rl_search_result.setRefreshing(true); + //娓呴櫎椤堕儴鍒嗙被 + typeList = null; search(mCurrentKey, mCurrentType); return true; } @@ -222,7 +236,7 @@ }); - rv_video_search.setLayoutManager(new LinearLayoutManager(getApplicationContext())); + 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); @@ -244,10 +258,12 @@ 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); + } } } } @@ -262,6 +278,59 @@ 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) { @@ -283,10 +352,7 @@ .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(); } @@ -354,11 +420,24 @@ 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) { @@ -373,8 +452,9 @@ videoContentList.add(adPosition, VideoContent.createAdContent(adList.get(0))); adList.remove(0); } - if (mCurrentPage <= 1) + if (mCurrentPage <= 1) { mVideoInfos.clear(); + } mVideoInfos.addAll(videoContentList); adapter.notifyDataSetChanged(); @@ -419,36 +499,6 @@ 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; } @@ -479,4 +529,21 @@ .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(); + } + } + } } -- Gitblit v1.8.0