How to detect double taps on ListView items in And

2019-07-01 15:12发布

i want to do some thing like instagram app when user double tab on photo it make like and while he just tab one time it open as full screen

7条回答
迷人小祖宗
2楼-- · 2019-07-01 15:44

if ()//add this block

else if(count>=3){previousMil2=System.currentTimeMillis(); 

you can not change click another row thne click selected row.This code prevent click another view then you want to click row.

then change if control

if(count>=2 && (System.currentTimeMillis()-previousMil<=1000 ||System.currentTimeMillis()-previousMil2<=1000) )
查看更多
够拽才男人
3楼-- · 2019-07-01 15:48

I made this one based on Graeme's answer, it's works for me.

class VibinOnItemClickListener implements OnItemClickListener {

    private boolean nonDoubleClick = true;
    private long firstClickTime = 0L;
    private final int DOUBLE_CLICK_TIMEOUT = 200;//ViewConfiguration.getDoubleTapTimeout();

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        // @TODO check and catch the double click event
        synchronized(VibinOnItemClickListener.this) {
            if(firstClickTime == 0) {
                firstClickTime = SystemClock.elapsedRealtime();
                nonDoubleClick = true;
            } else {
                long deltaTime = SystemClock.elapsedRealtime() - firstClickTime;
                firstClickTime = 0;
                if(deltaTime < DOUBLE_CLICK_TIMEOUT) {
                    nonDoubleClick = false;
                    this.onItemDoubleClick(parent, view, position, id);
                    return;
                }
            }

            view.postDelayed(new Runnable() {

                @Override
                public void run() {
                    if(nonDoubleClick) {
                        // @TODO add your logic for single click event
                    }
                }

            }, DOUBLE_CLICK_TIMEOUT);
        }
    }

    public void onItemDoubleClick(AdapterView<?> parent, View view, int position, long id) {
        // @TODO override this method with your own logic
    }
}
查看更多
We Are One
4楼-- · 2019-07-01 15:51

I think this answers to Your question as well. Check this link:

how to implement double click in android

查看更多
倾城 Initia
5楼-- · 2019-07-01 15:58

If you don't want to use a GestureListener for every list item or a GestureListener for the ListView which then needs to determine the item located under the double tap area you can use the following extension to the onItemClickListener.

This implementation waits to confirm a single click so it introduces single click latency.

private Object mClickedItem;
@Override
public void onItemClick(final AdapterView<?> adapter, final View view, final int index, final long id) {
    synchronized (DoubleClickAdapter.this) {        
        if(mClickedItem != null && mClickedItem.equals(getItem(index))) {
            mClickedItem = null;
            onDoubleClick(adapter, view, index, id);
            return;
        }
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                synchronized (DoubleClickAdapter.this) {
                    if(mClickedItem != null) {
                        mClickedItem = null;
                        DoubleClickAdapter.super.onItemClick(adapter, view, index, id);
                    }
                }
            }
        }, ViewConfiguration.getDoubleTapTimeout());    
        mClickedItem = getItem(index);
    }
}

This implementation doesn't wait to confirm a single click, it fires a single click event on the first click of a double click:

private Object mClickedItem;
@Override
public void onItemClick(final AdapterView<?> adapter, final View view, final int index, final long id) {
    synchronized (DoubleClickAdapter.this) {        
        if(mClickedItem != null && mClickedItem.equals(getItem(index))) {
            mClickedItem = null;
            onDoubleClick(adapter, view, index, id);
            return;
        }
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                synchronized (DoubleClickAdapter.this) {
                    mClickedItem = null;
                }
            }
        }, ViewConfiguration.getDoubleTapTimeout());    
        mClickedItem = getItem(index);
        DoubleClickAdapter.super.onItemClick(adapter, view, index, id);
    }
}
查看更多
做自己的国王
6楼-- · 2019-07-01 15:59

You can use the code below (lvData is a ListView variable):

int previousPosition=-1;
int count=0;
long previousMil=0;
private void addEvents() {
    lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(previousPosition==position)
            {
                count++;
                if(count==2 && System.currentTimeMillis()-previousMil<=1000)
                {
                    Toast.makeText(MainActivity.this,"Double tap at"+position,Toast.LENGTH_SHORT).show();
                    count=1;
                }
            }
            else
            {
                previousPosition=position;
                count=1;
                previousMil=System.currentTimeMillis();
            }
        }
    });
}
查看更多
【Aperson】
7楼-- · 2019-07-01 16:01

If you don't need to prevent single click to be fired, you can do much more simple:

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
    long currTime = System.currentTimeMillis();
    if (currTime - mLastClickTime < ViewConfiguration.getDoubleTapTimeout()) {
        onItemDoubleClick(adapterView, view, position, l);
    }
    mLastClickTime = currTime;
}

public void onItemDoubleClick(AdapterView<?> adapterView, View view, int position, long l) {
    ToastHelper.showToast("Double click");
}
查看更多
登录 后发表回答