| | |
| | | package com.mugua.mgvideo.ui.recommend; |
| | | |
| | | import android.annotation.SuppressLint; |
| | | import android.app.Activity; |
| | | import android.content.Intent; |
| | | import android.graphics.Color; |
| | | import android.graphics.Typeface; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | |
| | | import com.bumptech.glide.Glide; |
| | | import com.bumptech.glide.request.RequestOptions; |
| | | import com.bumptech.glide.request.target.DrawableImageViewTarget; |
| | | import com.google.gson.FieldNamingPolicy; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import com.bytedance.sdk.dp.DPArticleLevel; |
| | | import com.bytedance.sdk.dp.DPComponentPosition; |
| | | import com.bytedance.sdk.dp.DPSdk; |
| | | import com.bytedance.sdk.dp.DPWidgetTextChainParams; |
| | | import com.bytedance.sdk.dp.DPWidgetVideoCardParams; |
| | | import com.bytedance.sdk.dp.IDPElement; |
| | | import com.bytedance.sdk.dp.IDPVideoCardListener; |
| | | import com.bytedance.sdk.dp.IDPWidgetFactory; |
| | | import com.lcjian.library.util.common.DimenUtils; |
| | | import com.lcjian.library.widget.RatioLayout; |
| | | import com.mugua.mgvideo.MGVideoAPI; |
| | |
| | | import com.mugua.mgvideo.ui.category.StarsActivity; |
| | | import com.mugua.mgvideo.ui.common.VideosLiveActivity; |
| | | import com.mugua.mgvideo.ui.video.VideoRecommendAdapter; |
| | | import com.yeshi.base.utils.JsonUtil; |
| | | import com.yeshi.base.entity.ad.ExpressAdContainer; |
| | | import com.yeshi.base.entity.video.VideoType; |
| | | import com.yeshi.base.utils.JumpActivityUtil; |
| | | import com.yeshi.base.utils.ad.AdUtil; |
| | | import com.yeshi.base.utils.ad.ExpressAdManager; |
| | | import com.yeshi.base.utils.http.BasicTextHttpResponseHandler; |
| | | import com.ysh.wpc.appupdate.util.StringUtils; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.TreeSet; |
| | | |
| | | import androidx.annotation.Nullable; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade; |
| | |
| | | public final int TYPE_HEADER = 10001; |
| | | public final int TYPE_FOOTER = 10002; |
| | | |
| | | private Gson gson = new GsonBuilder().setVersion(1.0).setFieldNamingPolicy( |
| | | FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | |
| | | private View headerView; |
| | | private View footerView; |
| | | |
| | | private FrameLayout mNewsLinkLayout; |
| | | private IDPElement mTextChainElement; |
| | | |
| | | private FrameLayout mVideoCardLayout; |
| | | private TextView mVideoCardTitle; |
| | | private IDPElement mVideoCardElement; |
| | | |
| | | public void setHeaderView(View view) { |
| | | this.headerView = view; |
| | |
| | | case TYPE_FOOTER: |
| | | return new FooterViewHolder(footerView); |
| | | case RecommendContent.TYPE_HOMETYPE: |
| | | return new HomeTypeHolder(inflater.inflate(R.layout.item_recommend_home_type, null, false), new RecyclerView.RecycledViewPool()); |
| | | return new HomeTypeHolder(inflater.inflate(R.layout.item_recommend_home_type, parent, false), new RecyclerView.RecycledViewPool()); |
| | | case RecommendContent.TYPE_AD: |
| | | return new AdHolder(inflater.inflate(R.layout.item_recommend_home_type_ad, parent, false)); |
| | | |
| | | case RecommendContent.TYPE_NEWS: |
| | | //加载文字链 |
| | | loadTextChainWidget(); |
| | | return new NewsHolder(inflater.inflate(R.layout.item_recommend_news, parent, false)); |
| | | |
| | | case RecommendContent.TYPE_DRAW_VIDEOS: |
| | | // 加载 video card 组件 |
| | | loadVideoCard(); |
| | | return new DrawVideosHolder(inflater.inflate(R.layout.item_recommend_draw_video, parent, false)); |
| | | } |
| | | return null; |
| | | } |
| | |
| | | private int recyclerViewWidth = -1; |
| | | |
| | | @Override |
| | | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { |
| | | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) { |
| | | if (viewHolder instanceof HeaderViewHolder) |
| | | return; |
| | | if (viewHolder instanceof FooterViewHolder) |
| | |
| | | int p = i; |
| | | if (headerView != null) |
| | | p--; |
| | | |
| | | |
| | | p -= (AdUtil.isCanInitCSJ() ? 2 : 0); |
| | | if (viewHolder instanceof HomeTypeHolder) { |
| | | final HomeType homeType = (HomeType) contentList.get(p); |
| | | |
| | | |
| | | final HomeTypeHolder holder = (HomeTypeHolder) viewHolder; |
| | | |
| | | if (homeType == null) { |
| | | if (homeType == null || homeType.getHomeVideoList().size() == 0) { |
| | | holder.view.setVisibility(View.GONE); |
| | | return; |
| | | } |
| | | holder.view.setVisibility(View.VISIBLE); |
| | | |
| | | |
| | | if (homeType.getVideoInfoList() == null) { |
| | | homeType.setVideoInfoList(new ArrayList<>()); |
| | | } |
| | |
| | | List<HomeVideo> homeList = new ArrayList<>(); |
| | | if (homeArray != null && homeArray.length() > 0) { |
| | | for (int j = 0; j < homeArray.length(); j++) { |
| | | homeList.add(gson.fromJson(homeArray |
| | | homeList.add(JsonUtil.videoGson.fromJson(homeArray |
| | | .optJSONObject(j).toString(), |
| | | HomeVideo.class)); |
| | | } |
| | |
| | | intent = new Intent(v.getContext(), StarsActivity.class); |
| | | v.getContext().startActivity(intent); |
| | | } else { |
| | | Gson gson = new GsonBuilder() |
| | | .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | VideoType type = gson.fromJson(homeType.getParams().replace( |
| | | |
| | | VideoType type = JsonUtil.videoGson.fromJson(homeType.getParams().replace( |
| | | "\\" + "\"", "\""), VideoType.class); |
| | | try { |
| | | intent = new Intent(v.getContext(), Class.forName(homeType.getActivity())); |
| | |
| | | } else { |
| | | holder.ll_container.setVisibility(View.GONE); |
| | | } |
| | | } else if (viewHolder instanceof NewsHolder) { |
| | | final NewsHolder holder = (NewsHolder) viewHolder; |
| | | mNewsLinkLayout = holder.fl_container; |
| | | |
| | | } else if (viewHolder instanceof DrawVideosHolder) { |
| | | final DrawVideosHolder holder = (DrawVideosHolder) viewHolder; |
| | | mVideoCardLayout = holder.fl_container; |
| | | mVideoCardTitle = holder.tv_video_card_title; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 文字链组件 |
| | | */ |
| | | private void loadTextChainWidget() { |
| | | if (!AdUtil.isCanInitCSJ()) { |
| | | return; |
| | | } |
| | | DPSdk.factory().loadTextChain(DPWidgetTextChainParams.obtain() |
| | | .articleLevel(DPArticleLevel.LEVEL_2) // 内容等级 |
| | | .componentPosition(DPComponentPosition.TAB2) |
| | | .animationDuration(1000) // 动画时长 |
| | | .backgroundColor(0xFFFFFFFF) // 背景颜色 |
| | | // .iconDrawable(getResources().getDrawable(R.drawable.ic_launcher)) // icon标志 |
| | | .iconHeight(16) // icon高度 |
| | | .iconWidth(16) // icon宽度 |
| | | .showDuration(3000) // 停留时长 |
| | | .showIcon(true) // 是否展示icon |
| | | .showWatch(true) // 是否展示观看信息 |
| | | .titleTextColor(0xFF595959) // 标题文字颜色 |
| | | .titleTextSize(14) // 标题文字大小 |
| | | .titleTypeface(Typeface.DEFAULT) // 标题文字字体 |
| | | .watchTextColor(0xFFA0A0A0) // 观看文字颜色 |
| | | .watchTextSize(12) // 观看文字大小 |
| | | .watchTypeface(Typeface.DEFAULT), new IDPWidgetFactory.Callback() { |
| | | |
| | | @Override |
| | | public void onError(int i, String s) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(IDPElement data) { |
| | | if (data == null) { |
| | | return; |
| | | } |
| | | mTextChainElement = data; |
| | | |
| | | View view = mTextChainElement.getView(); |
| | | if (view != null && mNewsLinkLayout != null) { |
| | | mNewsLinkLayout.removeAllViews(); |
| | | mNewsLinkLayout.addView(view); |
| | | mTextChainElement.reportShow(); |
| | | notifyDataSetChanged(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void loadVideoCard() { |
| | | |
| | | if (!AdUtil.isCanInitCSJ()) { |
| | | return; |
| | | } |
| | | |
| | | DPWidgetVideoCardParams params = DPWidgetVideoCardParams.obtain() |
| | | .hideTitle(true) |
| | | .listener(new IDPVideoCardListener() { |
| | | @Override |
| | | public void onDPItemClick(Map<String, Object> map) { |
| | | log("onDPItemClick map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPLSwipeEnter() { |
| | | log("onDPLSwipeEnter"); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPRequestStart(@Nullable Map<String, Object> map) { |
| | | log("onDPRequestStart"); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPRequestSuccess(List<Map<String, Object>> list) { |
| | | if (list == null) { |
| | | return; |
| | | } |
| | | |
| | | for (int i = 0; i < list.size(); i++) { |
| | | log("onDPRequestSuccess i = " + i + ", map = " + list.get(i).toString()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onDPRequestFail(int code, String msg, @Nullable Map<String, Object> map) { |
| | | if (map == null) { |
| | | log("onDPRequestFail code = " + code + ", msg = " + msg); |
| | | return; |
| | | } |
| | | log("onDPRequestFail code = " + code + ", msg = " + msg + ", map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPClientShow(@Nullable Map<String, Object> map) { |
| | | log("onDPClientShow"); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPClickAuthorName(Map<String, Object> map) { |
| | | log("onDPClickAuthorName map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPClickAvatar(Map<String, Object> map) { |
| | | log("onDPClickAvatar map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPClickComment(Map<String, Object> map) { |
| | | log("onDPClickComment map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPClickLike(boolean isLike, Map<String, Object> map) { |
| | | log("onDPClickLike isLike = " + isLike + ", map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPVideoPlay(Map<String, Object> map) { |
| | | log("onDPVideoPlay map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPVideoPause(Map<String, Object> map) { |
| | | log("onDPVideoPause map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPVideoContinue(Map<String, Object> map) { |
| | | log("onDPVideoContinue map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPVideoOver(Map<String, Object> map) { |
| | | log("onDPVideoOver map = " + map.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onDPVideoCompletion(Map<String, Object> map) { |
| | | log("onDPVideoCompletion map = " + map.toString()); |
| | | } |
| | | }) |
| | | .dislikeListener(mContext, new DPWidgetVideoCardParams.IDislikeListener() { |
| | | @Override |
| | | public void onSelected(String msg) { |
| | | // 如果 hideTitle 为 true,可以不做任何处理 |
| | | log("dislike msg = " + msg); |
| | | if (mVideoCardLayout == null) { |
| | | return; |
| | | } |
| | | mVideoCardLayout.removeAllViews(); |
| | | mVideoCardLayout.setVisibility(View.GONE); |
| | | mVideoCardTitle.setVisibility(View.GONE); |
| | | } |
| | | }); |
| | | |
| | | DPSdk.factory().loadSmallVideoCard(params, new IDPWidgetFactory.Callback() { |
| | | @Override |
| | | public void onError(int code, String msg) { |
| | | log("onError code = " + code + ", msg = " + msg); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(IDPElement data) { |
| | | mVideoCardElement = data; |
| | | View view = data.getView(); |
| | | if (view == null || mVideoCardLayout == null) { |
| | | return; |
| | | } |
| | | |
| | | mVideoCardLayout.removeAllViews(); |
| | | ViewGroup viewGroup = (ViewGroup) view.getParent(); |
| | | if (viewGroup != null) { |
| | | viewGroup.removeAllViews(); |
| | | } |
| | | mVideoCardLayout.addView(view); |
| | | mVideoCardElement.reportShow(); |
| | | mVideoCardTitle.setVisibility(View.VISIBLE); |
| | | notifyDataSetChanged(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private static void log(String msg) { |
| | | Log.d(TAG, String.valueOf(msg)); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | |
| | | count++; |
| | | if (footerView != null) |
| | | count++; |
| | | if (AdUtil.isCanInitCSJ()) { |
| | | count += 2; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | |
| | | if (headerView != null) |
| | | position--; |
| | | |
| | | if (position >= contentList.size()) |
| | | if (position >= contentList.size() + (AdUtil.isCanInitCSJ() ? 2 : 0)) |
| | | return TYPE_FOOTER; |
| | | if (AdUtil.isCanInitCSJ()) { |
| | | if (position == 0) { |
| | | return RecommendContent.TYPE_NEWS; |
| | | } else if (position == 1) { |
| | | return RecommendContent.TYPE_DRAW_VIDEOS; |
| | | } |
| | | } |
| | | |
| | | if (contentList.get(position) instanceof HomeType) |
| | | position -= (AdUtil.isCanInitCSJ() ? 2 : 0); |
| | | if (contentList.get(position) instanceof HomeType) { |
| | | return RecommendContent.TYPE_HOMETYPE; |
| | | else |
| | | } else { |
| | | return RecommendContent.TYPE_AD; |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | class NewsHolder extends RecyclerView.ViewHolder { |
| | | |
| | | FrameLayout fl_container; |
| | | |
| | | |
| | | public NewsHolder(View view) { |
| | | super(view); |
| | | |
| | | fl_container = view.findViewById(R.id.fl_container); |
| | | } |
| | | } |
| | | |
| | | class DrawVideosHolder extends RecyclerView.ViewHolder { |
| | | |
| | | FrameLayout fl_container; |
| | | TextView tv_video_card_title; |
| | | |
| | | public DrawVideosHolder(View view) { |
| | | super(view); |
| | | tv_video_card_title = view.findViewById(R.id.tv_video_card_title); |
| | | fl_container = view.findViewById(R.id.fl_container); |
| | | |
| | | } |
| | | } |
| | | |
| | | public void onDestory() { |
| | | if (mTextChainElement != null) { |
| | | mTextChainElement.destroy(); |
| | | } |
| | | |
| | | if (mVideoCardElement != null) { |
| | | mVideoCardElement.destroy(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | public void refresh() { |
| | | loadVideoCard(); |
| | | loadTextChainWidget(); |
| | | } |
| | | } |