package com.weikou.beibeivideo.ui.category;
|
|
import android.app.Activity;
|
import android.graphics.Canvas;
|
import android.graphics.Rect;
|
import android.support.annotation.NonNull;
|
import android.support.v4.view.ViewPager;
|
import android.support.v7.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.RelativeLayout;
|
|
import com.lcjian.library.util.common.DimenUtils;
|
import com.viewpagerindicator.CirclePageIndicator;
|
import com.weikou.beibeivideo.entity.CategoryRecommendVideo;
|
import com.weikou.beibeivideo.entity.VideoInfo;
|
import com.weikou.beibeivideo.entity.VideoType;
|
import com.weikou.beibeivideo.ui.video.VideoColumn2Adapter;
|
import com.weikou.beibeivideo.R;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2016/8/18.
|
*/
|
public class CategoryRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
private Activity mContext;
|
|
private List<CategoryRecommendVideo> bannerList;
|
|
private List<VideoInfo> mVideoList;
|
|
private LayoutInflater inflater;
|
|
private final static String TAG = "CategoryRecommendAdapter";
|
|
private final static int TYPE_BANNER = 1;
|
|
private final static int TYPE_VIDEO_LIST = 2;
|
private VideoType videoType;
|
|
public CategoryRecommendAdapter(List<CategoryRecommendVideo> bannerList, List<VideoInfo> videoList, VideoType videoType, Activity context) {
|
this.mVideoList = videoList;
|
mContext = context;
|
this.bannerList = bannerList;
|
this.videoType = videoType;
|
inflater = LayoutInflater.from(context);
|
}
|
|
/**
|
* 设置banner
|
*
|
* @param bannerList
|
*/
|
public void setBannerList(List<CategoryRecommendVideo> bannerList) {
|
if (this.bannerList == null)
|
this.bannerList = new ArrayList<>();
|
this.bannerList.clear();
|
this.bannerList.addAll(bannerList);
|
notifyDataSetChanged();
|
}
|
|
/**
|
* 设置视频
|
*
|
* @param videoInfoList
|
*/
|
public void setVideoList(List<VideoInfo> videoInfoList) {
|
if (this.mVideoList == null)
|
this.mVideoList = new ArrayList<>();
|
this.mVideoList.clear();
|
this.mVideoList.addAll(videoInfoList);
|
notifyDataSetChanged();
|
}
|
|
@NonNull
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
if (viewType == TYPE_BANNER) {
|
return new BannerHolder(inflater.inflate(R.layout.item_cate_recommend_top, parent, false));
|
} else {
|
return new VideoListHolder(inflater.inflate(R.layout.item_recyclerview, null, false));
|
}
|
}
|
|
private Runnable autoPlayBanner = null;
|
|
private synchronized void autoPlayBanner(final ViewPager viewPager) {
|
|
if (autoPlayBanner == null) {
|
autoPlayBanner = new Runnable() {
|
@Override
|
public void run() {
|
try {
|
if (bannerList == null || bannerList.size() == 0)
|
return;
|
|
if (viewPager.getCurrentItem() >= bannerList.size() - 1) {
|
viewPager.setCurrentItem(0, true);
|
} else {
|
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
|
}
|
autoPlayBanner(viewPager);
|
} catch (Exception e) {
|
|
}
|
}
|
};
|
viewPager.postDelayed(autoPlayBanner, 3000);
|
} else {
|
viewPager.removeCallbacks(autoPlayBanner);
|
viewPager.postDelayed(autoPlayBanner, 3000);
|
}
|
}
|
|
|
RecyclerView.ItemDecoration itemDecoration = 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();
|
if (index % 2 == 0) {
|
outRect.left = 0; //第一列左边贴边
|
outRect.right = DimenUtils.dipToPixels(5, view.getContext());
|
} else {
|
outRect.right = 0; //第一列左边贴边
|
outRect.left = DimenUtils.dipToPixels(5, view.getContext());
|
}
|
|
if (index - 1 > 0) {
|
outRect.top = DimenUtils.dipToPixels(5, view.getContext());
|
} else
|
outRect.top = 0;
|
|
if (total % 2 == 0) {
|
if (total > 2 && (index < total - 2))
|
outRect.bottom = DimenUtils.dipToPixels(5, view.getContext());
|
else
|
outRect.bottom = 0;
|
} else {
|
if (total > 2 && (index < total - 1))
|
outRect.bottom = DimenUtils.dipToPixels(5, view.getContext());
|
else
|
outRect.bottom = 0;
|
}
|
}
|
};
|
|
@Override
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
if (holder instanceof BannerHolder) {
|
BannerHolder viewHolder = (BannerHolder) holder;
|
if (viewHolder.rl_banner == null)
|
return;
|
if (viewHolder.vp_recommend == null)
|
return;
|
if (viewHolder.indicator_recommend == null)
|
return;
|
|
if (bannerList == null || bannerList.size() == 0) {
|
viewHolder.rl_banner.setVisibility(View.GONE);
|
} else {
|
viewHolder.rl_banner.setVisibility(View.VISIBLE);
|
CategoryRecommedTopAdapter topAdapter = new CategoryRecommedTopAdapter(bannerList, mContext);
|
viewHolder.vp_recommend.setAdapter(topAdapter);
|
viewHolder.indicator_recommend.setViewPager(viewHolder.vp_recommend);
|
autoPlayBanner(viewHolder.vp_recommend);
|
}
|
|
} else {
|
VideoListHolder viewHolder = (VideoListHolder) holder;
|
if (mVideoList.size() > 0) {
|
int column = 2;
|
if (videoType != null && videoType.getName() != null) {
|
if (videoType.getName().contains("电影") || videoType.getName().contains("电视剧") || videoType.getName().contains("综艺") || videoType.getName().contains("动漫"))
|
column = 3;
|
}
|
VideoColumn2Adapter adapter = new VideoColumn2Adapter(mContext, mContext.getApplicationContext(), mVideoList, false, column, "categoryRecommend", null);
|
if (column == 3)
|
adapter.initRecyclerViewDisplayWidthColumn3(viewHolder.rv);
|
else
|
adapter.initRecyclerViewDisplayWidthColumn2(viewHolder.rv);
|
viewHolder.rv.setAdapter(adapter);
|
}
|
}
|
}
|
|
@Override
|
public int getItemCount() {
|
return 2;
|
}
|
|
|
@Override
|
public int getItemViewType(int position) {
|
if (position == 0)
|
return TYPE_BANNER;
|
else
|
return TYPE_VIDEO_LIST;
|
}
|
|
class BannerHolder extends RecyclerView.ViewHolder {
|
ViewPager vp_recommend;
|
CirclePageIndicator indicator_recommend;
|
RelativeLayout rl_banner;
|
|
public BannerHolder(View itemView) {
|
super(itemView);
|
vp_recommend = itemView.findViewById(R.id.vp_recommend);
|
indicator_recommend = itemView.findViewById(R.id.indicator_recommend);
|
rl_banner = itemView.findViewById(R.id.rl_banner);
|
}
|
}
|
|
|
class VideoListHolder extends RecyclerView.ViewHolder {
|
RecyclerView rv;
|
|
public VideoListHolder(View itemView) {
|
super(itemView);
|
rv = itemView.findViewById(R.id.rv);
|
}
|
}
|
}
|