66

私のアプリでは、2 つのことを実行したいと考えています。

  1. ImageButton をタッチしてドラッグすると、指に沿って移動するはずです。

    OnTouchListener()はこれに使用しましたが、正常に動作します。

  2. ImageButton をクリックすると、アクティビティが閉じます。

    OnClickListener()はこれに使用しましたが、正常に動作します。

だから、ここに私の問題があります。移動するたびに、ボタンを離しImageButton OnTouchListenerImageButton移動するOnClickListenerと、最後にトリガーされて移動します。

互いに干渉することなく、同じボタンで ontouch リスナーと onclick リスナーを使用する方法は?

4

12 に答える 12

35

、、、、、、をシングルにするにはClick Listener、必要があります。つまり、DoubleClick ListenerOnLongPress ListenerSwipe LeftSwipe RightSwipe UpSwipe DownViewsetOnTouchListener

view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

            @Override
            public void onClick() {
                super.onClick();
                // your on click here
            }

            @Override
            public void onDoubleClick() {
                super.onDoubleClick();
                // your on onDoubleClick here
            }

            @Override
            public void onLongClick() {
                super.onLongClick();
                // your on onLongClick here
            }

            @Override
            public void onSwipeUp() {
                super.onSwipeUp();
                // your swipe up here
            }

            @Override
            public void onSwipeDown() {
                super.onSwipeDown();
                // your swipe down here.
            }

            @Override
            public void onSwipeLeft() {
                super.onSwipeLeft();
                // your swipe left here.
            }

            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
                // your swipe right here.
            }
        });

}

このためにはOnSwipeTouchListener、 を実装するクラスが必要OnTouchListenerです。

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        onClick();
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        onDoubleClick();
        return super.onDoubleTap(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        onLongClick();
        super.onLongPress(e);
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {

}

public void onDoubleClick() {

}

public void onLongClick() {

}
}
于 2015-04-16T13:45:34.923 に答える
6

プロジェクトに @Biraj ソリューションを適用しようとしましたが、機能しませんでした。拡張機能はメソッドをSimpleOnGestureListenerオーバーライドするだけでなく、オーバーライドする必要があることに気付きました。ドキュメンテーションのため:onSingleTapConfirmedonDown

GestureDetector.SimpleOnGestureListener がデフォルトで行うように、onDown() から false を返すと、システムはジェスチャの残りを無視すると想定し、GestureDetector.OnGestureListener の他のメソッドは呼び出されません。

以下は複雑なソリューションです。

public class MainActivity extends Activity {
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetectorCompat(this, new SingleTapConfirm());
        ImageButton imageButton = (ImageButton) findViewById(R.id.img);

        imageButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (gestureDetector.onTouchEvent(arg1)) {
                    // single tap
                    return true;
                } else {
                    // your code for move and drag
                }

                return false;
            }
        });

    }

    private class SingleTapConfirm extends SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            /*it needs to return true if we don't want 
            to ignore rest of the gestures*/
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            return true;
        }
    }

}

この動作は GestureDetectorCompat の違いによって引き起こされる可能性があると思いますが、ドキュメントに従い、2 番目のものを使用します。

Android 1.6 以降を実行しているデバイスとの互換性を提供するために、可能な場合はサポート ライブラリ クラスを使用する必要があります。

于 2016-02-17T15:41:11.920 に答える
5

MainActivityコードでこれ。

public class OnSwipeTouchListener_imp extends AppCompatActivity {

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

    ImageView view = (ImageView)findViewById(R.id.view);

    view.setOnTouchListener(new OnSwipeTouchListener(OnSwipeTouchListener_imp.this)
    {
        @Override
        public void onClick()
        {
            super.onClick(); // your on click here              
            Toast.makeText(getApplicationContext(),"onClick",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDoubleClick()
        {
            super.onDoubleClick(); // your on onDoubleClick here               
        }

        @Override
        public void onLongClick()
        {
            super.onLongClick(); // your on onLongClick here                
        }

        @Override
        public void onSwipeUp() {
            super.onSwipeUp(); // your swipe up here                
        }

        @Override
        public void onSwipeDown() {
            super.onSwipeDown();  // your swipe down here.

        }

        @Override
        public void onSwipeLeft() {
            super.onSwipeLeft(); // your swipe left here.                
            Toast.makeText(getApplicationContext(),"onSwipeLeft",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSwipeRight() {
            super.onSwipeRight(); // your swipe right here.                
            Toast.makeText(getApplicationContext(),"onSwipeRight",Toast.LENGTH_SHORT).show();
        }
    });
}
}

次に、OnSwipeTouchListenerJava クラスを作成します。

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        onClick();
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        onDoubleClick();
        return super.onDoubleTap(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        onLongClick();
        super.onLongPress(e);
    }

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffX > 0)
                    {
                        onSwipeRight(); // Right swipe
                    } else {
                        onSwipeLeft();  // Left swipe
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown(); // Down swipe
                    } else {
                        onSwipeUp(); // Up swipe
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {
}

public void onDoubleClick() {
}

public void onLongClick() {
}
}

これがあなたを照らしてくれることを願っています:)

于 2016-12-05T04:27:15.703 に答える
2

たぶん、ブール値で作業できます。

ブール値 isMoving = false; としましょう。

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

    case MotionEvent.ACTION_MOVE:
                 isMoving = true;

          // implement your move codes
    break;

    case MotionEvent.ACTION_UP:
               isMoving = false;
    break;

default:
        break;
    }

次に、onclick メソッドでブール値を確認します。false の場合はクリック アクションを実行し、true の場合はアクションを実行しません。

public void onClick(View arg0) {

    switch (arg0.getId()) {

    case R.id.imagebutton:
        if(!isMoving) {
                   //code for on click here
                }
    default:
        break;
    }
}
于 2013-10-23T10:15:59.613 に答える
0

ブール値フィールドを使用して、OnTouchListener がトリガーされたときに true 値に設定するだけです。その後、OnClickListener がトリガーする必要がある場合は、ブール値フィールドをチェックし、true の場合は onClickListener で何も実行しません。

    private blnTouch = false;

private OnTouchListener btnOnTouchListener = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction()==MotionEvent.ACTION_DOWN){
        blnOnTouch = true;
    }
    if (event.getAction()==MotionEvent.ACTION_UP){
        blnOnTouch = false;
    }
               }
 };

private OnClickListener btnOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if (blnOnTouch){
            // Do Your OnClickJob Here without touch and move
        }

    }
};
于 2013-10-23T10:21:33.543 に答える
0

リスナー イベントをクリックします。onclicklistener を呼び出すよりも、コンポーネントの境界で actiondown と action up の場合。したがって、 onclick イベントはタッチ検出でアクティブになります。コンポーネントの開始位置でアクションアップとアクションダウンの場合にのみ、onTouchListener を設定してクリックイベントを受け取ることができます。

于 2013-10-23T10:13:59.050 に答える
0

次のようなプライベート変数を宣言します。boolean hasMoved = false;

画像ボタンが動き始めたらセットhasMoved = true

OnClickListener実行コードのみif(!hasMoved)- ボタンが移動していない場合にのみクリック機能を実行することを意味します。hasMoved = false;あとで設定

于 2013-10-23T10:14:37.310 に答える