您的位置:首页 > 其它

解决!Gallery中嵌套ListView,Gallery不能滑动的问题

2012-09-14 14:01 543 查看
直接代码,请看注释
/** 引入相关类* */import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.ViewConfiguration;import android.widget.Gallery;/** 继承Gallery,重写onInterceptTouchEvent(MotionEvent ev)方法* */public class BetterGallery extends Gallery {private float mLastMotionX;//滑动过程中,x方向的初始坐标private float mLastMotionY;//滑动过程中,y方向的初始坐标private int mTouchSlop;//手指大小的距离/** 三个构造函数* */public BetterGallery(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public BetterGallery(Context context, AttributeSet attrs) {super(context, attrs);init();}public BetterGallery(Context context) {super(context);init();}/** 初始化,设置x方向移动的最小距离为手指大小时,拦截处理事件,可以设置需要的大小* */private void init() {final ViewConfiguration configuration = ViewConfiguration.get(getContext());mTouchSlop = configuration.getScaledTouchSlop();}/** 重写的方法,最关键的地方* */public boolean onInterceptTouchEvent(MotionEvent ev) {final int action = ev.getAction();//获取触摸事件类型final float x = ev.getX();//每次触摸事件的x坐标final float y = ev.getY();//每次触摸事件的y坐标switch (action) {case MotionEvent.ACTION_DOWN://按下事件mLastMotionX = x;//初始化每次触摸事件的x方向的初始坐标,即手指按下的x方向坐标mLastMotionY = y;//初始化每次触摸事件的y方向的初始坐标,即手指按下的y方向坐标break;case MotionEvent.ACTION_MOVE:final int deltaX = (int) (mLastMotionX - x);//每次滑动事件x方向坐标与触摸事件x方向初始坐标的距离final int deltaY = (int) (mLastMotionY - y);//每次滑动事件y方向坐标与触摸事件y方向初始坐标的距离boolean xMoved = Math.abs(deltaX) > mTouchSlop && Math.abs(deltaY/deltaX) < 1;//判断触摸事件处理的传递方向,该业务中是,//x方向的距离大于手指,并且y方向滑动的距离小于x方向的滑动距离时,Gallery消费掉此次触摸事件//如果需要,请在您的业务中,改变判断的逻辑if (xMoved) {//Gallery需要消费掉此次触摸事件return true;//返回true就不会将此次触摸事件传递给子View了,我的业务中是ListView}break;}return false;//将此次触摸事件传递给子View,即ListView}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