本文主要介绍采用RecyclerView配合PagerSnapHelper实现短视频滑动播放内容。
短视频.gif
1. 主页内容构建
主页布局文件定义RecyclerView,为RecyclerView建立对应适配器。
android:id="@+id/rv_little_video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
适配器条目中添加视频播放容器FrameLayout及封面ImageVIew.
android:id="@+id/fl_content_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
android:id="@+id/iv_thumb_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
2.定义RecyclerView滑动管理
PagerSnapHelper 结合 LinearLayoutManager 实现滑动管理,实现监听任务。
PagerSnapHelper介绍
PagerSnapHelper can help achieve a similar behavior to
ViewPager. Set both RecyclerView and the items of the RecyclerView.Adapter to have android.view.ViewGroup.LayoutParams#MATCH_PARENT height and width and then attach PagerSnapHelper to the RecyclerView using #attachToRecyclerView(RecyclerView)}.
翻译:PagerSnapHelper可以帮助实现与以下类似的行为
ViewPager。 将RecyclerView和RecyclerView.Adapter的项目都设置为具有android.view.ViewGroup.LayoutParams#MATCH_PARENT的高度和宽度,然后使用#attachToRecyclerView(RecyclerView)}将PagerSnapHelper附加到RecyclerView。
自定义RecyclerView管理器
RecyclerView管理器为LinearLayoutManager 时,默认为纵向滑动,如果想采用横向滑动,就设置其滑动方向为RecyclerView.HORIZONTAL。同理,我们也可以这样采用setOrientation(RecyclerView.HORIZONTAL) 方法去改变滑动方向。
public class PagerLayoutManager extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener {
private OnPageChangedListener mOnPageChangedListener;
private PagerSnapHelper mSnapHel