Man*_*oba 4 android gesture swipe onfling
我正在尝试在我的应用中实现滑动手势.我已经制作了几乎所有的代码,但它不起作用.
这是我在Activity中的代码:
// Swipe detector
gestureDetector = new GestureDetector(new SwipeGesture(this));
gestureListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
Log.e("", "It works");
return gestureDetector.onTouchEvent(event);
}
};
LinearLayout root = (LinearLayout) findViewById(R.id.rules_root);
root.setOnTouchListener(gestureListener);
Run Code Online (Sandbox Code Playgroud)
当我触摸屏幕时,会显示logcat it works.
在这里他是我的班级代码SwipeGesture:
public class SwipeGesture extends SimpleOnGestureListener
{
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 Activity activity;
public SwipeGesture(Activity activity)
{
super();
this.activity = activity;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
Log.e("", "Here I am");
try
{
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
if ( ((TabActivity) activity.getParent()).getTabHost() != null )
{
TabHost th = ((TabActivity) activity.getParent()).getTabHost();
th.setCurrentTab(th.getCurrentTab() - 1);
}
else
{
activity.finish();
}
Log.e("", "Swipe left");
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
if ( ((TabActivity) activity.getParent()).getTabHost() != null )
{
TabHost th = ((TabActivity) activity.getParent()).getTabHost();
th.setCurrentTab(th.getCurrentTab() + 1);
}
Log.e("", "Swipe right");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
该行Log.e("", "Here I am");永远不会显示.所以我认为永远不会调用onFling方法.
为什么这不起作用的任何想法?
谢谢.
问候.
V.
在SimpleOnGestureListener中,覆盖onDown以便注册手势.它可以返回true但它必须像这样定义..
@Override
public boolean onDown(MotionEvent e) {
return true;
}
Run Code Online (Sandbox Code Playgroud)
.... 看到这个链接..和答案下面的评论..
你需要改变一些事情,这是一个例子.
设置OnTouchListener:
root.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
SwipeGesture类:
class SwipeGesture 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;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Do something
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Do something
return true;
}
} catch (Exception e) {
Log.e("Fling", "There was an error processing the Fling event:"
+ e.getMessage());
}
return true;
}
// Necessary for the onFling event to register
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来像你在之间滑动Tabs.使用Fragments和ViewPager更容易和更顺畅的用户.
| 归档时间: |
|
| 查看次数: |
3814 次 |
| 最近记录: |