直接看代码就行咯:
/**
* ViewPagerLayoutManager
*/
public class ViewPagerLayoutManagerActivity extends AppCompatActivity {
private static final String TAG = "ViewPagerActivity";
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private ViewPagerLayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_layout_manager);
initView();
initListener();
}
private void initView() {
mRecyclerView = findViewById(R.id.recycler);
mLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
mAdapter = new MyAdapter();
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
private void initListener() {
mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
@Override
public void onInitComplete() {
Log.e(TAG, "onInitComplete");
playVideo(0);
}
@Override
public void onPageRelease(boolean isNext, int position) {
Log.e(TAG, "释放位置:" + position + " 下一页:" + isNext);
int index = 0;
if (isNext) {
index = 0;
} else {
index = 1;
}
releaseVideo(index);
}
@Override
public void onPageSelected(int position, boolean isBottom) {
Log.e(TAG, "选中位置:" + position + " 是否是滑动到底部:" + isBottom);
playVideo(0);
}
}
);
}
private void playVideo(int position) {
View itemView = mRecyclerView.getChildAt(0);
final VideoView videoView = itemView.findViewById(R.id.video_view);
final ImageView imgPlay = itemView.findViewById(R.id.img_play);
final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
final RelativeLayout rootView = itemView.findViewById(R.id.root_view);
final MediaPlayer[] mediaPlayer = new MediaPlayer[1];
videoView.start();
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
mediaPlayer[0] = mp;
mp.setLooping(true);
imgThumb.animate().alpha(0).setDuration(200).start();
return false;
}
});
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
}
});
imgPlay.setOnClickListener(new View.OnClickListener() {
boolean isPlaying = true;
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
imgPlay.animate().alpha(1f).start();
videoView.pause();
isPlaying = false;
} else {
imgPlay.animate().alpha(0f).start();
videoView.start();
isPlaying = true;
}
}
});
}
private void releaseVideo(int index) {
View itemView = mRecyclerView.getChildAt(index);
final VideoView videoView = itemView.findViewById(R.id.video_view);
final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
final ImageView imgPlay = itemView.findViewById(R.id.img_play);
videoView.stopPlayback();
imgThumb.animate().alpha(1).start();
imgPlay.animate().alpha(0f).start();
}
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private int[] imgs = {R.mipmap.img_video_1, R.mipmap.img_video_2,R.drawable.animation_like};
private int[] videos = {R.raw.video_1, R.raw.video_2};
public MyAdapter() {
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_pager, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img_thumb.setImageResource(imgs[position % 2]);
holder.videoView.setVideoURI(Uri.parse("https://o6yh618n9.qnssl.com/7zAkFsjl_8682607551.mp4"));
// holder.videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videos[position % 2]));
}
@Override
public int getItemCount() {
return 20;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img_thumb;
VideoView videoView;
ImageView img_play;
RelativeLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
img_thumb = itemView.findViewById(R.id.img_thumb);
videoView = itemView.findViewById(R.id.video_view);
img_play = itemView.findViewById(R.id.img_play);
rootView = itemView.findViewById(R.id.root_view);
}
}
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dingmouren.example.layoutmanagergroup.activity.ViewPagerLayoutManagerActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:orientation="horizontal"
android:layout_marginTop="56dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="推荐"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近"
android:textColor="#f2f2f2"
android:textSize="17sp"
android:textStyle="bold"
/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/search_icon"
android:layout_alignParentRight="true"
android:tint="#f2f2f2"
android:layout_marginRight="16dp"
/>
</RelativeLayout>
<VideoView
android:id="@+id/line_bottom"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_above="@+id/bottom"
android:background="@android:color/white"
/>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textColor="@android:color/white"
android:text="首页"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#f2f2f2"
android:text="关注"
android:textSize="17sp"
android:textStyle="bold"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/add_bg"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#f2f2f2"
android:text="消息"
android:textSize="17sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#f2f2f2"
android:text="我"
android:textSize="17sp"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
item引用的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.dingmouren.example.layoutmanagergroup.widget.FullScreenVideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
/>
<ImageView
android:id="@+id/img_thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:clickable="false"
android:focusable="false"
android:src="@drawable/ic_launcher_background"
android:visibility="visible"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@mipmap/header_icon_2"
app:civ_border_color="@android:color/white"
app:civ_border_width="2dp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/circle_big_red"
android:src="@mipmap/add_icon"
android:tint="@android:color/white"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
/>
</RelativeLayout>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:drawableTop="@mipmap/heart_icon"
android:layout_marginTop="16dp"
android:text="1.6w"
android:textColor="@android:color/white"
android:gravity="center"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:drawableTop="@mipmap/msg_icon"
android:layout_marginTop="16dp"
android:text="1.6w"
android:textColor="@android:color/white"
android:gravity="center"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:drawableTop="@mipmap/share_icon"
android:layout_marginTop="16dp"
android:text="1.6w"
android:textColor="@android:color/white"
android:gravity="center"
/>
</LinearLayout>
<ImageView
android:id="@+id/img_play"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/play_arrow"
android:clickable="true"
android:focusable="true"
android:alpha="0"
android:layout_centerInParent="true"
/>
</RelativeLayout>
//自定义 LinearLayoutManager
public class ViewPagerLayoutManager extends LinearLayoutManager {
private static final String TAG = "ViewPagerLayoutManager";
private PagerSnapHelper mPagerSnapHelper;
private OnViewPagerListener mOnViewPagerListener;
private RecyclerView mRecyclerView;
private int mDrift;//位移,用来判断移动方向
public ViewPagerLayoutManager(Context context, int orientation) {
super(context, orientation, false);
init();
}
public ViewPagerLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
init();
}
private void init() {
mPagerSnapHelper = new PagerSnapHelper();
}
@Override
public void onAttachedToWindow(RecyclerView view) {
super.onAttachedToWindow(view);
mPagerSnapHelper.attachToRecyclerView(view);
this.mRecyclerView = view;
mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
//
}
/**
* 滑动状态的改变
* 缓慢拖拽-> SCROLL_STATE_DRAGGING
* 快速滚动-> SCROLL_STATE_SETTLING
* 空闲状态-> SCROLL_STATE_IDLE
* @param state
*/
@Override
public void onScrollStateChanged(int state) {
switch (state) {
case RecyclerView.SCROLL_STATE_IDLE:
View viewIdle = mPagerSnapHelper.findSnapView(this);
int positionIdle = getPosition(viewIdle);
if (mOnViewPagerListener != null && getChildCount() == 1) {
mOnViewPagerListener.onPageSelected(positionIdle,positionIdle == getItemCount() - 1);
}
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
View viewDrag = mPagerSnapHelper.findSnapView(this);
int positionDrag = getPosition(viewDrag);
break;
case RecyclerView.SCROLL_STATE_SETTLING:
View viewSettling = mPagerSnapHelper.findSnapView(this);
int positionSettling = getPosition(viewSettling);
break;
}
}
/**
* 监听竖直方向的相对偏移量
* @param dy
* @param recycler
* @param state
* @return
*/
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
this.mDrift = dy;
return super.scrollVerticallyBy(dy, recycler, state);
}
/**
* 监听水平方向的相对偏移量
* @param dx
* @param recycler
* @param state
* @return
*/
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
this.mDrift = dx;
return super.scrollHorizontallyBy(dx, recycler, state);
}
/**
* 设置监听
* @param listener
*/
public void setOnViewPagerListener(OnViewPagerListener listener){
this.mOnViewPagerListener = listener;
}
private RecyclerView.OnChildAttachStateChangeListener mChildAttachStateChangeListener = new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
if (mOnViewPagerListener != null && getChildCount() == 1) {
mOnViewPagerListener.onInitComplete();
}
}
@Override
public void onChildViewDetachedFromWindow(View view) {
if (mDrift >= 0){
if (mOnViewPagerListener != null) mOnViewPagerListener.onPageRelease(true,getPosition(view));
}else {
if (mOnViewPagerListener != null) mOnViewPagerListener.onPageRelease(false,getPosition(view));
}
}
};
}
public interface OnViewPagerListener {
/*初始化完成*/
void onInitComplete();
/*释放的监听*/
void onPageRelease(boolean isNext,int position);
/*选中的监听以及判断是否滑动到底部*/
void onPageSelected(int position,boolean isBottom);
}