Android onFling()方法不会触发

我正在尝试在我的Android应用中创建手势识别功能,该功能将对投掷手势做出响应并相应地更改视图。 一点背景信息:我的活动由线性布局中的两个并排视图组成。

我遇到的问题是onFling()方法从未触发,尽管在设备的屏幕上执行了手势。

这是我的手势识别处理程序类GestureListener.java

package com.example.guitarsim;

//this class is designed to listen for touch events corresponding to the fling gesture
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

class GestureListener extends SimpleOnGestureListener implements OnTouchListener {
    Context mContext;
    GestureDetector gDetector;
    static final double SWIPE_MIN_DISTANCE = PlayFrets.getActualHeight()*(0.05);  //must swipe across at least 5% of screen height to register a gesture
    static final int SWIPE_MAX_OFF_PATH = 150;
    static final int SWIPE_THRESHOLD_VELOCITY = 100;

    public GestureListener(Context context) {
        this.mContext = context;
    }

    public GestureListener(Context context, GestureDetector gDetector) {
        if (gDetector == null){
            gDetector = new GestureDetector(context, this);
        }
        this.mContext = context;
        this.gDetector = gDetector;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gDetector.onTouchEvent(event);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
        Log.e("gesture", String.valueOf(SWIPE_MIN_DISTANCE));
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
            if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH || Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
                return false;
            }
            else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
                //float traveled = e1.getY() - e2.getY();
                //Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext, "Swiped Up",Toast.LENGTH_SHORT).show();
            } 
            else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
                //float traveled = e2.getY() - e1.getY();
                //Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext, "Swiped Down",Toast.LENGTH_SHORT).show();
            }
        } 
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    public GestureDetector getDetector() {
        return gDetector;
    }

}

编辑这里是我的主要活动类,我实例化我的GestureListener对象, PlayFrets.java

public class PlayFrets extends Activity {
LinearLayout gestureOverlay; 
GestureListener gestureListener;        
public void configGestureRecognition(){       //Toolbar gesture recognition
        gestureOverlay = (LinearLayout) findViewById(R.id.toolbarGestureOverlay);
        gestureListener = new GestureListener(PlayFrets.this);
        if (gestureOverlay == null){
            Toast.makeText(PlayFrets.this, "gestureOverlay object is null bro" ,Toast.LENGTH_SHORT).show();
        }
        gestureOverlay.setOnTouchListener(gestureListener);
    }

}

编辑我还包括我的课运行一个Asynctask时,应用程序启动,调用我的主要活动中的方法,运行我的GestureListener实例化代码并设置视图, LoadNoteFiles.java

  public class LoadNoteFiles extends AsyncTask<Void, Void, Void>{
private Context mContext;
Activity instance;  

public LoadNoteFiles(Context context){ //constructor
    mContext = context;
}

@Override
protected void onPreExecute(){
    PlayFrets.viewSwitch = new ViewSwitcher(mContext);
    PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.splash_screen, null));
    instance = (Activity)mContext;  //cast context from main activity into an activity to access setContentView method
    instance.setContentView(PlayFrets.viewSwitch);
}
...//do in background code omitted
@Override
protected void onPostExecute(Void v){
    PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.activity_play_frets, null));
    PlayFrets.viewSwitch.showNext();
    ((PlayFrets)mContext).configFretboard();
    ((PlayFrets)mContext).configGestureRecognition(); //test gesture recognition
}

这里是我的XML文件。 *请注意,我试图检测手势的视图是id toolbarGestureOverlay的线性布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:keepScreenOn="true">

    <LinearLayout    
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/gold_guitarsim_text"
        android:orientation="vertical" 
        android:id="@+id/toolbarGestureOverlay" >
    </LinearLayout> 

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_weight="4">

    <com.example.guitarsim.Multitouch
        android:id="@+id/fretBoard"  
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/fret_board1" />
    </LinearLayout>    
</LinearLayout> 

所以这里是我的问题。 什么阻止GestureListener的onFling()方法到达? 另外,我的XML文件是否正确设置? 特别; 我需要标记线性布局,我试图检测手势,就像我用myother线性布局(com.example.guitarsim.Multitouch)在我的程序中进行有效控制一样吗?

链接地址: http://www.djcxy.com/p/91301.html

上一篇: Android onFling() method not firing

下一篇: I can't implement onFling, onScroll and more methods