jul*_*jul 11 android scrollview gesture
可能重复:
手势检测和ScrollView问题
编辑:问题与完整的代码在这里问.
我有一个孩子的布局.我设置了一个手势监听器来检测布局上的水平滑动.当布局是LinearLayout时,正确检测到滑动,但是当它是ScrollView时,它不是.我猜这个手势首先被ScrollView检测到,并没有传播到它的优先级,但我不知道如何解决它.
这是我的布局:
<?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">
<ImageView android:layout_width="320dp" android:layout_height="30dp"
android:src="@drawable/header"/>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<!-- stuff -->
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我将以下监听器设置为我的布局:
class ProductGestureListener extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show previous item
} else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// show next item
}
} catch (Exception e) {
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
kas*_*rch 18
如果您希望整体Activity可以水平滑动,您可以使用以下作为您的超级类Activity:
public abstract class SwipeActivity 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;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
gestureDetector = new GestureDetector( new SwipeDetector() );
}
private class SwipeDetector extends SimpleOnGestureListener {
@Override
public boolean onFling( MotionEvent e1, MotionEvent e2, float velocityX, float velocityY ) {
// Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
// then dismiss the swipe.
if( Math.abs( e1.getY() - e2.getY() ) > SWIPE_MAX_OFF_PATH )
return false;
// Swipe from right to left.
// The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
// and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
if( e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) {
next();
return true;
}
// Swipe from left to right.
// The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
// and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
if( e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) {
previous();
return true;
}
return false;
}
}
@Override
public boolean dispatchTouchEvent( MotionEvent ev ) {
// TouchEvent dispatcher.
if( gestureDetector != null ) {
if( gestureDetector.onTouchEvent( ev ) )
// If the gestureDetector handles the event, a swipe has been
// executed and no more needs to be done.
return true;
}
return super.dispatchTouchEvent( ev );
}
@Override
public boolean onTouchEvent( MotionEvent event ) {
return gestureDetector.onTouchEvent( event );
}
protected abstract void previous();
protected abstract void next();
}
Run Code Online (Sandbox Code Playgroud)
您需要做的就是在扩展后实现next()和previous()方法SwipeActivity.
jul*_*jul 10
我不得不补充一下
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
super.dispatchTouchEvent(ev);
return productGestureDetector.onTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
在您的活动类中添加此方法,该方法使用swiper,就像onCreate方法一样.
| 归档时间: |
|
| 查看次数: |
9941 次 |
| 最近记录: |