Skip to content

Commit

Permalink
add new demo viewpager
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Nov 28, 2024
1 parent b19ab2f commit 1d812ce
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,UnusedAttribute">
<activity android:name=".ViewPager2Activity" />
<activity
android:name=".ViewPager2Activity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:exported="true"
android:screenOrientation="portrait" />
<activity
android:name=".ViewPagerDemoActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:exported="true"
android:screenOrientation="portrait" />
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {
binding.detailSubtitleActivity.setOnClickListener(this);
binding.changeCore.setOnClickListener(this);
binding.viewPager2Activity.setOnClickListener(this);
binding.viewPagerDemoActivity.setOnClickListener(this);
binding.changeCache.setOnClickListener(this);
binding.detailDownloadExoActivity.setOnClickListener(this);
binding.tvPlay.setOnClickListener(this);
Expand Down Expand Up @@ -230,6 +231,9 @@ public void onClick(View view) {
case R.id.view_pager2_activity:
JumpUtils.goToViewPager2Activity(this);
break;
case R.id.view_pager_demo_activity:
JumpUtils.goToViewPagerDemoActivity(this);
break;
case R.id.change_core:
i += 1;
if (i % 3 == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.example.gsyvideoplayer;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.gsyvideoplayer.adapter.ViewPagerDemoAdapter;
import com.example.gsyvideoplayer.databinding.ActivityViewPager2Binding;
import com.example.gsyvideoplayer.holder.RecyclerItemPlayNormalHolder;
import com.example.gsyvideoplayer.model.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerDemoActivity extends AppCompatActivity {
ActivityViewPager2Binding binding;

List<VideoModel> dataList = new ArrayList<>();

ViewPagerDemoAdapter viewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


binding = ActivityViewPager2Binding.inflate(getLayoutInflater());

View rootView = binding.getRoot();
setContentView(rootView);


resolveData();
viewPagerAdapter = new ViewPagerDemoAdapter(this, dataList);
binding.viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
binding.viewPager2.setAdapter(viewPagerAdapter);
binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
//大于0说明有播放
int playPosition = GSYVideoManager.instance().getPlayPosition();
if (playPosition >= 0) {
//对应的播放列表TAG
if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemPlayNormalHolder.TAG) && (position == playPosition)) {
RecyclerView.ViewHolder viewHolder = ((RecyclerView) binding.viewPager2.getChildAt(0)).findViewHolderForAdapterPosition(position);
if (viewHolder instanceof RecyclerItemPlayNormalHolder) {
RecyclerItemPlayNormalHolder recyclerItemNormalHolder = (RecyclerItemPlayNormalHolder) viewHolder;
if (recyclerItemNormalHolder.getPlayer().isInPlayingState()) {
recyclerItemNormalHolder.getPlayer().post(new Runnable() {
@Override
public void run() {
GSYVideoManager.instance().setDisplay(recyclerItemNormalHolder.getPlayer().getCurrentSurface());
}
});
GSYVideoManager.onResume(false);
}
}
} else {
GSYVideoManager.onPause();
}
}
}
});
}

@Override
public void onBackPressed() {
if (GSYVideoManager.backFromWindowFull(this)) {
return;
}
super.onBackPressed();
}

@Override
protected void onPause() {
super.onPause();
GSYVideoManager.onPause();
}

@Override
protected void onResume() {
super.onResume();
GSYVideoManager.onResume(false);
}

@Override
protected void onDestroy() {
super.onDestroy();
GSYVideoManager.releaseAllVideos();
}

private void resolveData() {
for (int i = 0; i < 19; i++) {
VideoModel videoModel = new VideoModel();
dataList.add(videoModel);
}
if (viewPagerAdapter != null) viewPagerAdapter.notifyDataSetChanged();
}


private void playPosition(int position) {
binding.viewPager2.postDelayed(new Runnable() {
@Override
public void run() {
RecyclerView.ViewHolder viewHolder = ((RecyclerView) binding.viewPager2.getChildAt(0)).findViewHolderForAdapterPosition(position);
if (viewHolder != null) {
RecyclerItemPlayNormalHolder recyclerItemNormalHolder = (RecyclerItemPlayNormalHolder) viewHolder;
recyclerItemNormalHolder.getPlayer().startPlayLogic();
}
}
}, 50);
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.gsyvideoplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.holder.RecyclerItemPlayNormalHolder;
import com.example.gsyvideoplayer.holder.RecyclerPageItemViewHolder;
import com.example.gsyvideoplayer.model.VideoModel;

import java.util.List;

public class ViewPagerDemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final static String TAG = "ViewPagerDemoAdapter";

private List<VideoModel> itemDataList = null;
private Context context = null;

public ViewPagerDemoAdapter(Context context, List<VideoModel> itemDataList) {
this.itemDataList = itemDataList;
this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == 2) {
View v = LayoutInflater.from(context).inflate(R.layout.layout_viewpager2_item, parent, false);
final RecyclerView.ViewHolder holder = new RecyclerItemPlayNormalHolder(context, v);
return holder;
} else {
View v = LayoutInflater.from(context).inflate(R.layout.layout_viewpager_demo_item, parent, false);
final RecyclerView.ViewHolder holder = new RecyclerPageItemViewHolder(context, v);
return holder;
}

}

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
if(position == 2) {
RecyclerItemPlayNormalHolder recyclerItemViewHolder = (RecyclerItemPlayNormalHolder) holder;
recyclerItemViewHolder.setRecyclerBaseAdapter(this);
recyclerItemViewHolder.onBind(position, itemDataList.get(position));
} else {
RecyclerPageItemViewHolder recyclerItemViewHolder = (RecyclerPageItemViewHolder) holder;
recyclerItemViewHolder.setRecyclerBaseAdapter(this);
recyclerItemViewHolder.setText(position + "");
}
}

