1

我已经制作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);
            }
        };
4

2 回答 2

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>
于 2012-05-03T06:42:21.097 回答
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);

}
于 2012-05-03T06:29:10.497 回答