4

スワイプジェスチャーに応答するためにリニアレイアウトにタッチイベントを追加し、うまく動作します。しかし、レイアウトにボタンを追加すると、親ライナーのレイアウトは無視されます。これを防ぐにはどうすればよいですか?ボタンで満たされたライナーレイアウトのタッチリスナーを追加

LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2); 
ln2.setOnTouchListener(swipe); 

は、どのように私はonInterceptTouchを使用するには?

答えて

10

独自のレイアウトを作成し、レイアウトのonInterceptTouchEvent(MotionEvent ev)メソッドをオーバーライドする必要があります。例については

私はRelativeLayout

 @Override 
     public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return true; // With this i tell my layout to consume all the touch events from its childs 
     } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     // Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s", 
      break; 
     case MotionEvent.ACTION_MOVE: 
     //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
      break; 
     case MotionEvent.ACTION_UP: 
      break; 
     } 
     return true; 
    } 

を拡張して自分のレイアウトを作成し、私も、私のレイアウトにボタンを置いたとき、私はボタンが、私のレイアウトが原因onInterceptTouchEventのすべてのタッチイベントを消費し、常にtrueを返すことをクリック。 onClick =「tapEventを」あなたはクリックをしたいあなたのレイアウトで:

希望これはあなたが

+0

ありがとう私はそれを試してみます –

+0

私のコードでonInterceptTouchEventをオーバーライドする方法を理解できませんでした。すべてのボタン、または親レイアウトのためにそれを行う必要がありますか? –

+0

あなたはそれを親レイアウト用に行うべきです。 30分待つことができれば、私は自分の答えを編集することができます。 私は今すぐに仕事に行く必要があります –

0

アンドロイドを追加助けるべきです。 MAX_TAP_COUNTの値を変更することにより、任意の数のタップを使用できます。

private long thisTime = 0; 
private long prevTime = 0; 
private int tapCount = 0; 
private static final int MAX_TAP_COUNT = 5; 
protected static final long DOUBLE_CLICK_MAX_DELAY = 500; 


public void tapEvent(View v){ 

     if (SystemClock.uptimeMillis() > thisTime) { 
      if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) { 
       Log.d(TAG, "touch event " + "resetting tapCount = 0"); 
       tapCount = 0; 
      } 
      if (tapCount()) { 
       //DO YOUR LOGIC HERE 
      } 
     } 

} 

private Boolean tapCount(){ 

     if (tapCount == 0) { 
      thisTime = SystemClock.uptimeMillis(); 
      tapCount++; 
     } else if (tapCount < (MAX_TAP_COUNT-1)) { 
      tapCount++; 
     } else { 
      prevTime = thisTime; 
      thisTime = SystemClock.uptimeMillis(); 

      //just incase system clock reset to zero 
      if (thisTime > prevTime) { 

       //Check if times are within our max delay 
       if ((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) { 
        //We have detected a multiple continuous tap! 
        //Once receive multiple tap, reset tap count to zero for consider next tap as new start 
        tapCount = 0; 
        return true; 
       } else { 
        //Otherwise Reset tapCount 
        tapCount = 0; 
       } 
      } else { 
       tapCount = 0; 
      } 
     } 
     return false; 

} 
関連する問題