-1

I have listview in my application, When any listitem is tapped navigating to summary screen. Here when i double tap its opening two summary screens. How to handle double tap?

Could any one have any idea, suggest me.

B Bhanu Chander
  • 619
  • 2
  • 6
  • 17

2 Answers2

2

Try Something like this

    YourView.setOnTouchListener(new View.OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(Youractivity.this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.d("TEST", "onDoubleTap");
                return super.onDoubleTap(e);
            }
        });

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });
Masum
  • 4,879
  • 2
  • 23
  • 28
0

To do this you could use the Handler class.

Create a new variable of type int called something like counter and set it to 0.
Then find your listview and call the method setOnItemClickListener(listener) on it. Here you need to check if the counter is 0 or 1.
If it's 0 increase it by 1 and add a Handler postDelayed thread to reset the counter (set it to zero). If it's 1, reset the counter and do your stuff.


EDIT:

I just found this answer saying it's better to use the long press action how described in the UI Guidelines. Also it's what the user expect.

So maybe use the long press instead of double tap?


But here is some example code:

int counter = 0; //our counter to check if the item has been tapped already

Handler h = new Handler(); //import from android.os

ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(adapter); //set your adapter here
lv.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {
      switch(counter) {
          case 0: //first tap
            counter++; //increase the counter
            h.postDelayed(new Runnable() {
              @Override
              public void run() {
                counter = 0;
              }
            }, 2000); //set the counter to 0 after 2 seconds (2000 milliseconds)
            break;
          case 1: //second tap
            counter = 0; //reset the counter
            //Do your stuff here
            break;
      }
  }
});

Here some references:

Community
  • 1
  • 1
Axel
  • 568
  • 7
  • 18