【问题标题】:Android gesture overlay action bar touch confusionAndroid手势叠加操作栏触摸混乱
【发布时间】:2014-03-12 03:22:30
【问题描述】:

我的手势覆盖出现双击问题。当我按下操作栏中的某个项目时,它也会导致手势过度并触发侦听器。当我使用操作栏并按下更新或刷新或者我有在那里时,我不希望单击关闭?

activty_main.xml

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


 <android.gesture.GestureOverlayView
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/gestureOverlayView1"
         android:layout_width="match_parent"

         android:layout_height="match_parent" 
         tools:context=".MainActivity">



    <LinearLayout 
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="10dp" 
        android:layout_gravity="start">



    <TextView
        android:id="@+id/timerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="5sp"
        android:text="@string/startTime"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/hanziTextView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:gravity="center"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/instructionTextView"
        android:layout_width="574dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:textSize="40sp" />

</LinearLayout>

</android.gesture.GestureOverlayView>

   <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

   <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />

</android.support.v4.widget.DrawerLayout>

mainactivity.class 上创建

 detector=new GestureDetector(getBaseContext(), this);

        gLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!gLibrary.load()) {
            finish();
       }  

        GestureOverlayView gOverlay = 
                (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
            gOverlay.addOnGesturePerformedListener(this); 

           gOverlay.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
            gOverlay.setGestureColor(Color.TRANSPARENT);
            gOverlay.setUncertainGestureColor(Color.TRANSPARENT);

OnClickListener、OnLongClickListener、OnGesturePerformedListener、OnGestureListener等各种监听器

@Override
public boolean onDown(MotionEvent arg0) {

    return true;
}

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {


    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    onSwipeRight();
                } else {
                    onSwipeLeft();
                }
            }
        } else {
            if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
            }
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
}


public void onSwipeRight() {
    Log.d(TAG, "right");

}


public void onSwipeLeft() {

    //Alot of stuff  

}

public void onSwipeTop() {
}

public void onSwipeBottom() {
    //Toast.makeText(this, "Pause", Toast.LENGTH_SHORT).show();

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

}
@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
    detector.onTouchEvent(e);

    return super.dispatchTouchEvent(e);
}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub
    Log.i(LOGTAG, "testsing4");
    //Toast.makeText(getApplicationContext(), "Show Press gesture", 100).show();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.i(LOGTAG, "testsing3");
    return detector.onTouchEvent(event);
}

@Override
public boolean onSingleTapUp(MotionEvent e) {

    //stuff
    return true;
}

public boolean onSingleTapConfirmed(MotionEvent e) { 

    //Toast.makeText(getApplicationContext(), "Single Tap Gesture", 100).show();
    Log.i(LOGTAG, "testsing1");
    return true; 
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        //stuff

    }

【问题讨论】:

    标签: android gesture multi-touch


    【解决方案1】:

    DoubleTap in android

    为操作栏添加手势基本相同。

    另外,请考虑使用长按。正如 Jems 所提到的,它是一种更自然的输入法,并且更容易实现

    【讨论】:

    • 不,我不想要手势。问题是我有图标,当我按下位于操作栏中的那些图标时,它也会触发单击。它们都触发并给了我两个不好的结果,因为用户只想要一个动作。长按已用于其他用途,无法更改操作。
    • 恐怕我不会100%按照你的描述来
    • 考虑stackoverflow.com/questions/5434258/… 还要考虑调整布局大小,使手势检测器与操作栏不在同一区域。或者玩分层按钮,以便按钮上的触摸侦听器处理操作,然后终止事件
    • androidsnippets.com/handle-touch-events-ontouchevent这里有一个很简单的解释
    • 我使用了 onShowPress,它不那么敏感,而且效果很好。因此,操作栏再次可以包含“BAR”上的菜单图标项。那有意义吗?我有一个位于操作栏下方的文本视图,当我按下单点时,我会显示文本,但是当我按下操作栏以选择其中一个项目时,它也会使用单点,这不是我想要的。那有意义吗?如果不是,谢谢您的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多