android fragment ,listview 嵌套横向HorizontalScrollView 实现HorizontalScrollView 到最左或者最右切换fragment

啥也不说了额,直接上代码,老铁门自己看!



import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;



public class MyHorizontalScrollView extends HorizontalScrollView {
    private boolean mCanScroll = false;
    private float downX;
    private int mParentWidth = 720;
    int slopDistance;
    private GestureDetector mGesture;

    public MyHorizontalScrollView(Context context) {
        super(context);
        initView();
    }

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

    public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    boolean isScroll = false;

    public void setScroll(boolean isScroll) {
        this.isScroll = isScroll;
    }

    public synchronized void initView() {
        slopDistance = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        mGesture = new GestureDetector(getContext(), mOnGesture);

    }

    float mDownX, mDownY, mLastX, mLastY;


    public void setmParentWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mParentWidth = wm.getDefaultDisplay().getWidth();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        Log.d("Intercep: ", "ListView");
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                mLastX = (int) event.getX();
                if (Math.abs(mLastX - mDownX) > slopDistance) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        getParent().requestDisallowInterceptTouchEvent(false);
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
//        boolean handled = super.dispatchTouchEvent(ev);
//        handled |= mGesture.onTouchEvent(ev);
        boolean handled = mGesture.onTouchEvent(ev);
        return handled ? true : super.dispatchTouchEvent(ev);
    }

    private GestureDetector.OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {


        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (Math.abs(distanceX) > Math.abs(distanceY)) {
                int scrollx = getScrollX();
                if ((scrollx == 0 && e1.getX() - e2.getX() <= -10)
                        || (getChildAt(0).getMeasuredWidth() <= (scrollx + mParentWidth) && e1.getX()
                        - e2.getX() >= 10)) {
                    return true;
                }
                return false;
            }
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (getChildCount() == 0) return false;
            ViewGroup child = (ViewGroup) getChildAt(0);
            for (int i = 0; i < child.getChildCount(); i++) {
                View subChild = child.getChildAt(i);
                if (ViewUtils.isEventWithinView(e, subChild)) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(e, subChild, i, subChild.getId());
                    }
                    break;
                }
            }
            return true;
        }

    };

    public OnScrollViewItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnScrollViewItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnScrollViewItemClickListener {
        void onItemClick(MotionEvent ev, View view, int position, long id);
    }
}


public static boolean isEventWithinView(MotionEvent e, View child) {
    Rect viewRect = new Rect();
    int[] childPosition = new int[2];
    child.getLocationOnScreen(childPosition);
    int left = childPosition[0];
    int right = left + child.getWidth();
    int top = childPosition[1];
    int bottom = top + child.getHeight();
    viewRect.set(left, top, right, bottom);
    return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
 


你可能感兴趣的:(android,ViewPager,Android)