28

私はAndroidにOnGestureListenerを実装しようとしています。
レイアウトに3つのTextViewがあります。
私が達成しようとしているのは、2つのtextViewのジェスチャリスナーを設定することです。
これがレイアウトです-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvOne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="One" />

    <TextView
        android:id="@+id/tvTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvOne"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Two" />

    <TextView
        android:id="@+id/tvThree"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTwo"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Three" />

</RelativeLayout>

そしてここに活動があります

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;

    public class TimeActivity extends Activity implements OnGestureListener {

    GestureDetector gestureScanner;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wheelview);
        gestureScanner = new GestureDetector(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureScanner.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        Log.i("Test", "On Fling");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

現在、3つのテキストビューすべてのFlingが呼び出されています。レイアウト内の特定のビューにジェスチャリスナーを
設定する方法はありますか? どんな助けでも大歓迎です。

4

3 に答える 3

29

あなたのonCreate方法でこれをしてください。

findViewById(R.id.tvOne).setOnTouchListener(new View.OnTouchListener() { 
            @Override
           public boolean onTouch(View v, MotionEvent event){
                return gestureScanner.onTouchEvent(event);
           }
  });
于 2013-02-18T07:52:14.340 に答える
10

OnTouchListenersを個別に設定できますTextView

findViewById(R.id.tvOne).setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
         // Your code here
    }
}
于 2013-02-18T07:47:33.580 に答える
2

提案

すべてのジェスチャを検出したくない場合は、次のクラスを試してください: 自分で作成したSimpleGestureListener 。

ここに、このクラスの使用法のスニペットがいくつかあります。

使用法

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private GestureDetector mDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SimpleGestureListener simpleGestureListener = new SimpleGestureListener();
        simpleGestureListener.setListener(new SimpleGestureListener.Listener() {
            @Override
            public void onScrollHorizontal(float dx) {
                Log.i(TAG,"horizontal = " +dx);
            }

            @Override
            public void onScrollVertical(float dy) {
                Log.i(TAG,"vertical = " +dy);
            }
        });
        mDetector = new GestureDetector(this, simpleGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

スワイプジェスチャの検出に使用できます:

  • 左か右
  • 上か下
于 2017-02-21T09:11:48.677 に答える