并不总是长时间单击ExpandableListView子项

时间:2015-10-14 18:05:29

标签: android expandablelistview onlongclicklistener

我使用此源https://github.com/sreekumarsh/android/tree/master/Drag%20N%20Drop为子项实现ExpandableListView DragNDrop。所有主要功能都在类中,它扩展了ExpandableListView。它覆盖了onTouchEvent(MotionEvent事件)方法,以及我指示的长按功能:

    int flatPosition = pointToPosition(x, y);
    dragRatio = getHeight() / screenHeight;
    long packagedPosition = getExpandableListPosition(flatPosition);

    Runnable mLongPressed = new Runnable() {
        public void run() {
            event.setLocation(x, y);
            touchHandler(event);
            pressedItem = true;
        }
    };

    if (action == MotionEvent.ACTION_DOWN
            && getPackedPositionType(packagedPosition) == 1) {
        if (dragOnLongPress) {
            if (pressedItem) {
                mDragMode = true;
                pressedItem = false;
            } else {
                pressedItem = true;
                **handler.postDelayed(mLongPressed, 500);**
                return true;
            }
        } else if (x < dragOffset) {
            mDragMode = true;
        }
    }  

但是,有一些问题。长时间点击并非总是如此。我怎样才能更可靠地做到这一点(不使用GestureDetector类)。

1 个答案:

答案 0 :(得分:0)

我将分享我的个人实施,这似乎工作正常。

final Handler handler = new Handler();
final Runnable mLongPressed = new Runnable() {
            public void run() {
                vibrator.vibrate(100);


                Log.i("imageclick", "LONG_CLICK");
            }
        };

whateverViewYouWantToDetectLongClick.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Animation a = null;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i("imageclick", "ACTION_DOWN");
                    handler.postDelayed(mLongPressed, 750);
                    break;

                case MotionEvent.ACTION_UP:

                    Log.i("imageclick", "ACTION_UP");
                    handler.removeCallbacks(mLongPressed);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    Log.i("imageclick", "ACTION_CANCEL");
                    handler.removeCallbacks(mLongPressed);
                    break;
                default:
                    break;
                }


                return true;
            }
        });