| | |
| | | import android.app.Activity; |
| | | import android.content.Intent; |
| | | import android.support.v7.widget.RecyclerView; |
| | | import android.text.SpannableString; |
| | | import android.text.Spanned; |
| | | import android.text.style.AbsoluteSizeSpan; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | |
| | | import com.google.gson.FieldNamingPolicy; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import com.lcjian.library.util.common.DimenUtils; |
| | | import com.lcjian.library.util.common.StringUtils; |
| | | import com.lcjian.library.widget.RatioLayout; |
| | | import com.weikou.beibeivideo.BasicTextHttpResponseHandler; |
| | | import com.weikou.beibeivideo.BeibeiVideoAPI; |
| | | import com.weikou.beibeivideo.R; |
| | | import com.weikou.beibeivideo.entity.HomeType; |
| | | import com.weikou.beibeivideo.entity.HomeVideo; |
| | |
| | | import com.weikou.beibeivideo.util.VideoUtil; |
| | | import com.weikou.beibeivideo.util.ad.AdUtil; |
| | | import com.weikou.beibeivideo.util.ad.ExpressAdManager; |
| | | import com.weikou.beibeivideo.util.video.VideoUIUtil; |
| | | |
| | | import org.apache.http.Header; |
| | | import org.json.JSONArray; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.util.ArrayList; |
| | |
| | | private TreeSet mADSet = new TreeSet(); |
| | | |
| | | public final int TYPE_HEADER = 10001; |
| | | |
| | | private Gson gson = new GsonBuilder().setVersion(1.0).setFieldNamingPolicy( |
| | | FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | |
| | | private View headerView; |
| | | |
| | |
| | | homeType.setVideoInfoList(new ArrayList<>()); |
| | | } |
| | | |
| | | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| | | holder.view.setLayoutParams(params); |
| | | //是否为广告 |
| | | if (homeType.getAd() != null) { |
| | | holder.fl_ad.setVisibility(View.VISIBLE); |
| | |
| | | @Override |
| | | public void onClick(View v) { |
| | | |
| | | JumpActivityUtil.jumpPage(mContext,homeType.getAd().getJumpDetail(),homeType.getAd().getParams()!=null? JSONObject.parseObject(homeType.getAd().getParams().replace( |
| | | "\\" + "\"", "\"")):null); |
| | | JumpActivityUtil.jumpPage(mContext, homeType.getAd().getJumpDetail(), homeType.getAd().getParams() != null ? JSONObject.parseObject(homeType.getAd().getParams().replace( |
| | | "\\" + "\"", "\"")) : null); |
| | | } |
| | | }); |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | Glide.with(mContext).load(homeType.getIcon()) |
| | | .apply(new RequestOptions().placeholder(R.drawable.ic_stars_collection)).into(holder.tv_category_icon); |
| | | holder.tv_category_name.setText(homeType.getName()); |
| | | |
| | | |
| | | //更多 |
| | | if (Boolean.parseBoolean(homeType.getHasMore())) { |
| | | holder.tv_more.setVisibility(View.VISIBLE); |
| | | if (!StringUtils.isEmpty(homeType.getMoreTag())) { |
| | | holder.tv_more.setText(homeType.getMoreTag()); |
| | | } else { |
| | | holder.tv_more.setText("更多" + homeType.getName()); |
| | | } |
| | | } else { |
| | | holder.tv_more.setVisibility(View.GONE); |
| | | } |
| | | |
| | | int refreshP = homeType.getRefreshPosition(); |
| | | //右上角且有更多选项 |
| | | if (refreshP == 1 && Boolean.parseBoolean(homeType.getHasMore())) { |
| | | refreshP = 2;//移动至下方 |
| | | } |
| | | |
| | | //不足一页不显示刷新 |
| | | if (homeType.getCount() <= homeType.getPageSize()) { |
| | | refreshP = 0; |
| | | } |
| | | |
| | | |
| | | switch (refreshP) { |
| | | case 0: |
| | | holder.tv_refresh.setVisibility(View.GONE); |
| | | holder.tv_tag_refresh.setVisibility(View.GONE); |
| | | break; |
| | | |
| | | case 1: |
| | | holder.tv_refresh.setVisibility(View.GONE); |
| | | holder.tv_tag_refresh.setVisibility(View.VISIBLE); |
| | | break; |
| | | |
| | | case 2: |
| | | holder.tv_refresh.setVisibility(View.VISIBLE); |
| | | holder.tv_tag_refresh.setVisibility(View.GONE); |
| | | break; |
| | | } |
| | | //刷新的动作 |
| | | View.OnClickListener refreshListener = new View.OnClickListener() { |
| | | |
| | | |
| | | @Override |
| | | public void onClick(View v) { |
| | | BeibeiVideoAPI.getHomeVideoList(mContext, homeType.getId(), homeType.getCurrentPage() + 1, new BasicTextHttpResponseHandler() { |
| | | @Override |
| | | public void onSuccessPerfect(int statusCode, Header[] headers, org.json.JSONObject jsonObject) throws Exception { |
| | | if (jsonObject.optBoolean("IsPost")) { |
| | | org.json.JSONObject data = jsonObject.optJSONObject("Data"); |
| | | int page = data.optInt("page"); |
| | | homeType.setCurrentPage(page); |
| | | //解析数据 |
| | | homeType.getHomeVideoList().clear(); |
| | | //解析HomeVideo数据 |
| | | int count = data.optInt("count"); |
| | | homeType.setCount(count); |
| | | |
| | | JSONArray homeArray = data |
| | | .optJSONArray("list"); |
| | | List<HomeVideo> homeList = new ArrayList<>(); |
| | | if (homeArray != null && homeArray.length() > 0) { |
| | | for (int j = 0; j < homeArray.length(); j++) { |
| | | homeList.add(gson.fromJson(homeArray |
| | | .optJSONObject(j).toString(), |
| | | HomeVideo.class)); |
| | | } |
| | | } |
| | | |
| | | homeType.getHomeVideoList().clear(); |
| | | homeType.getHomeVideoList().addAll(homeList); |
| | | //改变当前选项 |
| | | notifyItemChanged(i); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | }; |
| | | holder.tv_refresh.setOnClickListener(refreshListener); |
| | | holder.tv_tag_refresh.setOnClickListener(refreshListener); |
| | | |
| | | |
| | | holder.tv_more |
| | | .setOnClickListener(new OnClickListener() { |
| | |
| | | FrameLayout fl_ad; |
| | | RatioLayout rl_ad; |
| | | TextView tv_category_name; |
| | | ImageView tv_category_icon; |
| | | ImageView iv_ad; |
| | | TextView tv_more; |
| | | RecyclerView rv_content; |
| | | RelativeLayout rl_title_item; |
| | | TextView tv_refresh; |
| | | TextView tv_tag_refresh; |
| | | View view; |
| | | |
| | | public HomeTypeHolder(View convertView, RecyclerView.RecycledViewPool recycledViewPool) { |
| | |
| | | |
| | | tv_category_name = convertView |
| | | .findViewById(R.id.tv_category_name); |
| | | tv_category_icon = convertView |
| | | .findViewById(R.id.tv_category_icon); |
| | | tv_more = convertView |
| | | .findViewById(R.id.tv_more); |
| | | rv_content = convertView |
| | | .findViewById(R.id.rv_content); |
| | | |
| | | tv_refresh = convertView |
| | | .findViewById(R.id.tv_refresh); |
| | | |
| | | tv_tag_refresh = convertView |
| | | .findViewById(R.id.tv_tag_refresh); |
| | | |
| | | |
| | | if (recycledViewPool != null) |
| | |
| | | ll_container = view.findViewById(R.id.ll_container); |
| | | fl_ad = view.findViewById(R.id.fl_ad); |
| | | rl_title_item = view.findViewById(R.id.rl_title_item); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | public class HomeTypeContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| | | private Activity mContext; |
| | | private List<HomeTypeContent> contentList; |
| | | private int maxItemNum; |
| | | |
| | | private ExpressAdManager expressAdManager; |
| | | |
| | | private boolean needAd; |
| | | |
| | | |
| | | public HomeTypeContentAdapter(final Activity context, List<HomeVideo> videoList, boolean needAd, int maxItemNum) { |
| | | this.needAd = needAd; |
| | | this.mContext = context; |
| | | if (contentList == null) |
| | | this.contentList = new ArrayList<>(); |
| | | this.contentList.clear(); |
| | | for (HomeVideo hv : videoList) { |
| | | this.contentList.add(new HomeTypeContent(HomeTypeContent.TYPE_CONTENT, hv)); |
| | | } |
| | | |
| | | |
| | | if (needAd) { |
| | | if (expressAdManager == null) |
| | | expressAdManager = new ExpressAdManager(AdUtil.getSmallExpressAdType(), mContext); |
| | | expressAdManager.loadRecommendSmallAd(new ExpressAdManager.IAdLoadListener() { |
| | | @Override |
| | | public void onSuccess(List<ExpressAdContainer> adList) { |
| | | if (adList != null && adList.size() > 0) { |
| | | if (contentList != null && contentList.size() > 0 && contentList.get(0).getType() == HomeTypeContent.TYPE_CONTENT) { |
| | | contentList.add(0, new HomeTypeContent(HomeTypeContent.TYPE_AD, adList.get(0))); |
| | | notifyDataSetChanged(); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | this.maxItemNum = maxItemNum; |
| | | } |
| | | |
| | | @Override |
| | | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) { |
| | | switch (type) { |
| | | case HomeTypeContent.TYPE_AD: |
| | | return new RecommendVideoAdHolder(inflater.inflate(R.layout.item_ad_list_small, parent, false)); |
| | | case HomeTypeContent.TYPE_CONTENT: |
| | | return new VideoHolder(inflater.inflate(R.layout.item_movie, parent, false)); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int p) { |
| | | final HomeTypeContent content = contentList.get(p); |
| | | if (viewHolder instanceof VideoHolder) { |
| | | VideoHolder holder = (VideoHolder) viewHolder; |
| | | final HomeVideo info = content.getHomeVideo(); |
| | | //设置内容 |
| | | holder.ll_video_show.setVisibility(View.VISIBLE); |
| | | if (StringUtils.isEmpty(info.getPicture())) { |
| | | Glide.with(mContext).load(info.getVideo().getPicture()) |
| | | .apply(new RequestOptions().centerCrop().placeholder(R.drawable.ic_default_cover).error(R.drawable.ic_default_cover)) |
| | | .into(holder.iv_movie_cover); |
| | | } else { |
| | | try { |
| | | Glide.with(mContext).load(info.getPicture()) |
| | | .apply(new RequestOptions().centerCrop().placeholder(R.drawable.ic_default_cover).error(R.drawable.ic_default_cover)) |
| | | .into(holder.iv_movie_cover); |
| | | } catch (IllegalArgumentException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | holder.tv_movie_title.setText(info.getVideo().getName()); |
| | | |
| | | String tag = StringUtils.isBlank(info |
| | | .getVideo().getTag()) ? "" : info.getVideo() |
| | | .getTag(); |
| | | holder.tv_movie_rating.setText(tag); |
| | | if (StringUtils.isBlank(tag)) { |
| | | holder.tv_movie_rating.setVisibility(View.GONE); |
| | | } else { |
| | | holder.tv_movie_rating.setVisibility(View.VISIBLE); |
| | | } |
| | | |
| | | //vip标识 |
| | | if (info.getVideo() != null && info.getVideo().getFree() == 1) { |
| | | holder.iv_vip.setVisibility(View.VISIBLE); |
| | | } else { |
| | | holder.iv_vip.setVisibility(View.GONE); |
| | | } |
| | | |
| | | |
| | | DecimalFormat df = new DecimalFormat("###.0"); |
| | | holder.tv_movie_play_num.setText(VideoUtil.getWatchCountShortName(info.getVideo().getWatchCount())); |
| | | holder.tv_movie_play_num.setVisibility(View.GONE); |
| | | holder.tv_movie_comment_num.setText(StringUtils.isBlank(info.getVideo().getCommentCount()) ? "0" : info.getVideo().getCommentCount()); |
| | | holder.ll_video_show |
| | | .setOnClickListener(new OnClickListener() { |
| | | @Override |
| | | public void onClick(final View v) { |
| | | //跳转到商品视频播放页 |
| | | myClick(info.getVideo(), v); |
| | | } |
| | | }); |
| | | } else if (viewHolder instanceof RecommendVideoAdHolder) {//广告 |
| | | final RecommendVideoAdHolder holder = (RecommendVideoAdHolder) viewHolder; |
| | | holder.fl_container.removeAllViews(); |
| | | |
| | | final ExpressAdContainer ad = content.getAd(); |
| | | if (ad == null) {//广告为空 |
| | | holder.view.setVisibility(View.GONE); |
| | | } else { |
| | | holder.view.setVisibility(View.VISIBLE); |
| | | if (ad.getGdt() != null) { |
| | | holder.rl_container.setRatio(0.88f); |
| | | holder.tv_movie_title.setVisibility(View.GONE); |
| | | // ad.getGdt().setAdEventListener(new AdEventListener() { |
| | | // @Override |
| | | // public void onClick() { |
| | | // |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onExposed() { |
| | | // |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onRenderSuccess() { |
| | | // holder.fl_container.addView(ad.getGdt().getAdView()); |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onRenderFail() { |
| | | // |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onAdClosed() { |
| | | // ad.getGdt().destroy(); |
| | | // contentList.remove(content); |
| | | // notifyDataSetChanged(); |
| | | // } |
| | | // }); |
| | | ad.getGdt().render(); |
| | | holder.fl_container.addView(ad.getGdt()); |
| | | holder.tv_movie_play_num.setText("112"); |
| | | } else if (ad.getCsj() != null) { |
| | | holder.rl_container.setRatio(0.88f); |
| | | holder.tv_movie_title.setVisibility(View.GONE); |
| | | ad.getCsj().setDislikeCallback(mContext, new TTAdDislike.DislikeInteractionCallback() { |
| | | |
| | | @Override |
| | | public void onSelected(int i, String s) { |
| | | contentList.remove(content); |
| | | notifyDataSetChanged(); |
| | | } |
| | | |
| | | @Override |
| | | public void onCancel() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onRefuse() { |
| | | |
| | | } |
| | | }); |
| | | ad.getCsj().render(); |
| | | holder.fl_container.addView(ad.getCsj().getExpressAdView()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | int count = contentList.size(); |
| | | if (count > maxItemNum) |
| | | return maxItemNum; |
| | | else |
| | | return count % 2 == 0 ? count : count - 1; |
| | | } |
| | | |
| | | @Override |
| | | public int getItemViewType(int position) { |
| | | return this.contentList.get(position).getType(); |
| | | } |
| | | |
| | | |
| | | private void myClick(VideoInfo info, final View v) { |
| | | Intent intent = new Intent(v.getContext(), VideoDetailActivity2.class); |
| | | intent.putExtra("video_info", info); |
| | | intent.putExtra("from", "recommend"); |
| | | v.getContext().startActivity(intent); |
| | | } |
| | | |
| | | } |