开发者

How to detect a double touch/taps on an Android ListView?

Do you know about how to detect two touches/taps on a ListView?

I am trying to have the following method called when double touched:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

Thanks in a开发者_JAVA百科dvance.


Why aren't you using a Long Touch? Or are you using that already for something else? The advantages over a long touch over a double touch:

  • Long Press is a recommeded interaction in the UI Guidelines, double touch is not.
  • It's what users expect; a user might not find a double touch action as they won't go looking for it
  • It's already handled in the API.


Edit:

See it here: Involving CountDownTimer: https://github.com/NikolaDespotoski/DoubleTapListView Involving Handler: http://code.google.com/p/double-tap-listview-handler/ https://github.com/NikolaDespotoski/DoubleTapListViewHandler My brief explaination: http://mobisys.in/blog/2012/01/how-to-detect-double-taps-on-listview-items-in-android/


If you are set on using a double tap in your UI, you could always save state in your Actvity or other container.

A simple implementation would be storing the time the touch was registered, and comparing against that on the next touch event to determine if the time lapsed since the second touch falls with in whatever range you are defining a double tab to mean.


I think that a double tap on a ListView can be totally valid, particularly if it is done over an unoccupied area. For instance I have a diary application listing actions for today. This is already so that long presses allow actions to be edited or deleted. But left / right touch gestures are used to quickly move to the previous / next day, and a double tap is a shortcut to get back to "today". Is that intuitive? Well, close enough, I think.


I think for double tap or touch you have to use GestureDetector and its methods on touch for single and double tap. here is a link for refrence http://www.linux.com/learn/tutorials/715651-make-android-multi-touch-coding-simpler-with-gesturedetector


My answer is base on this link,and it work for me.


final GestureDetector gestureDectector = new GestureDetector(mContext, new GestureListener());
        listview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                return false;
            }
            gestureDectector.onTouchEvent(event);
            return true;
        }
    });

class GestureListener extends GestureDetector.SimpleOnGestureListener {

public boolean onDoubleTap(MotionEvent e) { int position = listview.pointToPosition((int) e.getX(), (int) e.getY()); if (position < 0) { return true; } Toast.makeText(mContext, "double-click " + mList.get(position).content, Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { int position = listview.pointToPosition((int) e.getX(), (int) e.getY()); if (position < 0) { return true; } Toast.makeText(mContext, "single-click " + mList.get(position).content, Toast.LENGTH_SHORT).show(); return true; } }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