admin
2021-02-03 1981dee5aec45793d3c4ebdbc4e637528c71b3c5
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package com.weikou.beibeivideo.ui.media;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
 
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.RetainViewFragment;
import com.lcjian.library.util.common.DimenUtils;
import com.lcjian.library.util.common.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.PushEpisode;
import com.weikou.beibeivideo.entity.VideoInfo;
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
import com.weikou.beibeivideo.entity.video.ChangeVideoEvent;
import com.weikou.beibeivideo.ui.video.IVideoClickListener;
import com.weikou.beibeivideo.ui.video.VideoColumn2Adapter;
import com.weikou.beibeivideo.util.ad.AdUtil;
import com.weikou.beibeivideo.util.ad.ExpressAdManager;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.List;
 
import de.greenrobot.event.EventBus;
 
/**
 * 播放详情--选集页面
 */
public class VideoEpisodeFragment extends RetainViewFragment implements
        OnClickListener {
 
    private static VideoInfo mVideoInfo;
 
    private int mPlayingPosition;
 
    private ViewPager vp_episode;
 
    private RecyclerView rv_video_related_video, rv_video_guess_like;
 
    private ScrollView sv_detail;
 
    private EpisodePagerAdapter mEpisodePagerAdapter;
 
    private TextView video_score, video_year, video_type,
            video_actor, video_introduction, video_more_text;
    private ImageView video_more_image;
    private LinearLayout video_introduction_more;
    private FrameLayout fl_native_ad_1;
    private LinearLayout ll_relative_video;
 
    public static boolean isShowMore = false;// 是否显示更多
    public static int page_varietyshow = 0;// 综艺节目的分页
    int maxDescripLine = 2;
 
 
    final String TAG = "VideoEpisodeFragment";
 
    public static VideoEpisodeFragment newInstance(VideoInfo videoInfo,
                                                   int playingPostion) {
        VideoEpisodeFragment videoEpisodeFragment = new VideoEpisodeFragment();
        Bundle args = new Bundle();
        args.putSerializable("video_info", videoInfo);
        args.putInt("playing_position", playingPostion);
        videoEpisodeFragment.setArguments(args);
        return videoEpisodeFragment;
    }
 
 
    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 int getContentResource() {
        return R.layout.video_episode_fragment;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        sv_detail = contentView.findViewById(R.id.sv_detail);
        mVideoInfo = (VideoInfo) getArguments().getSerializable("video_info");
        mPlayingPosition = getArguments().getInt("playing_position");
        if (getArguments() != null)
            getArguments().clear();
        // ******简介*******
        video_score = contentView.findViewById(R.id.video_score);
        video_year = contentView.findViewById(R.id.video_year);
        video_type = contentView.findViewById(R.id.video_type);
        video_actor = contentView.findViewById(R.id.video_actor);
        video_introduction = contentView
                .findViewById(R.id.video_introduction);
        video_introduction_more = contentView
                .findViewById(R.id.video_introduction_more);
        video_more_image = contentView
                .findViewById(R.id.video_more_image);
        video_more_text = contentView
                .findViewById(R.id.video_more_text);
 
        video_introduction_more.setOnClickListener(this);
        vp_episode = contentView.findViewById(R.id.vp_episode);
 
        rv_video_related_video = contentView
                .findViewById(R.id.rv_video_related_video);
 
        rv_video_guess_like = contentView
                .findViewById(R.id.rv_video_guess_like);
 
        fl_native_ad_1 = contentView
                .findViewById(R.id.fl_native_ad_1);
 
        ll_relative_video = contentView
                .findViewById(R.id.ll_relative_video);
 
        contentView.findViewById(R.id.ll_container).setFocusable(true);
        contentView.findViewById(R.id.ll_container).setFocusableInTouchMode(true);
 
        rv_video_guess_like.setHasFixedSize(true);
        rv_video_guess_like.setNestedScrollingEnabled(false);
//
        rv_video_related_video.setHasFixedSize(true);
        rv_video_related_video.setNestedScrollingEnabled(false);
 
 
        if (mVideoInfo == null)
            return;
 
        mEpisodePagerAdapter = new EpisodePagerAdapter(
                getChildFragmentManager(), mVideoInfo, mPlayingPosition);
        //
        vp_episode.setAdapter(mEpisodePagerAdapter);
        // 指示当前播放页---hxh
 
        vp_episode.post(new Runnable() {
 
            @Override
            public void run() {
                int page = (mPlayingPosition + 1) % 20 == 0 ? (mPlayingPosition + 1) / 20
                        : (mPlayingPosition + 1) / 20 + 1;
                if (page < 1)
                    return;
                vp_episode.setCurrentItem(page - 1);
                try {
                    mEpisodePagerAdapter.notifyDataSetChanged();
                } catch (Exception e) {
 
                }
            }
        });
 
        getRelativeVideos();
        guessLike();
 
 
        video_introduction.setHeight(video_introduction.getLineHeight()
                * maxDescripLine);
        video_introduction.post(new Runnable() {
 
            @Override
            public void run() {
                video_introduction_more.setVisibility(video_introduction
                        .getLineCount() > maxDescripLine ? View.VISIBLE
                        : View.GONE);
 
            }
        });
        getIntroductionInfo();
    }
 
    private void getIntroductionInfo() {
        Log.i("", "");
        video_score.setText("地区:" + (StringUtils.isEmpty(mVideoInfo.getArea()) ? "" : mVideoInfo.getArea()));
        video_year.setText("年份:" + mVideoInfo.getYear());
        if (mVideoInfo.getVideoType() != null)
            video_type.setText("类型:" + (StringUtils.isEmpty(mVideoInfo.getVideoType().getName()) ? "未知" : mVideoInfo.getVideoType().getName()));
        video_actor.setText("主演:" + mVideoInfo.getMainActor());
        if (!StringUtils.isBlank(mVideoInfo.getIntroduction())) {
            video_introduction_more.setVisibility(View.VISIBLE);
            video_introduction.setText(Html.fromHtml(mVideoInfo.getIntroduction()));
        } else {
            video_introduction.setText("");
            video_introduction.setVisibility(View.GONE);
            video_introduction_more.setVisibility(View.GONE);
        }
    }
 
    private void getRelativeVideos() {
        SharedPreferences preferences = getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.getRelativeVideos(getContext(), uid,
                mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                    .create();
                            final List<VideoInfo> videoInfos = gson.fromJson(
                                    jsonObject.getJSONObject("Data")
                                            .getJSONArray("data").toString(),
                                    new TypeToken<List<VideoInfo>>() {
                                    }.getType());
 
                            if (videoInfos == null || videoInfos.size() == 0) {
                                ll_relative_video.setVisibility(View.GONE);
                            } else {
                                ll_relative_video.setVisibility(View.VISIBLE);
                                final VideoColumn2Adapter adapter = new VideoColumn2Adapter(getActivity(), getContext(), videoInfos, false, 2, new IVideoClickListener() {
                                    @Override
                                    public void onClick(VideoInfo video) {
                                        ChangeVideoEvent changeVideoEvent = new ChangeVideoEvent();
                                        changeVideoEvent.setVideoInfo(video);
                                        changeVideoEvent.setTag("VideoPlayerFragment");
                                        EventBus.getDefault().post(changeVideoEvent);
                                    }
                                });
                                adapter.initRecyclerViewDisplayWidthColumn2(rv_video_related_video);
 
                                if (getActivity() == null)
                                    return;
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (rv_video_related_video != null) {
                                            rv_video_related_video.setAdapter(adapter);
                                        }
                                        if (adRunnable != null)
                                            rv_video_related_video.post(adRunnable);
                                    }
                                });
                            }
                        }
                    }
                });
    }
 
    private Runnable adRunnable = new Runnable() {
        @Override
        public void run() {
            loadAD2();
        }
    };
 
    private void guessLike() {
        SharedPreferences preferences = getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.guessLike(getContext(), uid, mVideoInfo.getId(),
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                    .create();
                            List<VideoInfo> videoInfos = gson.fromJson(
                                    jsonObject.getJSONObject("Data")
                                            .getJSONArray("data").toString(),
                                    new TypeToken<List<VideoInfo>>() {
                                    }.getType());
                            final VideoColumn2Adapter adapter = new VideoColumn2Adapter(getActivity(), getContext(), videoInfos, false, 2, new IVideoClickListener() {
                                @Override
                                public void onClick(VideoInfo video) {
                                    ChangeVideoEvent changeVideoEvent = new ChangeVideoEvent();
                                    changeVideoEvent.setVideoInfo(video);
                                    changeVideoEvent.setTag("VideoPlayerFragment");
                                    EventBus.getDefault().post(changeVideoEvent);
                                }
                            });
                            adapter.initRecyclerViewDisplayWidthColumn2(rv_video_guess_like);
                            if (getActivity() != null)
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (rv_video_guess_like != null)
                                            rv_video_guess_like
                                                    .setAdapter(adapter);
                                    }
                                });
 
