longClick侦听器的持续时间

时间:2015-01-08 07:05:32

标签: android onlongclicklistener

我希望减少列表视图响应长点击监听器的时间。是否可以缩短到长点击持续时间?

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

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

                    if(selectedHabit){
                        Intent intent = new Intent(parent.getContext(),AddScheduleEventActivity.class );
                        startActivityForResult(intent, CREATE_EVENT);
                        return true;
                    }



                return false;
            }
        });

1 个答案:

答案 0 :(得分:4)

您可以使用OnTouchListener:

    private int lastTouchedViewId = -1;
    private long duration = System.currentTimeMillis();
    private long LONG_CLICK_DURATION = 1000;

...

view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                switch (motionEvent.getAction()) {


                    case MotionEvent.ACTION_DOWN:
                        if (lastTouchedViewId != view.getId()) {
                            lastTouchedViewId = view.getId();
                            duration = System.currentTimeMillis();
                        }
                        else
                        {

                            if(duration-System.currentTimeMillis()> LONG_CLICK_DURATION)

                            doStuff();
                        }
                        return true;

                    case MotionEvent.ACTION_UP:
                        lastTouchedViewId = -1;
                        return true;
                }


                return false;
            }
        });