转载请注明:https://blog.csdn.net/u012854870/article/details/84984066
我们在使用ViewPager的时候多多少少都会遇到一些坑,这里今天就不细说了。
Google 在 Android 24.2.0 的support 包中添加了SnapHelper,SnapHelper是对RecyclerView的拓展,结合RecyclerView使用,能很方便的做出一些炫酷的效果。SnapHelper到底有什么功能呢?SnapHelper旨在支持RecyclerView的对齐方式,也就是通过计算对齐RecyclerView中TargetView 的指定点或者容器中的任何像素点。
(1) LinearSnapHelperLinearSnapHelper
使当前Item居中显示,类似ViewPager效果,但是又可以快速滑动(滑动多页)。代码如下
LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
//将SnapHelper attach 到RecyclrView
linearSnapHelper.attachToRecyclerView(recyclerView);
2) PagerSnapHelper (在Android 25.1.0 support 包加入的)PagerSnapHelper
的展示效果和LineSnapHelper
是一样的,只是PagerSnapHelper 限制一次只能滑动一页,不能快速滑动。代码如下:
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
//将SnapHelper attach 到RecyclrView
pagerSnapHelper.attachToRecyclerView(recyclerView);
至此我们可以根据自己的需求选择合适的SnapHelper实现ViewPager翻页效果。
ViewPager本身帮我们实现了OnPageChangeListener我们只需要添加就可以,而我们使用RecycleView实现的ViewPager效果本身是没有OnPageChangeListener事件的实现的,我们可以利用RecyclerView.OnScrollListener实现此功能。代码如下:
/**
* Created by pangli on 2018/12/13 9:43
* 备注:RecyclerView实现类似ViewPager的PageChangeListener监听
*/
public class RecyclerViewPageChangeListenerHelper extends RecyclerView.OnScrollListener {
private SnapHelper snapHelper;
private OnPageChangeListener onPageChangeListener;
private int oldPosition = -1;//防止同一Position多次触发
public RecyclerViewPageChangeListenerHelper(SnapHelper snapHelper, OnPageChangeListener onPageChangeListener) {
this.snapHelper = snapHelper;
this.onPageChangeListener = onPageChangeListener;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (onPageChangeListener != null) {
onPageChangeListener.onScrolled(recyclerView, dx, dy);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int position = 0;
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//获取当前选中的itemView
View view = snapHelper.findSnapView(layoutManager);
if (view != null) {
//获取itemView的position
position = layoutManager.getPosition(view);
}
if (onPageChangeListener != null) {
onPageChangeListener.onScrollStateChanged(recyclerView, newState);
//newState == RecyclerView.SCROLL_STATE_IDLE 当滚动停止时触发防止在滚动过程中不停触发
if (newState == RecyclerView.SCROLL_STATE_IDLE && oldPosition != position) {
oldPosition = position;
onPageChangeListener.onPageSelected(position);
}
}
}
public interface OnPageChangeListener {
void onScrollStateChanged(RecyclerView recyclerView, int newState);
void onScrolled(RecyclerView recyclerView, int dx, int dy);
void onPageSelected(int position);
}
}
至此我们就实现了RecyclerView的OnPageChangeListener功能。
使用:
LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(4);
}
}, 5000);
recyclerView.addOnScrollListener(new RecyclerViewPageChangeListenerHelper(linearSnapHelper,
new RecyclerViewPageChangeListenerHelper.OnPageChangeListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
@Override
public void onPageSelected(int position) {
ToastUtils.showToastShort(mContext, "选中" + position);
Log.e("Zorro", "选中" + position);
}
}));