ViewPager过半(1/2)滑动翻页,超过1/2滑动下页

ViewPager过半(1/2)滑动翻页,超过1/2滑动下页

公司需求要求必须手动触摸滑动超过1/2的时候松开可以滑动下一页,没有超过1/2返回原页,首先肯定是重写viewpager,但是发现效果比较卡顿,只能从源码下手

经过分析,源码滑动的逻辑处理在此处,truncator的属性代表判断的比例值

private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {  
        int targetPage;  
        if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {  
            targetPage = velocity > 0 ? currentPage : currentPage + 1;  
        } else {  
            final float truncator = currentPage >= mCurItem ? 0.4f : 0.6f;  
            targetPage = (int) (currentPage + pageOffset + truncator);  
        }  
  
        if (mItems.size() > 0) {  
            final ItemInfo firstItem = mItems.get(0);  
            final ItemInfo lastItem = mItems.get(mItems.size() - 1);  
  
            // Only let the user target pages we have items for  
            targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));  
        }  
  
        return targetPage;  
    }  

将ViewPager的源码修改为以下,改为0.5f:0.5f

private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {  
        int targetPage;  
        if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {  
            targetPage = velocity > 0 ? currentPage : currentPage + 1;  
        } else {  
            final float truncator = currentPage >= mCurItem ? 0.5f : 0.5f;  
            targetPage = (int) (currentPage + pageOffset + truncator);  
        }  
  
        if (mItems.size() > 0) {  
            final ItemInfo firstItem = mItems.get(0);  
            final ItemInfo lastItem = mItems.get(mItems.size() - 1);  
  
            // Only let the user target pages we have items for  
            targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));  
        }  
  
        return targetPage;  
    }

测试完全解决问题,如果使用只需要复制Viewpager和Adapter的文件到项目就可以了,没有必要全部拷入项目,现在我将viewpager相关精简的源码单独剥离出来,可以下载研究下:,希望帮助小伙伴!!!!!