//                            sv_detail.scrollTo(0, 0);
                        }
                    }
                });
    }
 
    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        MobclickAgent.onPageStart("播放详情—选集");
        Log.i(TAG,"onResume");
    }
 
    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        isShowMore = false;
        page_varietyshow = 0;
        MobclickAgent.onPageEnd("播放详情—选集");
        Log.i(TAG,"onPause");
    }
 
    public void onEventMainThread(VideoInfo info) {
        int playingPosition = 0;
        if (mPlayingPosition != playingPosition) {
            mPlayingPosition = playingPosition;
        }
        mEpisodePagerAdapter = new EpisodePagerAdapter(
                getChildFragmentManager(), info, mPlayingPosition);
        //
        vp_episode.setAdapter(mEpisodePagerAdapter);
    }
 
    public void onEventMainThread(PushEpisode info) {
        if (mVideoInfo == null)
            return;
        if (mVideoInfo.getShowType() == 1) {// 综艺 如果是综艺的话就分页加载
            if (info.getEpisodeNum() > 10) {
                page_varietyshow = info.getEpisodeNum() / 10 + 1;
                isShowMore = true;
            }
        } else {
            if (info.getEpisodeNum() > 12) {
                isShowMore = true;
            }
        }
        mEpisodePagerAdapter.setPlayingPosition(info.getEpisodeNum());
    }
 
    boolean isExpand;
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_introduction_more:
                isExpand = !isExpand;
                video_introduction.clearAnimation();
                final int deltaValue;
                final int startValue = video_introduction.getHeight();
                int durationMillis = 100;
                if (isExpand) {
                    video_introduction_more.setVisibility(View.VISIBLE);
                    deltaValue = video_introduction.getLineHeight()
                            * video_introduction.getLineCount() - startValue;
                    RotateAnimation animation = new RotateAnimation(0, 180,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    video_more_image.startAnimation(animation);
                    video_more_text.setText("点击收起");
 
                } else {
                    video_introduction_more.setVisibility(View.VISIBLE);
                    deltaValue = video_introduction.getLineHeight()
                            * maxDescripLine - startValue;
                    RotateAnimation animation = new RotateAnimation(180, 0,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    video_more_image.startAnimation(animation);
                    video_more_text.setText("查看更多");
 
                }
                Animation animation = new Animation() {
                    protected void applyTransformation(float interpolatedTime,
                                                       Transformation t) {
                        video_introduction.setHeight((int) (startValue + deltaValue
                                * interpolatedTime));
                    }
                };
                animation.setDuration(durationMillis);
                video_introduction.startAnimation(animation);
                break;
        }
    }
 
    /**
     * 广点通原生广告
     */
    // 加载播放页第二条广告
    private void loadAD2() {
        new ExpressAdManager(AdUtil.AD_TYPE.gdt2, getContext()).loadVideoDetailRelativeVideoBottomAd(new ExpressAdManager.IAdLoadListener() {
            @Override
            public void onSuccess(List<ExpressAdContainer> adList) {
                if (adList != null && adList.size() > 0) {
                    ExpressAdManager.renderAd(getActivity(), adList.get(0), new ExpressAdManager.IAdRenderListener() {
                        @Override
                        public void onRenderSuccess(List<ExpressAdContainer> adList) {
                            ExpressAdManager.fillAd(adList.get(0), fl_native_ad_1);
                        }
 
                        @Override
                        public void onRenderFail(List<ExpressAdContainer> adList) {
 
                        }
                    }, new ExpressAdManager.IAdEventListener() {
 
                        @Override
                        public void closeAd(ExpressAdContainer ad) {
                            fl_native_ad_1.removeAllViews();
                        }
                    });
                } else {
                    new ExpressAdManager(AdUtil.AD_TYPE.csj, getContext()).loadVideoDetailRelativeVideoBottomAd(new ExpressAdManager.IAdLoadListener() {
                        @Override
                        public void onSuccess(List<ExpressAdContainer> adList) {
                            if (adList != null && adList.size() > 0) {
                                ExpressAdManager.renderAndFillAd(getActivity(), adList.get(0), fl_native_ad_1, new ExpressAdManager.IAdEventListener() {
                                    @Override
                                    public void closeAd(ExpressAdContainer ad) {
                                        fl_native_ad_1.removeAllViews();
                                    }
                                });
                            }
                        }
                    });
                }
 
            }
        });
    }
 
    @Override
    public void onDestroy() {
        if (rv_video_related_video != null && adRunnable != null)
            rv_video_related_video.removeCallbacks(adRunnable);
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG,"onDestroyView");
    }
}