Home > Net >  Double Click Problem in android with java
Double Click Problem in android with java

Time:12-03

I had created a Java Class for double click but I am not able to double Click on the Item because when i click for only one time the item doesn't wait for double click !

Double Click Class Code

package com.programiner.studyvillaadmin.Classes.Listener;

import android.view.View;

public abstract class DoubleClickListener implements View.OnClickListener {

    private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds

    long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
            onDoubleClick(v);
            lastClickTime = 0;
        } else {
            onSingleClick(v);
        }
        lastClickTime = clickTime;
    }

    public abstract void onSingleClick(View v);
    public abstract void onDoubleClick(View v);
}

CodePudding user response:

private long mLastClickTime = 0;
    @Override
    public void onClick(View v) {
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
            return; *// return when second click happen in second*
        mLastClickTime = SystemClock.elapsedRealtime();
        GameSound.getInstance(getApplicationContext()).sound__(GameSound.buttonClick);

        if (v == findViewById(R.id.tvUsername)) {
            StartActivity(UserProfile.class);
        }
    }

example

public long mLastClickTime = 0; //declare and define public

holder.itemView.setOnClickListener(new View.OnClickListener() {  //click lister of itemview of RV Adapter
                            @Override
                            public void onClick(View v) {
                                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
                                    return; // return when second click happen in second*
                                mLastClickTime = SystemClock.elapsedRealtime();

                                //do stuff here...
                            }
                        });
  • Related