android 在一个scrollView里面嵌套一个需要滑动的控件(listView、gridView)

package cn.via.dageeeOrderFood.widget;



import android.content.Context;

import android.graphics.PointF;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.ListView;



/**

 * Created by Heyiyong on 2014-2-27 下午10:34.

 */

public class MyListView extends ListView {



    /** 触摸时按下的点 **/

    PointF downP = new PointF();

    /** 触摸时当前的点 **/

    PointF curP = new PointF();



    public MyListView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }



    public MyListView(Context context) {

        super(context);

    }



    public MyListView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }



    @Override

    public boolean onInterceptTouchEvent(MotionEvent arg0) {

        //当拦截触摸事件到达此位置的时候,返回true,

        //说明将onTouch拦截在此控件,进而执行此控件的onTouchEvent

        return true;

    }



    @Override

    public boolean onTouchEvent(MotionEvent arg0) {

        //每次进行onTouch事件都记录当前的按下的坐标

        curP.x = arg0.getX();

        curP.y = arg0.getY();



        if(arg0.getAction() == MotionEvent.ACTION_DOWN){

            //记录按下时候的坐标

            //切记不可用 downP = curP ,这样在改变curP的时候,downP也会改变

            downP.x = arg0.getX();

            downP.y = arg0.getY();

            //此句代码是为了通知他的父ViewPager现在进行的是本控件的操作,不要对我的操作进行干扰

            getParent().requestDisallowInterceptTouchEvent(true);

        }



        if(arg0.getAction() == MotionEvent.ACTION_MOVE){

            //此句代码是为了通知他的父ViewPager现在进行的是本控件的操作,不要对我的操作进行干扰

            getParent().requestDisallowInterceptTouchEvent(true);

        }



        if(arg0.getAction() == MotionEvent.ACTION_UP){

            //在up时判断是否按下和松手的坐标为一个点

            //如果是一个点,将执行点击事件,这是我自己写的点击事件,而不是onclick

            if(downP.x==curP.x && downP.y==curP.y){

               /* onSingleTouch();*/

                return true;

            }

        }



        return super.onTouchEvent(arg0);

    }



}

 

你可能感兴趣的:(scrollview)