admin
2021-04-02 545ce4665c1d506393908b55aafd681a45c774e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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);
        }
    }
}