OnFling方法无法在app中运行

时间:2013-09-21 19:54:21

标签: android android-intent onfling

我在我的每个活动中实施了一个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;
        }
    }

1 个答案:

答案 0 :(得分:1)

首先为视图设置触控侦听器,然后连接到手势检测器。

以下是示例代码:

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

           setOnTouchListener(gestureListener);

这将解决您的问题。