作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 ScrollView
上设置了 Gesture 的 onFling()
事件,但它在 ScrollView 上不起作用,
package com.doubletap;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
public class DoubleTapActivity extends Activity implements OnGestureListener
{
private GestureDetector gd;
private TextView tvTap;
String TAG = getClass().getSimpleName();
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;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvTap = (TextView)findViewById(R.id.tvTap);
gd = new GestureDetector(this);
gd.setOnDoubleTapListener(new OnDoubleTapListener()
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return gd.onTouchEvent(event);//return the double tap events
}
@Override
public boolean onDown(MotionEvent e)
{
return false;
}
@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) {
tvTap.setText("Flip Right to Left");
Log.v(TAG, "Right to Left");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
tvTap.setText("Flip Left to Right");
Log.v(TAG, "Left to Right");
}
} catch (Exception e)
{
}
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
@Override
public void onShowPress(MotionEvent e)
{
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return false;
}
}
主.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/hello"
android:textStyle="bold" />
<TextView
android:id="@+id/tvTap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip" >
</TextView>
<TextView
android:id="@+id/tvTapEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
</TextView>
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button10" />
</LinearLayout>
</ScrollView>
有人知道如何在 Scrollview 上实现 onFling() 事件吗??
最佳答案
这对我有用。希望您可以在此处添加双击操作。
public class ScrollViewFling extends Activity
{
private GestureDetector mGesture;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollviewfling);
mGesture = new GestureDetector(this, mOnGesture);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = super.dispatchTouchEvent(ev);
handled = mGesture.onTouchEvent(ev);
return handled;
}
private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.v("fling", "Flinged.");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
};
};
关于android - 如何在android中的ScrollView上设置Gesture的onFling()事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927305/
我是一名优秀的程序员,十分优秀!