Android OnFling方法在应用程序中不起作用

Android OnFling方法在应用程序中不起作用,android,android-intent,onfling,Android,Android Intent,Onfling,我在我的每个活动中都实现了一个onFling(),它应该使用意图在上一个活动和下一个活动之间导航 但当我运行应用程序时,它不工作,也不会显示在我的日志中。是否有其他方法可以调用该方法或代码中的一些小错误 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated metho

我在我的每个活动中都实现了一个
onFling()
,它应该使用意图在上一个活动和下一个活动之间导航

但当我运行应用程序时,它不工作,也不会显示在我的日志中。是否有其他方法可以调用该方法或代码中的一些小错误

 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            Log.d(null,"Fling");
            int dx = (int) (e2.getX() - e1.getX());
            // don't accept the fling if it's too short
            // as it may conflict with a button push
            if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                 //switch to activity on left fling  
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);  

                } else {
                //switch to activity on right fling
                Intent intent = new Intent(this, StudentLife.class);
                startActivity(intent);      


                }
                return true;

            } else {

                return false;
            }
        }

This is the whole class file to clarify some of the questions:

    import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class GalleryStudent extends Activity implements OnGestureListener {

     private static final int MAJOR_MOVE = 0;


    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_galery_student);

            Gallery g = (Gallery) findViewById(R.id.gallery);
            g.setAdapter(new ImageAdapter(this));

            g.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
                    Toast.makeText(GalleryStudent.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }


     public boolean onOptionsItemSelected(MenuItem item)
        {

            switch (item.getItemId())
            {   
                case android.R.id.home:
                    // This is called when the Home (Up) button is pressed
                    // in the Action Bar.
                    Intent parentActivityIntent = new Intent(this, 
                    MainActivity.class);
                    parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(parentActivityIntent);
                    finish();
                    return true;

                default:

                    return super.onOptionsItemSelected(item);



            }



        }

     public void onBackPressed() {
         Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);

    }



    public class ImageAdapter extends BaseAdapter {


        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.gmitlogo,
                R.drawable.michaelcarmody,
                R.drawable.fb
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }


    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
              return detector.onTouchEvent(event);
        }
  };

  setOnTouchListener(gestureListener);




    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        Log.d(null,"Fling");
        int dx = (int) (e2.getX() - e1.getX());
        // don't accept the fling if it's too short
        // as it may conflict with a button push
        if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
            if (velocityX > 0) {
             //switch to activity on left fling  
            Intent intent = new Intent(this, StudentLife.class);
            startActivity(intent);  

            } else {
            //switch to activity on right fling
            Intent intent = new Intent(this, StudentPortal.class);
            startActivity(intent);      


            }
            return true;

        } else {

            return false;
        }
    }
首先为视图设置触摸监听器,然后连接到手势检测器

以下是示例代码:

  View.OnTouchListener gestureListener = new View.OnTouchListener() {
                 public boolean onTouch(View v, MotionEvent event) {
                       return detector.onTouchEvent(event);
                 }
           };

           setOnTouchListener(gestureListener);

这将解决您的问题。

您的断点是否采用此方法?您是否将touchEvent发送给gestureDetector?实际上我没有。我将更新我的问题,以便您可以看到整个类。我的印象是,触摸事件将触发onFling()。我在onFling()之前实现了代码修补程序但是我得到了一个错误:setOnTouchListener(手势Listener);令牌“gestureListener”上出现语法错误,此令牌后应为VariableDeclaratorId。我不确定我在这里哪里出错了@乌默法罗