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 androidx.viewpager.widget.ViewPager; import androidx.recyclerview.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.JsonUtil; import com.weikou.beibeivideo.util.ad.AdUtil; import com.weikou.beibeivideo.util.ad.ExpressAdManager; import org.apache.http.Header; import org.greenrobot.eventbus.Subscribe; import org.json.JSONObject; import java.util.List; import org.greenrobot.eventbus.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"; private String from; public static VideoEpisodeFragment newInstance(VideoInfo videoInfo, int playingPostion, int pageSize, String from) { VideoEpisodeFragment videoEpisodeFragment = new VideoEpisodeFragment(); Bundle args = new Bundle(); args.putSerializable("video_info", videoInfo); args.putInt("playing_position", playingPostion); args.putInt("pageSize", pageSize); args.putString("from", from); 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; } private int pageSize = 100; @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"); pageSize = getArguments().getInt("pageSize", 100); 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, pageSize, from); // 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")) { final List videoInfos = JsonUtil.videoGson.fromJson( jsonObject.getJSONObject("Data") .getJSONArray("data").toString(), new TypeToken>() { }.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, "relative", new IVideoClickListener() { @Override public void onClick(VideoInfo video) { ChangeVideoEvent changeVideoEvent = new ChangeVideoEvent(); changeVideoEvent.setVideoInfo(video); changeVideoEvent.setTag("VideoPlayerFragment"); changeVideoEvent.setFrom("relative"); 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")) { List videoInfos = JsonUtil.videoGson.fromJson( jsonObject.getJSONObject("Data") .getJSONArray("data").toString(), new TypeToken>() { }.getType()); final VideoColumn2Adapter adapter = new VideoColumn2Adapter(getActivity(), getContext(), videoInfos, false, 2, "guessLike", new IVideoClickListener() { @Override public void onClick(VideoInfo video) { ChangeVideoEvent changeVideoEvent = new ChangeVideoEvent(); changeVideoEvent.setVideoInfo(video); changeVideoEvent.setTag("VideoPlayerFragment"); changeVideoEvent.setFrom("guessLike"); 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); Log.i(TAG, "onResume"); } @Override public void onPause() { super.onPause(); EventBus.getDefault().unregister(this); isShowMore = false; page_varietyshow = 0; Log.i(TAG, "onPause"); } @Subscribe public void onEventMainThread(VideoInfo info) { int playingPosition = 0; if (mPlayingPosition != playingPosition) { mPlayingPosition = playingPosition; } pageSize = info.getPageSize(); mEpisodePagerAdapter = new EpisodePagerAdapter( getChildFragmentManager(), info, mPlayingPosition, pageSize, from); // vp_episode.setAdapter(mEpisodePagerAdapter); } @Subscribe 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; } } @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"); } }