android: How to increase long press time for listview items in android?

不羁的心 提交于 2021-01-27 06:44:46

问题


I have a custom listview, in which items are scroll horizontal. I want to perform single touch and longpress for items for 5 sec for listview items. How to do this. How can i increase longpress time interval for listview items to 5 sec.


回答1:


This replicates onLongPress more accurately because it does not wait for the user to lift their finger before executing. Was written specifically for ViewPager, but should be able to apply similar logic.

// long press duration in milliseconds
public static final int LONG_PRESS_DURATION = 2000;

private boolean mIsTouching = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == event.ACTION_DOWN) {
        mIsTouching = true;
    } else if (event.getAction() == event.ACTION_UP) {
        mIsTouching = false;
    }

    return super.onTouchEvent(event);
}

@Override
public void onLongPress(MotionEvent event) {

    // subtracts the system set timeout since that time has already occured at this point
    int duration = LONG_PRESS_DURATION - getLongPressTimeout();

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mIsTouching) {
                do something...
            }
        }
    }, duration > 0 ? duration : 0);
}



回答2:


You can setOnItemLongClickListener, then you can set the view you touch a OnTouchListener, you can record the time when ACTION_DOWN and the time ACTION_UP, so you can calculate if the time is more than 5 sec between ACTION_DOWN and ACTION_UP.




回答3:


You cant change the delay. It is hardwired in the android framework. I also came across same problem days ago.

You can use setOnTouchListener for it manually.

Example :

private long then;
private int longClickDur= 5000; //5 seconds


//you can use any view you want
ImageView imageView = (ImageView) findViewById(R.id.longclick_view);
imageView.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          then = (long) System.currentTimeMillis();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          if ((System.currentTimeMillis() - then) > longClickDuration) {
            /* Long click behaviour will go here*/
            Toast.makeText(context, "yay, long click", Toast.LENGTH.SHORT);
            return false;
          } else {
            /* LONG CLICK FAILED*/
            Toast.makeText(context, "TRY AGAIN", Toast.LENGTH.SHORT);
            return false;
          }
        }
        return true;
      }
    });


来源:https://stackoverflow.com/questions/41327179/android-how-to-increase-long-press-time-for-listview-items-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!