gpt4 book ai didi

android - 平板电脑 3.0 中的滑动手势

转载 作者:行者123 更新时间:2023-11-29 14:54:08 25 4
gpt4 key购买 nike

我制作了一个应用程序,其中滑动 Gesture 检测用于从左到右和从右到左。
我在 2.2 AVD 模拟器上取得了成功,但是当我尝试在 tablet 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);
}
};

最佳答案

您的代码似乎是正确的,您可以粘贴 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>

关于android - 平板电脑 3.0 中的滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com