LongClick listener duration

I want to reduce the time after which my list view responds to a long click listener. Is it possible to shorten the duration of clicks?

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;
            }
        });
+4
source share
1 answer

You can use 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;
            }
        });
+4
source

Source: https://habr.com/ru/post/1570417/


All Articles