首先做数据上的处理
public static class LoopViewPagerAdapter extends PagerAdapter{
.....
LoopViewPagerAdapter(Context context, ArrayList<Integer> imgIds){
this.context = context;
this.ids.add(imgIds.get(imgIds.size()-1)); 将原本的最后一页复制一份放到第一页
this.ids.addAll(imgIds);
this.ids.add(imgIds.get(0)); 将原本的第一页复制一份放到最后一页
views = new View[ids.size()];
}
.....
}
配合OnPageChangeListener 即可实现无限循环
private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
LogUtils.d("LoopViewPager onPageSelected --> " + loopViewPagerAdapter.getCount() + " : " + position);
if (position == 0){
// 滑动到第一页的时候直接跳到倒数第二页,因为两页内容完全一致,跳的过程不会被人眼捕捉到
setCurrentItem(loopViewPagerAdapter.getCount() - 2, false);
}
if (position == loopViewPagerAdapter.getCount() - 1){
// 同理滑动到倒数第一页的时候直接跳到第二页
setCurrentItem(1, false);
}
}
...
}
借助OnPageChangeListener 的 onPageScrolled 滑动进度给指定的Item放大和缩小
private static final float BASE_SCALE = 0.9f; // 为方便调整放大幅度,设立的最小倍数
private static final float BASE_INCREMENT = 0.2f; // 最大放大倍数与最小放大倍数的差值
private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
...
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
float zoom = BASE_SCALE + (BASE_INCREMENT - Math.abs(positionOffset * BASE_INCREMENT));
loopViewPagerAdapter.getViews(position).setScaleX(zoom);
loopViewPagerAdapter.getViews(position).setScaleY(zoom);
if (positionOffset > 0) {
// 左滑,右侧放大
View leftView = loopViewPagerAdapter.getViews(position + 1);
if (leftView != null){
float zoomNext = BASE_SCALE + Math.abs(positionOffset * BASE_INCREMENT);
leftView.setScaleX(zoomNext);
leftView.setScaleY(zoomNext);
}
}
if (positionOffset < 0) {
// 右滑,左侧放大