package com.hhl.tubatu;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by HanHailong on 15/9/27.
 */
public class ClipViewPager extends ViewPager {

    public ClipViewPager(Context context) {
        super(context);
    }

    public ClipViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_UP) {
            View view = viewOfClickOnScreen(ev);
            if (view != null) {
                int index = indexOfChild(view);
                if (getCurrentItem() != index) {
                    setCurrentItem(indexOfChild(view));
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * @param ev
     * @return
     */
    private View viewOfClickOnScreen(MotionEvent ev) {
        int childCount = getChildCount();
        int[] location = new int[2];
        for (int i = 0; i < childCount; i++) {
            View v = getChildAt(i);
            v.getLocationOnScreen(location);
            int minX = location[0];
            int minY = getTop();

            int maxX = location[0] + v.getWidth();
            int maxY = getBottom();

            float x = ev.getX();
            float y = ev.getY();

            if ((x > minX && x < maxX) && (y > minY && y < maxY)) {
                return v;
            }
        }
        return null;
    }
}

@@ -275,24 +275,132 @@
 iewPager %7B%0A%0A
+    //%E9%BB%98%E8%AE%A4%E8%B7%9D%E7%A6%BB%0A    private final static float DISTANCE = 10;%0A    private float downX;%0A    private float downY;%0A%0A
     public C
@@ -638,63 +638,474 @@
 ) %7B%0A
-%0A        if (ev.getAction() == MotionEvent.ACTION_UP) %7B
+        if(ev.getAction() == MotionEvent.ACTION_DOWN)%7B%0A            downX = ev.getX();%0A            downY = ev.getY();%0A        %7Delse if (ev.getAction() == MotionEvent.ACTION_UP) %7B%0A%0A            float upX = ev.getX();%0A            float upY = ev.getY();%0A            //%E5%A6%82%E6%9E%9C up%E7%9A%84%E4%BD%8D%E7%BD%AE%E5%92%8Cdown %E7%9A%84%E4%BD%8D%E7%BD%AE %E8%B7%9D%E7%A6%BB %3E %E8%AE%BE%E7%BD%AE%E7%9A%84%E8%B7%9D%E7%A6%BB,%E5%88%99%E4%BA%8B%E4%BB%B6%E7%BB%A7%E7%BB%AD%E4%BC%A0%E9%80%92,%E4%B8%8D%E6%89%A7%E8%A1%8C%E4%B8%8B%E9%9D%A2%E7%9A%84%E7%82%B9%E5%87%BB%E5%88%87%E6%8D%A2%E4%BA%8B%E4%BB%B6%0A            if(Math.abs(upX - downX) %3E DISTANCE %7C%7C Math.abs(upY - downY) %3E DISTANCE)%7B%0A                return super.dispatchTouchEvent(ev);%0A            %7D%0A
 %0A   

修改bug - 滑动到最后一个界面继续滑动，如果up位置在图片外区域，就返回到上一个节目的bug