繁体   English   中英

修改列表视图中onItemLongClick的失效时间

[英]Modify the lapse of onItemLongClick in a listview

我需要修改onItemLongClick的失效时间(默认持续时间对于老人来说太快了)。 有人能帮我吗?

    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            // Do something
            ...
        }

    });

您将无法使用OnItemLongClickListener实现此OnItemLongClickListener ,您将需要自己的OnTouchListener实现来定义/确定长点击持续时间。

我目前无法测试代码,但这应该为您提供从哪里开始的基本思路:

private long mTimestampDown;
private long mTimestampUp;
private final int longPressDurationMs = 2000; // 2 seconds
private boolean isLongPress = false;

@Override
public boolean onTouchEvent(MotionEvent e) {
 switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
      mTimestampDown = System.currentTimeMillis();
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
       mTimestampUp = System.currentTimeMillis();
       if(mTimestampUp - mTimestampDown > longPressDurationMs) 
         isLongPress = true;
        break;
    default:
        break;
}
return isLongPress;
}

您的类应扩展OnTouchListener

为什么不使用OnTouchListener 定义您的持续时间,然后在ACTION_DOWN上检查触摸时间。

ACTION_UP

if  ( the time > your duration ) { 
    do the job you want 
}

也许这不是一个完美的例子,但是有2个全局变量(IsDown和position)可以解决我的问题。

    listview.setOnTouchListener(new AdapterView.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent e) {
            switch (e.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    setIsDown(true);
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    setIsDown(false);
                    break;
                default:
                    break;
            }
            return false;
        }

    });

    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            setPosition(position);

            final Handler h = new Handler();
            h.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    if (getIsDown()) {


                         // Do something
                         ...


                    }
                }
            }, 1000); // milliseconds added to longpress

            return true;
        }

    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM