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");
|
}
|
}
|