package com.example.pagersnaphelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class PagerSnapHelperActivity extends Activity {
/**
* UI
*/
// recycleView
private RecyclerView mRecyclerView;
// adapter
private MyAdapter myAdapter;
/**
* 数据
*/
//data
private ArrayList mDataList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_pager_activity);
// -----------创建数据集-------------
initData();
// 纵向List
initUI();
}
//视频数据源
private void initData() {
mDataList.add("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319104618910544.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/17/mp4/190317150237409904.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314223540373995.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314102306987969.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/13/mp4/190313094901111138.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4");
mDataList.add("http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4");
}
@SuppressLint("WrongConstant")
public void initUI() {
// ---RecyclerView---
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_vertical);
mRecyclerView.setNestedScrollingEnabled(false);
// PagerSnapHelper
PagerSnapHelper snapHelper = new PagerSnapHelper() {
// 在 Adapter的 onBindViewHolder 之后执行
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
// TODO 找到对应的Index
return super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
}
// 在 Adapter的 onBindViewHolder 之后执行
@Nullable
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
// TODO 找到对应的View
return super.findSnapView(layoutManager);
}
};
snapHelper.attachToRecyclerView(mRecyclerView);
// ---布局管理器---
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
// 默认是Vertical (HORIZONTAL则为横向列表)
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//
mRecyclerView.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter(R.layout.recycle_pager_item,mDataList);
// 设置Adapter
mRecyclerView.setAdapter(myAdapter);
}
}