8

私のアプリケーションには、ボタンでいっぱいの領域が含まれています。このような方法でアクティビティを実装したいのですが、ボタン領域でジェスチャーを行うと、別の2つの領域のいずれかに切り替わります(ViewFlipperを使用)。

ジェスチャの検出に関して2つのアプローチを行いました。最初のものはGestureDetectorの使用に関係していました。ただし、ボタンを介したタッチモーションイベントはonTouchEventアクティビティメソッドを発生させなかったため、結果として、GestureDetectorクラスに転送できませんでした。要するに、失敗。

2番目のアプローチ-GestureOverlayViewの使用が含まれます。ただし、今回は2番目の極端に達しました。ジェスチャが検出されただけでなく、ジェスチャが実行されるボタンもクリックを報告しました。

インターフェイスが次のように機能することを望みます。ユーザーがボタンに触れてタッチを離すと(または指を少しだけ動かすと)、ボタンはクリックを報告し、ジェスチャは検出されません。一方、ユーザーが画面に触れて長い動きをした場合、ジェスチャが検出され、ボタンによってクリックイベントが報告されることはありません。

小さな概念実証アプリケーションを実装しました。アクティビティXMLコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"  xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
  <android.gesture.GestureOverlayView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/overlay">
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:id="@+id/display" android:layout_width="match_parent" android:layout_height="wrap_content" />
    <Button android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button"/>
    </LinearLayout>
  </android.gesture.GestureOverlayView>
</LinearLayout>

アクティビティJavaコードは次のとおりです。

package spk.sketchbook;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.gesture.*;
import android.gesture.GestureOverlayView.OnGestureListener;

public class Main extends Activity implements OnGestureListener, OnClickListener
{       
  private void SetupEvents()
  {
    GestureOverlayView ov = (GestureOverlayView)findViewById(R.id.overlay);
    ov.addOnGestureListener(this);

    Button b = (Button)findViewById(R.id.button);
    b.setOnClickListener(this);
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    SetupEvents();
  }

  @Override
  public void onGesture(GestureOverlayView arg0, MotionEvent arg1)
  {
    TextView tv = (TextView)findViewById(R.id.display);
    tv.setText("Gesture");
  }

  @Override
  public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1)
  {

  }

  @Override
  public void onGestureEnded(GestureOverlayView overlay, MotionEvent event)
  {

  }

  @Override
  public void onGestureStarted(GestureOverlayView overlay, MotionEvent event)
  {

  }

  @Override
  public void onClick(View v)
  {
    TextView tv = (TextView)findViewById(R.id.display);
    tv.setText("Click");
  }


}

問題は、ユーザーのアクションをジェスチャーまたはボタンのクリックとして扱うかどうかを決定できる、このようなインターフェイスをどのように実装するかです。

よろしく-スプーク。

4

2 に答える 2

2

別の解決策: この関数を作成します。

public void SetSwipe(View v, final GestureDetector gestureScanner){
    // Sets scroll view listener
    v.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
              if (gestureScanner.onTouchEvent(event))  
                return true;  
              else  
                return false;  
        }
    });
}

そして、次のようにウィジェットごとに呼び出します。

scfSetTag.SetSwipe(svAllView, gestureScanner);

多くのアクティビティまたはフラグメントからこれを作成する必要がある場合は、class.java ファイルを作成し、スワイプを実装する必要がある各アクティビティでこのクラスをインスタンス化できます。これは、任意のウィジェットでのスワイプとクリックの両方を検出します。助けてください!

于 2012-07-21T18:21:35.453 に答える