在平板电脑3.0中滑动手势

时间:2012-05-03 06:05:23

标签: android

我做了一个应用,其中从左到右使用滑动Gesture检测&从右到左。
我使用2.2 AVD模拟器取得了成功,但是当我尝试在平板电脑3.0中使用相同的代码时,它无法正常工作

请告诉我什么是错的。

private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
   private class GestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                Toast.makeText(RssActivity.this, "Right to left",
                        Toast.LENGTH_LONG).show();
                SetNewRightclickActivity();
                Log.i(TAG, "Right to left");
                return true; // Right to left

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                Toast.makeText(RssActivity.this, "Left to right",
                        Toast.LENGTH_LONG).show();
                Log.i(TAG, "Left to right");
                SetNewLeftclickActivity();
                return true; // Left to right

            }
            if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {

                return true; // Bottom to top
            } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {

                return true; // Top to bottom
            }
            return false;
        }
    }

主要::

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rssheader);

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

2 个答案:

答案 0 :(得分:4)

你的代码似乎是正确的,你可以粘贴log cat trace !!想出什么是错误

此代码对我有用:

 public class MainActivity extends Activity {
    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;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
        View mainview = (View) findViewById(R.id.mainView);

        // Set the touch listener for the main view to be our custom gesture listener
        mainview.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


            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(getApplicationContext(), "right_left", Toast.LENGTH_LONG).show();

                // Do Something
                //  left to right  swipe
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "left_right", Toast.LENGTH_LONG).show();
                // Do somethin

            }

            return false;
        }

        // It is necessary to return true from onDown for the onFling event to register
        @Override
        public boolean onDown(MotionEvent e) {
                return true;
        }

    }
}

为主要。 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainView"
    android:background="#AA1BAF"
    > 
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="Swipe left and right"
        />
</LinearLayout>

答案 1 :(得分:2)

您需要将gestureListener(在 onCreate 方法中)设置为您要检测滑动的视图。


rssheader.xml 中将ID设置为布局中的视图/视图组,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:id="@+id/myLayout"
    android:clickable="true">

</LinearLayout>

现在在Activity类中获取该id并将侦听器设置为

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rssheader);

    LinearLayout ll = (LinearLayout) findViewById(R.id.myLayout);

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

    ll.setOnTouchListener(gestureListener);

}