【问题标题】:Not always work long click on ExpandableListView child item长时间单击 ExpandableListView 子项并不总是有效
【发布时间】:2015-10-14 18:05:28
【问题描述】:

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

    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 类)。

【问题讨论】:

  • 不总是工作是什么意思?你想要的行为是什么?
  • 当检测到长按时,我设置标志,然后根据操作,我选择或移动子项。如果可以,请在我的问题中打开链接并找到 DragNDropListView 类。它在 touchHandler 方法中有所有代码。谢谢

标签: android expandablelistview onlongclicklistener


【解决方案1】:

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

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;
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多