1

onClickListeneronFling(を使用) の両方がありGestureDetectorます。

ボタンをクリックするとonClickListener発火します。

画面の本体に飛びかかるとonFling発砲します。

ただし、ボタンからフリングを開始すると、どちらも発火しません。

次のようにレイアウトします。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
            android:id="@+id/myButton"
            android:layout_margin="50dp"
            android:layout_height="100dp"
            android:layout_width="match_parent"
            android:text="Button"/>
</LinearLayout>

コードは次のようになります。

public class myLayout extends Activity implements GestureDetector.OnGestureListener {
    private GestureDetector gDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myLayout);

        gDetector = new GestureDetector(getBaseContext(), this);
        findViewById(R.id.myButton).setOnClickListener(button_OnClickListener);
    }

    final View.OnClickListener button_OnClickListener = new View.OnClickListener() {
        public void onClick(final View buttonView) {
            Toast.makeText(getApplicationContext(), "button pressed", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
    }

    @Override
    public boolean onFling(MotionEvent start, MotionEvent finish, float v, float v2) {
        if (start.getRawY() < finish.getRawY()) {
            Toast.makeText(getApplicationContext(), "Fling detected", Toast.LENGTH_LONG).show();
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        Log.i("Touch", "onTouchEvent");
        return gDetector.onTouchEvent(me);
    }
}

onFlingを最初に実行するにはどうすればよいですか?

これに関する他の投稿 ( onClick のブロック onFling など) を見ましたが、適切な答えが見つかりませんでした。

4

1 に答える 1