admin
2020-08-12 cefe2a41db4a275fb1e940a902cb156f1ed68d80
BuWanVideo/src/com/weikou/beibeivideo/ui/recommend/SearchResultActivity.java
@@ -1,27 +1,28 @@
package com.weikou.beibeivideo.ui.recommend;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
@@ -29,22 +30,19 @@
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.SoftKeyboardUtils;
import com.lcjian.library.util.common.StringUtils;
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.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.entity.VideoInfo;
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.discover.StarWorksAdapter;
import com.weikou.beibeivideo.ui.media.VideoDetailActivity;
import com.weikou.beibeivideo.util.BeibeiConstant;
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.ui.DividerItemDecoration;
import com.yeshi.buwanshequ.R;
import org.apache.http.Header;
@@ -62,11 +60,12 @@
    private ImageView tv_search_cancel;
    private ListView lv_video_search;
    private RecyclerView rv_video_search;
    private StarWorksAdapter adapter;
    private List<VideoContent> mVideoInfos;
    private List<VideoInfo> mVideoInfos;
    private VideoCloumn1Adapter adapter;
    private int mCurrentPage = 1;
@@ -76,14 +75,24 @@
    private boolean mFirst = true;
    private RefreshLayout rl_search_result;
    private SwipeRefreshLayout rl_search_result;
    // private ImageView iv_search_result_ad;// 添加聚效广告图片
    private ExpressAdManager expressAdManager;
    private boolean isLoad;
    private View loading;
    private void initExpressAd() {
        if (expressAdManager == null)
            expressAdManager = new ExpressAdManager(ExpressAdManager.SOURCE_GDT, getApplicationContext());
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result_activity);
        loadAd();
        /*
         * 计算状态栏高度并设置
         */
@@ -101,12 +110,12 @@
            findViewById(R.id.v_status_bar).setVisibility(View.GONE);
        }
        rl_search_result = (RefreshLayout) findViewById(R.id.rl_search_result);
        et_search_key = (AutoCompleteTextView) findViewById(R.id.et_search_key);
        tv_back = (TextView) findViewById(R.id.tv_top_bar_left);
        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 = (ImageView) findViewById(R.id.tv_search_cancel);
        lv_video_search = (ListView) findViewById(R.id.gv_video_search);
        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);
@@ -136,11 +145,9 @@
                    }
                    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);
                }
            }
        });
@@ -153,7 +160,7 @@
                mCurrentKey = parent.getItemAtPosition(position).toString();
                SoftKeyboardUtils.hideSoftInput(SearchResultActivity.this);
                mCurrentPage = 1;
                refreshAd();
                search(mCurrentKey, mCurrentType);
            }
        });
@@ -187,53 +194,55 @@
        et_search_key.setSelection(mCurrentKey.length());
        // et_search_key.setText(mCurrentKey);
        mVideoInfos = new ArrayList<VideoInfo>();
        mVideoInfos = new ArrayList<>();
        rl_search_result.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                refreshAd();
            }
        });
        rl_search_result.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                mCurrentPage++;
                refreshAd();
                search(mCurrentKey, mCurrentType);
            }
        });
        lv_video_search.setOnItemClickListener(new OnItemClickListener() {
        rv_video_search.setLayoutManager(new LinearLayoutManager(this));
        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));
        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) {
                        mCurrentPage++;
                        isLoad = true;
                        loading.setVisibility(View.VISIBLE);
                        search(mCurrentKey, mCurrentType);
                    }
                }
            }
            @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);
            public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        lv_video_search.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });
        lv_video_search.addHeaderView(new View(this));
        adapter = new StarWorksAdapter(mVideoInfos, getApplicationContext());
        lv_video_search.setAdapter(adapter);
        ProgressBar pb = new ProgressBar(SearchResultActivity.this);
        rl_search_result.setFooter(pb);
        refreshAd();
        rl_search_result.setRefreshing(true);
        search(mCurrentKey, mCurrentType);
    }
    private void suggestSearch(String key) {
@@ -266,6 +275,37 @@
                });
    }
    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);
                }
                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 List<ExpressAdContainer> adList = new ArrayList<>();
    private void search(String key, String videoType) {
        SharedPreferences preferences = getSharedPreferences("user",
                Context.MODE_PRIVATE);
@@ -288,16 +328,37 @@
                                    new TypeToken<List<VideoInfo>>() {
                                    }.getType());
                            mVideoInfos.addAll(videoInfos);
                            if (mVideoInfos.size() > 0)
                                adapter.notifyDataSetChanged();
                            List<VideoContent> videoContentList = new ArrayList<>();
                            for (VideoInfo video : videoInfos) {
                                videoContentList.add(VideoContent.createVideoContent(video));
                            }
                            if (adList.size() > 0) {
                                int adPosition = (int) (videoContentList.size() * Math.random());
                                if (mCurrentPage == 1)
                                    adPosition = 0;
                                videoContentList.add(adPosition, VideoContent.createAdContent(adList.get(0)));
                                adList.remove(0);
                            }
                            if (mCurrentPage <= 1)
                                mVideoInfos.clear();
                            mVideoInfos.addAll(videoContentList);
                            adapter.notifyDataSetChanged();
                            //加载下一次要用的广告
                            if (videoInfos.size() > 0) {
                                if (adList.size() < 1)
                                    loadAd();
                            }
                        }
                    }
                    @Override
                    public void onFinish() {
                        isLoad = false;
                        rl_search_result.setRefreshing(false);
                        rl_search_result.setLoading(false);
                        loading.setVisibility(View.GONE);
                    }
                });
@@ -328,7 +389,6 @@
                }
                SoftKeyboardUtils.hideSoftInput(this);
                mCurrentPage = 1;
                refreshAd();
            }
            break;
            case R.id.tv_top_bar_left:
@@ -337,14 +397,12 @@
            case R.id.tv_moive_all:
                rl_search_result.setRefreshing(true);
                mVideoInfos.clear();
//                adapter.notifyDataSetChanged();
                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);
                refreshAd();
                search(mCurrentKey, mCurrentType);
                break;
            case R.id.tv_moive:
                mVideoInfos.clear();
@@ -354,7 +412,7 @@
                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);
                refreshAd();
                search(mCurrentKey, mCurrentType);
                break;
            case R.id.tv_moive_special:
                mVideoInfos.clear();
@@ -364,13 +422,8 @@
                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);
                refreshAd();
                search(mCurrentKey, mCurrentType);
                break;
            // case R.id.iv_search_result_ad:// 聚效广告点击事件处理
            // if (list.size() > 0) {
            // list.get(0).onAdClicked();
            // }
            // break;
            default:
                break;
        }
@@ -400,87 +453,5 @@
                .edit()
                .putString("history",
                        builder.substring(0, builder.length() - 1)).commit();
    }
    // 1.加载广告,先设置加载上下文环境和条件
    private void refreshAd() {
        if (mCurrentPage == 1) {
            mVideoInfos.clear();
        }
        NativeExpressAD nativeExpressAD = new NativeExpressAD(SearchResultActivity.this, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
                BeibeiConstant.GDT_ID, BeibeiConstant.GDT_SEARCH_RESULT_MIN_NATIVE, 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);
    }
}