Don't know how to update the fling gesture code

Let's assume this:

I have activity1,activity2,activity3. The app will start with activity 2.What I want is that when the user makes that fling gesture to right activity 3 is shown,when to left activity 1 is shown and so on.

I have a code from the web but I failed to implement it.Mostly because is incomplete,and I have never worked with filters and all that stuff/

Here's the code:

public class SelectFilterActivity extends Activity implements OnClickListener
{

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* ... */

        // Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };

    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

    }

Attach your gesture listener to all the views you add to the main layout;

// Do this for each view added to the grid

imageView.setOnClickListener(SelectFilterActivity.this); 
imageView.setOnTouchListener(gestureListener);

Watch in awe as your overridden methods are hit, both the onClick(View v) of the activity and the onFling of the gesture listener.

public void onClick(View v) {
        Filter f = (Filter) v.getTag();
        FilterFullscreenActivity.show(this, input, f);
}

So again,my layout is very simple,how do I implement the code for my 3 activities that I told you above? The class for gesture goes in all activities or only in 1? What do I need to add to the other activities to make the swipe gesture work? Please help me here...

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

上一篇: 手势检测(向上/向下滑动)特定视图

下一篇: 不知道如何更新投掷手势代码