具有onTouch和onItemLongClick监听器的Android列表视图



你好,我还在开发一个安卓应用程序。早些时候,我创建了一个列表视图,其中包含用于滑动和双击的 OnTouch 侦听器以及用于执行拖放的 OnItemLongClick。这是一个简单的,只包含一个文本视图现在,我更改为带有文本视图和按钮的自定义列表视图行。使用此配置时,两种手势检测中只有一个可用。当我将android:longclickable设置为true时,拖放是可能的,但它不会检测到OnTouch。反之亦然。有没有人有使用两种手势检测的解决方案?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content" >
    <TextView   
     android:id="@+id/rowTextView"   
     android:layout_width="fill_parent"   
     android:layout_height="wrap_content"
     android:padding="10dp"  
     android:textSize="16sp" >  
    </TextView>
    <ImageButton
     android:id="@+id/rowImgButton"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:background="#ffff00"
     android:focusable="false"
     android:focusableInTouchMode="false"
     android:layout_alignRight="@+id/rowTextView" />
</RelativeLayout>
这可能

适用于,您可以创建一个GestureDetector:

myGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public void onLongPress(MotionEvent e) {
                //Handle long press
            }
        });
myGestureDetector.setIsLongpressEnabled(true);

然后在您的 onTouch 方法中:

public boolean onTouch (View v, MotionEvent event) {
    myGestureDetector.onTouchEvent(e);
    ...
}

然后,当用户执行长按时,onLongPress 方法应触发

最新更新