@Override
public int getItemCount() {
return itemDataList.size();
}


@Override
public int getItemViewType(int position) {
if (position == 2) {
return 2;
}
return 1;
}

public void setListData(List<VideoModel> data) {
itemDataList = data;
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.example.gsyvideoplayer.holder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.model.VideoModel;
import com.example.gsyvideoplayer.video.SampleCoverVideo;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.HashMap;
import java.util.Map;

/**
* Created by guoshuyu on 2017/1/9.
*/

public class RecyclerItemPlayNormalHolder extends RecyclerItemBaseHolder {

public final static String TAG = "RecyclerView2List";

protected Context context;

SampleCoverVideo gsyVideoPlayer;

ImageView imageView;

GSYVideoOptionBuilder gsyVideoOptionBuilder;

public RecyclerItemPlayNormalHolder(Context context, View v) {
super(v);
this.context = context;
gsyVideoPlayer = v.findViewById(R.id.video_item_player);
imageView = new ImageView(context);
gsyVideoOptionBuilder = new GSYVideoOptionBuilder();
}

public void onBind(final int position, VideoModel videoModel) {

String url;
String title;
if (position % 2 == 0) {
url = "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4";
title = "这是title";
} else {
url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
title = "哦?Title?";
}


Map<String, String> header = new HashMap<>();
header.put("ee", "33");
int playPosition = GSYVideoManager.instance().getPlayPosition();
if (!GSYVideoManager.instance().isPlaying() && (position != playPosition)) {
//防止错位,离开释放
//gsyVideoPlayer.initUIState();
gsyVideoOptionBuilder.setIsTouchWiget(false)
//.setThumbImageView(imageView)
.setUrl(url).setVideoTitle(title).setCacheWithPlay(false).setRotateViewAuto(true).setLockLand(true).setPlayTag(TAG).setMapHeadData(header).setShowFullAnimation(true).setNeedLockFull(true).setPlayPosition(position).setVideoAllCallBack(new GSYSampleCallBack() {
@Override
public void onPrepared(String url, Object... objects) {
super.onPrepared(url, objects);
if (!gsyVideoPlayer.isIfCurrentIsFullscreen()) {
//静音
GSYVideoManager.instance().setNeedMute(true);
}

}

@Override
public void onAutoComplete(String url, Object... objects) {
super.onAutoComplete(url, objects);
}

@Override
public void onQuitFullscreen(String url, Object... objects) {
super.onQuitFullscreen(url, objects);
//全屏不静音
GSYVideoManager.instance().setNeedMute(true);
}

@Override
public void onEnterFullscreen(String url, Object... objects) {
super.onEnterFullscreen(url, objects);
GSYVideoManager.instance().setNeedMute(false);
gsyVideoPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]);
}
}).build(gsyVideoPlayer);


//增加title
gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE);

//设置返回键
gsyVideoPlayer.getBackButton().setVisibility(View.GONE);

//设置全屏按键功能
gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resolveFullBtn(gsyVideoPlayer);
}
});

gsyVideoPlayer.loadCoverImageBy(R.mipmap.xxx2, R.mipmap.xxx2);
}
}

/**
* 全屏幕按键处理
*/
private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) {
standardGSYVideoPlayer.startWindowFullscreen(context, true, true);
}

public SampleCoverVideo getPlayer() {
return gsyVideoPlayer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.gsyvideoplayer.holder;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.model.VideoModel;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;


/**
* Created by GUO on 2015/12/3.
*/
public class RecyclerPageItemViewHolder extends RecyclerItemBaseHolder {

public final static String TAG = "RecyclerPageItemViewHolder";

protected Context context ;
protected TextView textView ;

public RecyclerPageItemViewHolder(Context context, View v) {
super(v);
this.context = context;
textView = v.findViewById(R.id.viewpage_demo_text);
}

public void setText(String text) {
textView.setText(text);
}
}





Loading

0 comments on commit 1d812ce

Please sign in to comment.