如何在两个不同的视图中使用两个不同的GestureDetector?

时间:2016-02-06 12:56:59

标签: android gesture-recognition

我在relativelayout中有两个线性布局。我能够使用GestureDetector类检测手势。我想在不同的视图上同时检测手势。为此,我希望有两个不同的GestureDetectors,但不知道如何实现这一点。

这是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffffff">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="500dp"
    android:layout_alignParentTop="true"
    android:id="@+id/LinearLayout1"
    android:layout_above="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#673AB7"></LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#1DE9B6"></LinearLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:layout_marginBottom="47dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

这是我的.java文件:

public class MainActivity extends AppCompatActivity implements OnGestureListener {

LinearLayout myLayout;
LinearLayout myLayout2;
GestureDetectorCompat detector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    detector = new GestureDetectorCompat(this, this);
}

@Override
protected void onStart() {
    super.onStart();

    myLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
    myLayout2 = (LinearLayout) findViewById(R.id.LinearLayout2);
    myLayout.setOnTouchListener(new LinearLayout.OnTouchListener() {
                                    public boolean onTouch(View v, MotionEvent m) {

                                        return detector.onTouchEvent(m);
                                    }
                                }
    );
    myLayout2.setOnTouchListener(new LinearLayout.OnTouchListener() {
                                     public boolean onTouch(View v, MotionEvent m) {


                                         return detector.onTouchEvent(m);
                                     }
                                 }
    );
}

@Override
public boolean onDown(MotionEvent event) {

    return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
                       float velocityX, float velocityY) {

    return true;
}

@Override
public void onLongPress(MotionEvent event) {
    Log.d("Long Press", ".........");
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                        float distanceY) {
    Log.d("X,Y", " " + e1.getX() + " " + e2.getX() + " " + e1.getY() + " " + e2.getY());

    return true;
}

@Override
public void onShowPress(MotionEvent event) {

}

@Override
public boolean onSingleTapUp(MotionEvent event) {

    return true;
}


}

我想为myLayout2使用不同的GestureDetector。

1 个答案:

答案 0 :(得分:0)

试试这个,注意把一个not运算符放在onTouchEvent返回前面。

package com.android.example.detectortestproj;

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    LinearLayout myLayout0;
    LinearLayout myLayout1;
    GestureDetectorCompat mDetector0;
    GestureDetectorCompat mDetector1;

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

    @Override
    protected void onStart() {
        super.onStart();

        myLayout0 = (LinearLayout) findViewById(R.id.LinearLayout1);
        myLayout1 = (LinearLayout) findViewById(R.id.LinearLayout2);

        mDetector0 = new GestureDetectorCompat(this, new SimpleGestureListener(0));
        mDetector1 = new GestureDetectorCompat(this, new SimpleGestureListener(1));

        myLayout0.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return !mDetector0.onTouchEvent(motionEvent);
            }
        });

        myLayout1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return !mDetector1.onTouchEvent(motionEvent);
            }
        });

    }

    public class SimpleGestureListener extends GestureDetector.SimpleOnGestureListener{

        private int mControlId = -1;
        private static final String TAG = "SimpleGestureListener";

        public SimpleGestureListener(int controlId){
            super();
            mControlId = controlId;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.d(TAG, "onFling() " + mControlId + " called with: " + "e1 = [" + e1 + "], e2 = [" +
                    e2 + "], velocityX = [" + velocityX + "], velocityY = [" + velocityY + "]");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d(TAG, "onDoubleTap()  " + mControlId + " called with: " + "e = [" + e + "]");
            return true;
        }
    }
}