在Linearlayout设置为可见之后,GestureDetectorCompat无法正常工作

时间:2015-07-01 08:31:13

标签: android android-layout

我遇到了问题:我正在使用GestureDetectorCompat进行手势检测。我想在doubletap之后设置linearlayout。如果新的线性布局被轻敲一次,我想隐藏它。 因此,我得到了这个activity_main.xml,第二个LinearLayout设置为vibility ="去了":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#000000">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ImageView>

</LinearLayout>

<LinearLayout android:id="@+id/overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#99000000"
    android:clickable="true"
    android:visibility="gone">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="90dp"
            android:id="@+id/headerLbl"
            android:text="Motivate your Friends!"
            android:textColor="#ffffffff"
            android:textSize="25dp"
            android:textAlignment="center"
            android:textStyle="bold"
            android:gravity="center" />

</LinearLayout>

Double Tap工作正常,LinearLayout变得可见。但是下面的单击还没有工作......我想我可能不得不在第二个线性布局上设置手势探测器......但是如何?

这是我的MainActivity的一部分:

package com.testapp.test;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.InputStream;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;


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


private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;


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



    mDetector = new GestureDetectorCompat(this,this);
    mDetector.setOnDoubleTapListener(this);



}


@Override
public boolean onTouchEvent(MotionEvent event){
    this.mDetector.onTouchEvent(event);
    // Be sure to call the superclass implementation
    return super.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent event) {
    Log.d(DEBUG_TAG,"onDown: " + event.toString());
    return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
                       float velocityX, float velocityY) {
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
    return true;
}

@Override
public void onLongPress(MotionEvent event) {
    Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                        float distanceY) {
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
    return true;
}

@Override
public void onShowPress(MotionEvent event) {
    Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}

@Override
public boolean onSingleTapUp(MotionEvent event) {
    Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
    return true;
}

@Override
public boolean onDoubleTap(MotionEvent event) {
    LinearLayout shareOverlay = (LinearLayout) findViewById(R.id.overlay);

    shareOverlay.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mDetector.onTouchEvent(event);
            return false;
        }
    });

    shareOverlay.setVisibility(shareOverlay.VISIBLE);

    Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent event) {
    Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    LinearLayout shareOverlay = (LinearLayout) findViewById(R.id.overlay);
    shareOverlay.setVisibility(shareOverlay.GONE);
    Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
    return true;
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您只考虑双击,您还应该考虑OnGestureListener.onSingleTapUp(MotionEvent)

编辑:你为什么使用mDetector?删除它,只使用重写的doubleTap方法。