Android 在线问题
我想在 LinearLayout 上添加一个 fling 动作。为此,我使用了以下代码。
public class NewsActivity 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;
View.OnTouchListener gestureListener;
@Override
public void onCreate(Bundle savedInstanceState) {
.....................
.....................
.....................
LinearLayout newDeailsBlock = (LinearLayout) findViewById(R.id.newdeailsblock);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
newDeailsBlock.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
Log.d("move","Swipe test");
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) {
Log.d("move","Left Swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(NewsActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
Log.d("move","Right Swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
但它根本不起作用 amd gestureDetector.onTouchEvent(event)
返回 false。我的代码有什么问题?
I want to add a fling action on a LinearLayout. I have used the following code for this purpose.
public class NewsActivity 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;
View.OnTouchListener gestureListener;
@Override
public void onCreate(Bundle savedInstanceState) {
.....................
.....................
.....................
LinearLayout newDeailsBlock = (LinearLayout) findViewById(R.id.newdeailsblock);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
newDeailsBlock.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
Log.d("move","Swipe test");
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) {
Log.d("move","Left Swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(NewsActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
Log.d("move","Right Swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
But it is not working at all amd gestureDetector.onTouchEvent(event)
is returning false. What is the problem in my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试让您的 Activity 扩展下面的抽象类,然后使用您想要的 fling 功能实现抽象方法
next()
和previous()
。Try having your Activity extend the below abstract class instead, and then implement the abstract methods
next()
andprevious()
with the functionality you want for the fling.