---
title: "LinearLayout上onFling事件失效问题"
author: "Beyon_javaeye"
date: "2011-10-11T09:56:41Z"
tags: ["Layout", "onTouch", "onfling"]
categories: ["移动开发"]
slug: "linearlayout-onfling"
draft: false
lastmod: "2011-10-11T09:56:41Z"
id: 1189285
url: "https://net-art.iteye.com/blog/1189285"
---

1.  写一个类，实现OnGestureListener, OnTouchListener接口。重写里面的方法，记得把onDown()方法return true;这样才能触发onFling事件。  
2.  设置layout的onTouch事件：  
LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout); layout.setOnTouchListener(this);  
3.  在touch时调用如下：  
在onTouch方法中return detector.onTouchEvent(event);  
//其中detector是：GestureDetector detector = new GestureDetector(this);  
4.  重写onFling()方法：  
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        if(e1.getX() - e2.getX() > 50 && Math.abs(velocityX)>0){  
            System.out.println("Left.......");//向左滑动  
        }else if(e2.getX()-e1.getX()>50 && Math.abs(velocityX)>0){  
            System.out.println("Right......");//向右滑动  
        }  
        return false;  
    }  