- 效果如下
1.添加网路权限
<uses-permission android:name="android.permission.INTERNET" />
2.导入依赖
implementation 'cn.jzvd:jiaozivideoplayer:7.0.5'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.android.support:recyclerview-v7:30.0.0'
3.item_video_page.xml页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<cn.jzvd.JzvdStd
android:id="@+id/jz_video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
4.VideoPageAdapter类
public class VideoPageAdapter extends RecyclerView.Adapter<VideoPageAdapter.VideoViewHolder> {
private Context context;
private List<String> list;
public VideoPageAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_video_page,
parent, false));
}
@Override
public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
holder.jz_video.setUp(list.get(position), String.format("第%d个视频", position + 1),
JzvdStd.STATE_NORMAL);
if (position==0){
holder.jz_video.startVideo();
}
Glide.with(context).load(list.get(position)).into(holder.jz_video.thumbImageView);
}
@Override
public int getItemCount() {
return Math.max(list.size(), 0);
}
public static class VideoViewHolder extends RecyclerView.ViewHolder {
public JzvdStd jz_video;
public VideoViewHolder(@NonNull View itemView) {
super(itemView);
jz_video = itemView.findViewById(R.id.jz_video);
}
}
}
5.MainActivity布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
6.MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView main_recycler_view;
private List<String> urlList;
private PagerSnapHelper snapHelper;
private VideoPageAdapter videoPageAdapter;
private LinearLayoutManager linearLayoutManager;
private int currentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
addListener();
}
private void initView() {
main_recycler_view = findViewById(R.id.main_recycler_view);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
main_recycler_view.setLayoutManager(linearLayoutManager);
snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(main_recycler_view);
videoPageAdapter = new VideoPageAdapter(this, urlList);
main_recycler_view.setAdapter(videoPageAdapter);
}
private void initData() {
urlList = new ArrayList<>();
urlList.add("https://txmov2.a.yximgs.com/upic/2020/12/20/17/BMjAyMDEyMjAxNzE1MzhfNTE2Mzk3NTlfNDEwMjU3Njk1NDRfMl8z_b_B7e41ba62101c94976842bcc7af03dc97.mp4?tag=1-1608534403-xpcwebfeatured-0-hyhdk1sbcw-602f13ef885c2a01&clientCacheKey=3x8ivifqcq8cnhe_b.mp4&tt=b&di=6fa1a076&bp=10004");
urlList.add("https://txmov2.a.yximgs.com/upic/2020/12/11/18/BMjAyMDEyMTExODMzMDNfMjE2OTQzMzE3XzQwNTQwOTk4OTYxXzFfMw==_b_Bec32850fcb750498a083b0feb762aafc.mp4?tag=1-1608535635-xpcwebfeatured-0-nt0jgld1yc-12ccbe0339e55412&clientCacheKey=3xni5wzrxs97mek_b.mp4&tt=b&di=6fa1a076&bp=10004");
urlList.add("https://txmov2.a.yximgs.com/upic/2020/12/15/17/BMjAyMDEyMTUxNzQ4NTlfMTExMjEyN180MDc0NDYyNzg0NF8xXzM=_b_B27dd445721f9eece05c9687b06b8a24c.mp4?tag=1-1608537145-xpcwebfeatured-0-nzsggdddww-9d0bed1c999a8027&clientCacheKey=3xkf3nar7n46f5e_b.mp4&tt=b&di=6fa1a076&bp=10004");
}
private void addListener() {
main_recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE://停止滚动
View view = snapHelper.findSnapView(linearLayoutManager);
//当前固定后的item,position
assert view != null;
int position = recyclerView.getChildAdapterPosition(view);
if (currentPosition != position) {
//如果当前position 和上一次固定后的position相同,说明是同一个item,只不过是滑动了一点点,没有切换到下一个item
JzvdStd.releaseAllVideos();//释放所有资源
RecyclerView.ViewHolder viewHolder =
recyclerView.getChildViewHolder(view);
if (viewHolder != null && viewHolder instanceof VideoPageAdapter.VideoViewHolder) {
((VideoPageAdapter.VideoViewHolder) viewHolder).jz_video.startVideo();
}
}
currentPosition = position;
break;
case RecyclerView.SCROLL_STATE_DRAGGING://拖动
break;
case RecyclerView.SCROLL_STATE_SETTLING://惯性滑动
break;
}
}
});
}
@Override
protected void onPause() {
super.onPause();
JzvdStd.releaseAllVideos();
}
}
版权声明:本文为PatrickSta原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。