1

スワイプを聞くことができるようにしたいアクティビティがあります。ScrollView私のアクティビティの中にありTextView、そのScrollViewのすべての領域が大きくなっています。アクティビティ全体でスワイプを有効にするために、クラスを拡張して次のようActivityに定義しました。SwipeActivity

public abstract class SwipeActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private int SWIPE_THRESHOLD_VELOCITY;
private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(new SwipeDetector());
    ViewConfiguration configuration = ViewConfiguration.get(this);
    SWIPE_THRESHOLD_VELOCITY = configuration.getScaledMinimumFlingVelocity();
}

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) {
            leftSwipe();
            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) {
            rightSwipe();
            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 rightSwipe();

protected abstract void leftSwipe();
}

これで、SwipeActivity を拡張するあらゆるアクティビティをキャプチャできるようにrightSwipe()なりました。leftSwipe()そこで、主な活動を次のように宣言しました。

public class ContentActivity extends SwipeActivity implements OnLongClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Here I add a TextView programmatically to my Vertical ScrollView and set this activity to listen to its long clicks (touches)
        ScrollView scrollView = (ScrollView) findViewById(R.id.content_scroll);
        TextView textView = new TextView(ContentActivity.this);
        textView.setText("Some Really Long Text!");
        textView.setOnLongClickListener(this);
        scrollView.addView(textView);
    }      

    @Override
    public boolean onLongClick(View v) {
        return doLongClickAction();
    }

    @Override
    public void rightSwipe() {
        doSwipeAction();
    }

    @Override
    public void leftSwipe() {
        doSwipeAction();
    }

}

これで、すべてが正常に表示され、垂直scrollViewスクロールが正しく表示され、スワイプ アクションとロング クリックが機能しますtextView

をスワイプする約10回からtextView、3回両方doLongClickAction()を起動doSwipeAction()し、ユーザーにとってまったくフレンドリーではありません。

私の問題がタッチイベントの衝突に関連していることはわかっていますが、何時間も勉強してテストした後、解決できませんでした。どこが間違っているのか教えてください。

(これを追加する必要があります:TextViewでロングクリックの代わりにクリックをキャプチャすると、すべて問題ありませんが、ユーザーがtextview内のリンクをクリックすると、textviewのonClickイベントが起動され、ブラウザが開きます!)

4

0 に答える 0