使用两个SimpleOnGestureListener

时间:2012-12-28 19:51:21

标签: android

我想实现一个应用程序,其中两个计数器(或更多)递增和递减,向上和向下滑动。因此我的想法是“主要活动”使用两个自定义视图(称为CounterView.java)。 该应用程序显示两个计数器,但交互不适用于SimpleOnGestureListener。 这是代码。 activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:orientation="vertical"
    android:background="@color/opaque_black"
    tools:context=".MainActivity" >

<de.thomaslaemmlein.counter.CounterView
    android:id="@+id/counterView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</de.thomaslaemmlein.counter.CounterView>

<de.thomaslaemmlein.counter.CounterView
    android:id="@+id/counterView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@id/counterView1" >
</de.thomaslaemmlein.counter.CounterView>

MainActivity.java:

package de.thomaslaemmlein.counter;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {

    public MainActivity()
    {       
        Log.d("MainActivity", "Constructor");
    }

    protected void onDestroy() 
    {
        Log.d("MainActivity", "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

counter_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/counterTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/opaque_white"
        android:includeFontPadding="false"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="250sp" />

</RelativeLayout>

CounterView.java:

package de.thomaslaemmlein.counter;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CounterView extends LinearLayout {
    TextView m_CounterTextView;

    private int m_CurrentNumber;

    private int m_MaximalNumber = 99;

    public CounterView(Context context) {
        super(context);
        init();
    }

    public CounterView(Context context, AttributeSet attr) 
    {
        super(context, attr);
        init();
    }

    private void init()
    {
        Log.d("CounterView", "init");

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View root = inflater.inflate(R.layout.counter_view, this, false);

        m_CurrentNumber = 0;

        m_CounterTextView = (TextView)root.findViewById(R.id.counterTextView);

        m_CounterTextView.setText( Integer.toString(m_CurrentNumber));

        addView(root);
    }

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

    SimpleOnGestureListener simpleOnGestureListener
       = new SimpleOnGestureListener(){


      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
       String swipe = "";
       float sensitvity = 50;

       // TODO Auto-generated method stub
       if((e1.getX() - e2.getX()) > sensitvity){
        swipe += "Swipe Left;";
       }else if((e2.getX() - e1.getX()) > sensitvity){
        swipe += "Swipe Right;";
       }else{
        swipe += ";";
       }

       if((e1.getY() - e2.getY()) > sensitvity){
        swipe += "Swipe Up;";
        if (m_CurrentNumber < m_MaximalNumber)
        {
            m_CurrentNumber++;
            m_CounterTextView.setText( Integer.toString(m_CurrentNumber));      
        }
       }else if((e2.getY() - e1.getY()) > sensitvity){
           if (m_CurrentNumber > 0)
           {
               m_CurrentNumber--;
               m_CounterTextView.setText( Integer.toString(m_CurrentNumber));
           }
        swipe += "Swipe Down;";
       }else{
        swipe += ";";
       }

       Log.d("onFling", swipe);

       return super.onFling(e1, e2, velocityX, velocityY);
      }
       };

       GestureDetector gestureDetector
       = new GestureDetector(simpleOnGestureListener);

}

不会调用“onFling”方法。

在这种情况下有人帮助我吗?

1 个答案:

答案 0 :(得分:0)

onTouchEvent 替换为

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

 return true; // This function must return true for onFling() to be called.
}