package com.weikou.beibeivideo.ui.video;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.graphics.Canvas;
|
import android.graphics.Rect;
|
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.RecyclerView;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.ImageView;
|
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.request.RequestOptions;
|
import com.bumptech.glide.request.target.DrawableImageViewTarget;
|
import com.lcjian.library.util.common.DimenUtils;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.qq.e.ads.nativ.NativeUnifiedADData;
|
import com.qq.e.ads.nativ.widget.NativeAdContainer;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.VideoInfo;
|
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
|
import com.weikou.beibeivideo.entity.recommend.holder.RecommendVideoAdHolder;
|
import com.weikou.beibeivideo.entity.video.VideoContent;
|
import com.weikou.beibeivideo.entity.video.holder.FooterViewHolder;
|
import com.weikou.beibeivideo.entity.video.holder.HeaderViewHolder;
|
import com.weikou.beibeivideo.entity.video.holder.VideoHolder;
|
import com.weikou.beibeivideo.ui.media.VideoDetailActivity2;
|
import com.weikou.beibeivideo.util.VideoUtil;
|
import com.weikou.beibeivideo.util.ad.ExpressAdManager;
|
import com.weikou.beibeivideo.util.ad.GDTConstant;
|
import com.weikou.beibeivideo.util.ad.GDTNativeADUnifiedManager;
|
|
import java.text.DecimalFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
|
public class VideoRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private Context mContext;
|
private List<VideoContent> contentList;
|
private LayoutInflater inflater;
|
private IVideoClickListener itemClickListener;
|
private ExpressAdManager.IAdShowListener showListener;
|
private Activity activity;
|
private int columns;
|
|
String TAG = "VideoColumn2Adapter";
|
|
private View headerView;
|
|
private View footerView;
|
|
//如果是横向的才设置宽度
|
private Integer itemWidth;//条目宽度
|
|
private final static int TYPE_HEADER = 10001;
|
private final static int TYPE_FOOTER = 10002;
|
|
private String from;
|
private String tag;
|
|
|
public void setHeaderView(View headerView) {
|
this.headerView = headerView;
|
}
|
|
public void setFooterView(View footerView) {
|
this.footerView = footerView;
|
}
|
|
RecyclerView.ItemDecoration itemDecoration2 = new RecyclerView.ItemDecoration() {
|
@Override
|
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDraw(c, parent, state);
|
}
|
|
@Override
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDrawOver(c, parent, state);
|
}
|
|
@Override
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
super.getItemOffsets(outRect, view, parent, state);
|
int index = parent.getChildAdapterPosition(view);
|
int total = parent.getAdapter().getItemCount();
|
int minWidth = DimenUtils.dipToPixels(2, view.getContext());
|
|
GridLayoutManager.SpanSizeLookup lookup = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup();
|
int size = lookup.getSpanSize(index);
|
if (size == 6) {
|
outRect.left = 0;
|
outRect.right = 0;
|
} else {
|
//计算距离上一个大图的距离
|
int lastBigIndex = 0;
|
for (int i = index - 1; i >= 0; i--) {
|
if (lookup.getSpanSize(i) == 6) {
|
lastBigIndex = i + 1;
|
break;
|
}
|
}
|
|
|
if ((index - lastBigIndex) % 2 == 0) {
|
outRect.left = 0; //第一列左边贴边
|
outRect.right = minWidth;
|
} else {
|
outRect.right = 0; //第一列左边贴边
|
outRect.left = minWidth;
|
}
|
}
|
|
int totalSize = 0;
|
for (int i = 0; i < index; i++) {
|
totalSize += lookup.getSpanSize(i);
|
}
|
|
if (totalSize >= 6) {
|
outRect.top = minWidth * 2;
|
} else
|
outRect.top = 0;
|
|
//是否是最后一行
|
boolean lastRow = false;
|
//最后一个
|
if (index == total - 1) {
|
lastRow = true;
|
} else {
|
if (index == total - 2) {
|
totalSize += lookup.getSpanSize(index);
|
if (totalSize % 6 != 0) {
|
lastRow = true;
|
}
|
}
|
}
|
if (lastRow)
|
outRect.bottom = minWidth * 2;
|
else
|
outRect.bottom = 0;
|
|
Log.i(TAG, String.format(" %s: left:%s right:%s top:%s bottom:%s", tag, outRect.left, outRect.right, outRect.top, outRect.bottom));
|
}
|
};
|
|
RecyclerView.ItemDecoration itemDecorationRow = new RecyclerView.ItemDecoration() {
|
@Override
|
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDraw(c, parent, state);
|
}
|
|
@Override
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDrawOver(c, parent, state);
|
}
|
|
@Override
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
super.getItemOffsets(outRect, view, parent, state);
|
int index = parent.getChildAdapterPosition(view);
|
int total = parent.getAdapter().getItemCount();
|
int minWidth = DimenUtils.dipToPixels(2, view.getContext());
|
if (index == 0) {
|
outRect.left = minWidth * 2; //第一列左边贴边
|
outRect.right = minWidth;
|
} else if (index == total - 1) {
|
outRect.right = minWidth;
|
outRect.left = minWidth;
|
} else {
|
outRect.right = minWidth * 2; //第一列左边贴边
|
outRect.left = minWidth;
|
}
|
}
|
};
|
|
RecyclerView.ItemDecoration itemDecoration3 = new RecyclerView.ItemDecoration() {
|
@Override
|
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDraw(c, parent, state);
|
}
|
|
@Override
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
super.onDrawOver(c, parent, state);
|
}
|
|
@Override
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
super.getItemOffsets(outRect, view, parent, state);
|
int index = parent.getChildAdapterPosition(view);
|
int total = parent.getAdapter().getItemCount();
|
int minWidth = DimenUtils.dipToPixels(1, view.getContext());
|
if (index % 3 == 0) {
|
outRect.left = 0; //第一列左边贴边
|
outRect.right = minWidth * 2;
|
} else if (index % 3 == 1) {
|
outRect.right = minWidth * 1;
|
outRect.left = minWidth * 1;
|
} else {
|
outRect.right = 0; //第一列左边贴边
|
outRect.left = minWidth * 2;
|
}
|
|
if (index - 2 > 0) {
|
outRect.top = minWidth * 3;
|
} else
|
outRect.top = 0;
|
|
if (total > 3) {
|
if (index < ((total - 1) / 3) * 3)
|
outRect.bottom = minWidth * 3;
|
else
|
outRect.bottom = 0;
|
} else
|
outRect.bottom = 0;
|
}
|
};
|
|
/**
|
* 获取每行的宽度
|
*
|
* @param activity
|
* @param column
|
* @return
|
*/
|
public static int getItemWidth(Activity activity, int column, int padding) {
|
int deviceWidth = DimenUtils.getScreenWidth(activity);
|
if (column == 3)
|
return (deviceWidth - DimenUtils.dip2px(activity, 3 * 2) - padding) / 3;
|
else if (column == 2)
|
return (deviceWidth - DimenUtils.dip2px(activity, 3) - padding) / 2;
|
else
|
return (int) (deviceWidth / 2.5f);
|
}
|
|
|
/**
|
* 设置列表的显示样式(一行2列)
|
*
|
* @param rv
|
* @param firstBigPosition 第一个数据是否以大图显示
|
*/
|
public void initRecyclerViewDisplayWidthColumn2(RecyclerView rv, int firstBigPosition) {
|
if (rv.getLayoutManager() == null || !(rv.getLayoutManager() instanceof GridLayoutManager)) {
|
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(rv.getContext(), 6);
|
rv.setLayoutManager(layoutManager);
|
}
|
//统一分为2列
|
((GridLayoutManager) rv.getLayoutManager()).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
@Override
|
public int getSpanSize(int position) {
|
int type = getItemViewType(position);
|
if (type == TYPE_FOOTER || type == TYPE_HEADER) {
|
return 6;
|
} else {
|
if (getContentPosition(position) == firstBigPosition) {
|
return 6;
|
} else
|
return 3;
|
}
|
}
|
});
|
for (int i = 0; i < rv.getItemDecorationCount(); i++) {
|
rv.removeItemDecoration(rv.getItemDecorationAt(i));
|
i--;
|
}
|
|
int padding = 0;
|
padding += rv.getPaddingLeft();
|
padding += rv.getPaddingRight();
|
|
int count = rv.getItemDecorationCount();
|
for (int i = 0; i < count; i++) {
|
rv.removeItemDecorationAt(i);
|
}
|
rv.addItemDecoration(itemDecoration2);
|
}
|
|
|
/**
|
* 设置列表的显示样式(一行2列)
|
*
|
* @param rv
|
*/
|
public void initRecyclerViewDisplayWidthColumn2(RecyclerView rv) {
|
initRecyclerViewDisplayWidthColumn2(rv, -1);
|
}
|
|
|
public void initRecyclerViewDisplayWidthRow1(RecyclerView rv) {
|
// if (rv.getLayoutManager() == null || !(rv.getLayoutManager() instanceof LinearLayoutManager)) {
|
LinearLayoutManager layoutManager = new LinearLayoutManager(rv.getContext());
|
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
|
rv.setLayoutManager(layoutManager);
|
// } else {
|
// ((LinearLayoutManager) rv.getLayoutManager()).setOrientation(LinearLayoutManager.HORIZONTAL);
|
// }
|
for (int i = 0; i < rv.getItemDecorationCount(); i++) {
|
rv.removeItemDecoration(rv.getItemDecorationAt(i));
|
i--;
|
}
|
int padding = 0;
|
padding += rv.getPaddingLeft();
|
padding += rv.getPaddingRight();
|
itemWidth = getItemWidth(activity, Integer.MAX_VALUE, padding);
|
rv.addItemDecoration(itemDecorationRow);
|
}
|
|
/**
|
* 设置列表的显示样式(一行3列)
|
*
|
* @param rv
|
*/
|
public void initRecyclerViewDisplayWidthColumn3(RecyclerView rv) {
|
if (rv.getLayoutManager() == null || !(rv.getLayoutManager() instanceof GridLayoutManager)) {
|
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(rv.getContext(), 6);
|
rv.setLayoutManager(layoutManager);
|
}
|
//统一分为2列
|
((GridLayoutManager) rv.getLayoutManager()).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
@Override
|
public int getSpanSize(int position) {
|
int type = getItemViewType(position);
|
if (type == TYPE_FOOTER || type == TYPE_HEADER)
|
return 6;
|
else
|
return 2;
|
}
|
});
|
for (int i = 0; i < rv.getItemDecorationCount(); i++) {
|
rv.removeItemDecoration(rv.getItemDecorationAt(i));
|
i--;
|
}
|
rv.addItemDecoration(itemDecoration3);
|
}
|
|
public VideoRecommendAdapter() {
|
|
}
|
|
public void refreshAd() {
|
if (contentList == null || contentList.size() == 0)
|
return;
|
|
GDTNativeADUnifiedManager.loadAD(columns == 3 ? GDTConstant.PID_2_VIDEO_LIST_SMALL_AD_COLUMN3 : GDTConstant.PID_2_VIDEO_LIST_SMALL_AD_COLUMN2, 1, mContext, new GDTNativeADUnifiedManager.IAdLoadListener() {
|
@Override
|
public void onSuccess(List<NativeUnifiedADData> adList) {
|
if (adList != null && adList.size() > 0)
|
VideoRecommendAdapter.this.addAdToPosition(0, new ExpressAdContainer(adList.get(0)));
|
}
|
});
|
}
|
|
public void setVideoList(List<VideoInfo> videoList) {
|
if (contentList == null)
|
contentList = new ArrayList<>();
|
contentList.clear();
|
for (VideoInfo video : videoList) {
|
contentList.add(VideoContent.createVideoContent(video));
|
}
|
}
|
|
public void setColumns(int columns) {
|
this.columns = columns;
|
}
|
|
public VideoRecommendAdapter(final Activity activity, Context context, List<VideoInfo> videoList, boolean needAd, int columns, String from, String tag, IVideoClickListener itemClickListener) {
|
this(activity, context, videoList, needAd, columns, from, tag, itemClickListener, null);
|
}
|
|
|
public VideoRecommendAdapter(final Activity activity, Context context, List<VideoInfo> videoList, boolean needAd, int columns, String from, String tag, IVideoClickListener itemClickListener, ExpressAdManager.IAdShowListener showListener) {
|
if (activity == null || context == null)
|
return;
|
this.mContext = context;
|
this.activity = activity;
|
this.contentList = new ArrayList<>();
|
inflater = LayoutInflater.from(context);
|
this.itemClickListener = itemClickListener;
|
this.columns = columns;
|
this.showListener = showListener;
|
this.from = from;
|
this.tag = tag;
|
|
for (VideoInfo video : videoList) {
|
contentList.add(VideoContent.createVideoContent(video));
|
}
|
|
if (needAd) {
|
refreshAd();
|
}
|
}
|
|
// 把返回的 广告 添加到数据集里面去
|
void addAdToPosition(int position, ExpressAdContainer ad) {
|
if (position >= 0 && position < contentList.size() && ad != null) {
|
contentList.add(position, VideoContent.createAdContent(ad));
|
notifyDataSetChanged();
|
// notifyItemInserted(position);
|
// notifyItemRangeChanged(0, contentList.size());
|
if (showListener != null)
|
showListener.show();
|
|
Log.d(TAG, "addAdToPosition:" + VideoRecommendAdapter.this.toString());
|
}
|
}
|
|
// 移除 广告 的时候是一条一条移除的
|
void removeADView(int position, ExpressAdContainer ad) {
|
if (contentList.get(position).getAd() == null)
|
return;
|
contentList.remove(position);
|
notifyItemRemoved(position);
|
notifyItemRangeChanged(0, contentList.size() - 1);
|
}
|
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
|
|
switch (type) {
|
case TYPE_HEADER:
|
return new HeaderViewHolder(headerView);
|
case TYPE_FOOTER:
|
return new FooterViewHolder(footerView);
|
case VideoContent.TYPE_AD:
|
return new RecommendVideoAdHolder(inflater.inflate(R.layout.item_ad_list_small, parent, false));
|
case VideoContent.TYPE_VIDEO:
|
return new VideoHolder(inflater.inflate(R.layout.item_movie, parent, false));
|
}
|
return null;
|
}
|
|
@Override
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int p) {
|
if ((viewHolder instanceof HeaderViewHolder) || (viewHolder instanceof FooterViewHolder)) {
|
return;
|
}
|
if (headerView != null)
|
p = p - 1;
|
|
final VideoContent content = contentList.get(p);
|
|
ViewGroup.LayoutParams params = viewHolder.itemView.getLayoutParams();
|
if (columns > 3) {
|
if (itemWidth != null) {
|
params.width = itemWidth;
|
}
|
} else {
|
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
}
|
|
if (viewHolder instanceof VideoHolder) {
|
VideoHolder holder = (VideoHolder) viewHolder;
|
final VideoInfo info = content.getVideo();
|
//设置内容
|
holder.ll_video_show.setVisibility(View.VISIBLE);
|
String defaultPicture = "";
|
if (columns == 2) {
|
defaultPicture = info.getHpicture();
|
} else if (columns == 3) {
|
defaultPicture = info.getVpicture();
|
}
|
|
if (StringUtils.isEmpty(defaultPicture)) {
|
defaultPicture = info.getPicture();
|
}
|
|
try {
|
Glide.with(mContext).load(defaultPicture).transition(withCrossFade())
|
.apply(new RequestOptions().placeholder(R.drawable.shape_default_cover).error(R.drawable.shape_default_cover))
|
.into(new DrawableImageViewTarget(holder.iv_movie_cover, true));
|
} catch (IllegalArgumentException e) {
|
e.printStackTrace();
|
}
|
|
//vip标识
|
if (info.getFree() == 1) {
|
holder.iv_vip.setVisibility(View.VISIBLE);
|
} else {
|
holder.iv_vip.setVisibility(View.GONE);
|
}
|
|
|
holder.tv_movie_title.setText(info.getName());
|
|
String tag = StringUtils.isBlank(info.getTag()) ? "" : info
|
.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);
|
}
|
|
DecimalFormat df = new DecimalFormat("###.0");
|
holder.tv_movie_play_num.setText(VideoUtil.getWatchCountShortName(info.getWatchCount()));
|
holder.tv_movie_comment_num.setText(StringUtils.isBlank(info.getCommentCount()) ? "0" : info.getCommentCount());
|
holder.ll_video_show
|
.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(final View v) {
|
//跳转到商品视频播放页
|
if (itemClickListener == null)
|
myClick(info, v);
|
else
|
itemClickListener.onClick(info);
|
}
|
});
|
holder.tv_movie_play_num.setVisibility(View.GONE);
|
|
if (columns == 2) {
|
holder.rl_ratio.setRatio(0.625f);
|
} else if (columns == 3) {
|
holder.rl_ratio.setRatio(1.4f);
|
} else {
|
holder.rl_ratio.setRatio(0.625f);
|
}
|
|
} else if (viewHolder instanceof RecommendVideoAdHolder) {//广告
|
final RecommendVideoAdHolder holder = (RecommendVideoAdHolder) viewHolder;
|
if (columns == 2) {
|
holder.rl_container.setRatio(0.625f);
|
} else if (columns == 3) {
|
holder.rl_container.setRatio(1.4f);
|
}
|
ExpressAdContainer ad = content.getAd();
|
View v = inflater.inflate(R.layout.item_ad_list_small_gdt2, null);
|
ImageView iv_picture = v.findViewById(R.id.iv_picture);
|
NativeAdContainer nv = v.findViewById(R.id.native_ad_container);
|
GDTNativeADUnifiedManager.renderSinglePicture(iv_picture, holder.tv_movie_title, nv, ad.getGdt2Unfied());
|
holder.fl_container.removeAllViews();
|
holder.fl_container.addView(v);
|
// ExpressAdManager.fillAd(ad, holder.fl_container);
|
}
|
}
|
|
@Override
|
public int getItemCount() {
|
int count = 0;
|
if (contentList != null)
|
count = contentList.size();
|
if (columns < 4)
|
count = count % columns == 0 ? count : ((count / columns) * columns);
|
if (headerView != null)
|
count++;
|
if (footerView != null)
|
count++;
|
return count;
|
}
|
|
@Override
|
public int getItemViewType(int position) {
|
if (columns == 0)
|
return -1;
|
|
int count = contentList == null ? 0 : contentList.size();
|
int contentCount = count;
|
|
if (columns < 4)
|
contentCount = count % columns == 0 ? count : ((count / columns) * columns);
|
|
if (position == 0) {
|
if (headerView != null)
|
return TYPE_HEADER;
|
else if (this.contentList.size() > 0)
|
return this.contentList.get(position).getType();
|
else if (footerView != null)
|
return TYPE_FOOTER;
|
else
|
return -1;
|
} else if (position < (contentCount + (headerView == null ? 0 : 1))) {
|
if (headerView != null)
|
position--;
|
this.contentList.get(position).getType();
|
} else if (footerView != null)
|
return TYPE_FOOTER;
|
else
|
return -1;
|
return this.contentList.get(position).getType();
|
}
|
|
|
/**
|
* 获取内容的position
|
*
|
* @param position
|
* @return
|
*/
|
public int getContentPosition(int position) {
|
if (headerView != null)
|
return position - 1;
|
else
|
return position;
|
}
|
|
|
private void myClick(VideoInfo info, final View v) {
|
Intent intent = new Intent(v.getContext(), VideoDetailActivity2.class);
|
intent.putExtra("video_info", info);
|
intent.putExtra("from", from);
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
v.getContext().startActivity(intent);
|
}
|
|
}
|