GestureDetector没有处理新的布局

时间:2017-03-05 20:49:37

标签: java android xml gesturedetector

我创建了标准手势检测器,可根据给定的手势更改文本视图。当我的XML仅包含我希望更改的相对布局和文本视图时,一切正常。但是,当我添加网格布局和几个图标时,手势检测器不再有效,只是不像以前那样更改文本视图。 代码是;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/homebackground"
tools:context="com.example.james.fitness.MainActivity"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

    <GridLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/Grid1">

        <TextView
            android:id="@+id/someText"
            android:text="sometext"
            android:foregroundGravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleX="5"
            android:scaleY="5"
            android:textColor="@color/colorAccent" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/fitnessbutton"
            android:id="@+id/fitnessbutton"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/drinkbutton"
            android:id="@+id/drinkbutton"
            android:layout_column="2"
            android:layout_row="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/foodbutton"
            android:id="@+id/foodbutton"
            android:layout_column="3"
            android:layout_row="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill_horizontal"
            android:gravity="center"
            android:paddingBottom="100dp" />
    </GridLayout>
</RelativeLayout>

和Java

import android.app.Activity;
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.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {


    private TextView someText;
    private GestureDetectorCompat gestureDetector;


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

        someText = (TextView)findViewById(R.id.someText);
        this.gestureDetector = new GestureDetectorCompat(this, this);
        gestureDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        someText.setText("SINGLETAP");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        someText.setText("DOUBLETAP");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        someText.setText("DOUBLETAPEVENT");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        someText.setText("DOWN");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        someText.setText("PRESS");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        someText.setText("SINGLETAPUP");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        someText.setText("SCROLL");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        someText.setText("LONGPRESS");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        someText.setText("FLING");
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return false;

    }

}

请注意,如果我注释掉网格布局和图像视图,则代码会再次更改textview。我只能假设手势探测器现在埋在网格布局下但不确定?

1 个答案:

答案 0 :(得分:1)

尝试在布局中删除这些行,它们在您的情况下无用

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"