admin
2020-08-08 3945f9a2c70335958c64c73894e3a1a14a7113b3
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.weikou.beibeivideo.ui.category;
 
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.print.PrintAttributes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.lcjian.library.util.common.StringUtils;
import com.weikou.beibeivideo.entity.VideoInfo;
import com.weikou.beibeivideo.ui.media.VideoDetailActivity;
import com.weikou.beibeivideo.util.AdPromptDialog;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.yeshi.buwanshequ.R;
 
import java.text.DecimalFormat;
import java.util.List;
 
/**
 * Created by weikou2015 on 2016/8/18.
 */
public class CategoryRecommendAdapter extends BaseAdapter {
 
    private List<VideoInfo> mList;
 
    private Context mContext;
 
    public CategoryRecommendAdapter(List<VideoInfo> list, Context context) {
        mList = list;
        mContext = context;
    }
 
    @Override
    public int getCount() {
        if (mList.size() % 2 == 0) {
            return mList == null ? 0 : (mList.size() / 2);
        } else {
            return mList == null ? 0 : (mList.size() / 2 + 1);
        }
    }
 
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item3, null);
 
            holder.ll_video_show1 = view
                    .findViewById(R.id.ll_video_show1);
            holder.ll_video_show2 = view
                    .findViewById(R.id.ll_video_show2);
            holder.iv_movie_cover = view
                    .findViewById(R.id.iv_movie_cover);
            holder.tv_movie_title = view
                    .findViewById(R.id.tv_movie_title);
            holder.tv_movie_rating = view
                    .findViewById(R.id.tv_movie_rating);
            holder.tv_movie_play_num = view
                    .findViewById(R.id.tv_movie_play_num);
            holder.tv_movie_comment_num = view
                    .findViewById(R.id.tv_movie_comment_num);
            holder.iv_movie_cover1 = view
                    .findViewById(R.id.iv_movie_cover1);
            holder.tv_movie_title1 = view
                    .findViewById(R.id.tv_movie_title1);
            holder.tv_movie_rating1 = view
                    .findViewById(R.id.tv_movie_rating1);
            holder.tv_movie_play_num1 = view
                    .findViewById(R.id.tv_movie_play_num1);
            holder.tv_movie_comment_num1 = view
                    .findViewById(R.id.tv_movie_comment_num1);
            holder.fl_ad = view
                    .findViewById(R.id.fl_ad);
 
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        VideoInfo info = null;
        VideoInfo info1 = null;
 
        int startP = position * 2;
        info = mList.get(startP);
        if (startP + 1 < mList.size()) {
            info1 = mList.get(startP + 1);
        }
        if (info == null)
            return view;
        if (info1 == null)
            return view;
 
        if (info != null) {
            if (null == info.getAdView()) {
                holder.fl_ad.setVisibility(View.GONE);
                holder.ll_video_show1.setVisibility(View.VISIBLE);
                try {
                    Glide.with(mContext).load(info.getPicture())
                            .placeholder(R.drawable.ic_default_cover)
                            .error(R.drawable.ic_default_cover)
                            .centerCrop().crossFade()
                            .into(holder.iv_movie_cover);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
 
                holder.tv_movie_title.setText(info.getName());
                holder.tv_movie_rating.setText(StringUtils.isBlank(info
                        .getTag()) ? "" : info
                        .getTag());
//            holder.tv_movie_play_num.setText(StringUtils.isBlank(info
//                    .getWatchCount()) ? "0" : info.getWatchCount());
                DecimalFormat df = new DecimalFormat("###.0");
                holder.tv_movie_play_num.setText(StringUtils.isBlank(info
                        .getWatchCount()) ? "0" : (Integer.parseInt(info
                        .getWatchCount())) / 10000 > 0 ? df.format(Integer.parseInt(info
                        .getWatchCount()) / 10000f) + "万" : info
                        .getWatchCount() + "");
                holder.tv_movie_comment_num.setText(StringUtils.isBlank(info
                        .getCommentCount()) ? "0" : info.getCommentCount());
            } else {
                holder.fl_ad.setVisibility(View.VISIBLE);
                holder.ll_video_show1.setVisibility(View.GONE);
                FrameLayout parentAd = (FrameLayout) info.getAdView().getParent();
                info.getAdView().render();
                if (null != parentAd) {
                    parentAd.removeAllViews();
                }
                holder.fl_ad.addView(info.getAdView());
            }
 
        } else {
            holder.ll_video_show1.setVisibility(View.INVISIBLE);
        }
        if (info1 != null) {
            holder.ll_video_show2.setVisibility(View.VISIBLE);
            if (StringUtils.isEmpty(info1.getPicture())) {
                // imageLoader.displayImage(info1.getVideo().getPicture(),
                // holder.iv_movie_cover1, options);
                Glide.with(mContext).load(info1.getPicture())
                        .placeholder(R.drawable.ic_default_cover)
                        .error(R.drawable.ic_default_cover)
                        .centerCrop().crossFade()
                        .into(holder.iv_movie_cover1);
            } else {
                try {
                    Glide.with(mContext).load(info1.getPicture())
                            .placeholder(R.drawable.ic_default_cover)
                            .error(R.drawable.ic_default_cover)
                            .centerCrop().crossFade()
                            .into(holder.iv_movie_cover1);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                // imageLoader.displayImage(info1.getPicture(),
                // holder.iv_movie_cover1, options);
            }
            holder.tv_movie_title1.setText(info1.getName());
            holder.tv_movie_rating1.setText(StringUtils.isBlank(info1
                    .getTag()) ? "" : info1
                    .getTag());
//            holder.tv_movie_play_num1.setText(StringUtils.isBlank(info1
//                    .getWatchCount()) ? "0" : info1.getWatchCount());
            DecimalFormat df = new DecimalFormat("###.0");
            holder.tv_movie_play_num1.setText(StringUtils.isBlank(info1
                    .getWatchCount()) ? "0" : (Integer.parseInt(info1
                    .getWatchCount())) / 10000 > 0 ? df.format(Integer.parseInt(info1
                    .getWatchCount()) / 10000f) + "万" : info1
                    .getWatchCount() + "");
            holder.tv_movie_comment_num1.setText(StringUtils.isBlank(info1
                    .getCommentCount()) ? "0" : info1.getCommentCount());
 
            if (info1.getAdInfo() != null)
                info1.getAdInfo().onExposured(holder.ll_video_show1);
        } else {
            holder.ll_video_show2.setVisibility(View.INVISIBLE);
        }
        if (info != null) {
            final VideoInfo myInfo = info;
            holder.ll_video_show1
                    .setOnClickListener(new View.OnClickListener() {
 
                        @Override
                        public void onClick(final View v) {
                            if (myInfo.getAdInfo() == null) {
                                myClick(myInfo, v);
                            } else {
                                if (myInfo.getAdInfo().isAPP() && BeibeiConstant.AD_CLICK_DOWNLOAD) {
                                    DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {
 
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                            int which) {
                                            switch (which) {
                                                case Dialog.BUTTON_POSITIVE:
                                                    myInfo.getAdInfo().onClicked(v);
                                                    dialog.dismiss();
                                                    break;
                                                case Dialog.BUTTON_NEGATIVE:
                                                    dialog.dismiss();
                                                    break;
                                            }
                                        }
                                    };
                                    AdPromptDialog.Builder builder = new AdPromptDialog.Builder(
                                            v.getContext()); // 先得到构造器
                                    builder.setPositiveButton("确认", dialogOnclicListener);
                                    builder.setNegativeButton("取消", dialogOnclicListener);
                                    builder.create().show();
                                } else {
                                    myInfo.getAdInfo().onClicked(v);
                                }
                            }
                        }
                    });
        }
        if (info1 != null) {
            final VideoInfo myInfo1 = info1;
 
            holder.ll_video_show2
                    .setOnClickListener(new View.OnClickListener() {
 
                        @Override
                        public void onClick(final View v) {
                            if (myInfo1.getAdInfo() == null) {
                                myClick(myInfo1, v);
                            } else {
                                if (myInfo1.getAdInfo().isAPP() && BeibeiConstant.AD_CLICK_DOWNLOAD) {
                                    DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {
 
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                            int which) {
                                            switch (which) {
                                                case Dialog.BUTTON_POSITIVE:
                                                    myInfo1.getAdInfo().onClicked(v);
                                                    dialog.dismiss();
                                                    break;
                                                case Dialog.BUTTON_NEGATIVE:
                                                    dialog.dismiss();
                                                    break;
                                            }
                                        }
                                    };
                                    AdPromptDialog.Builder builder = new AdPromptDialog.Builder(
                                            v.getContext()); // 先得到构造器
                                    builder.setPositiveButton("确认", dialogOnclicListener);
                                    builder.setNegativeButton("取消", dialogOnclicListener);
                                    builder.create().show();
                                } else {
                                    myInfo1.getAdInfo().onClicked(v);
                                }
                            }
                        }
                    });
        }
 
        return view;
    }
 
    private static class ViewHolder {
        ImageView iv_movie_cover;
        TextView tv_movie_title;
        TextView tv_movie_rating;
        TextView tv_movie_play_num;
        TextView tv_movie_comment_num;
        TextView tv_movie_title1;
        ImageView iv_movie_cover1;
        TextView tv_movie_rating1;
        TextView tv_movie_play_num1;
        TextView tv_movie_comment_num1;
        LinearLayout ll_video_show1;
        LinearLayout ll_video_show2;
        FrameLayout fl_ad;
    }
 
    private void myClick(VideoInfo videoInfo, View v) {
        Intent intent = new Intent(v.getContext(), VideoDetailActivity.class);
        intent.putExtra("video_info", videoInfo);
        v.getContext().startActivity(intent);
    }
}