-1

I have used grid view in my project. When I click on grid view item speedily, it calls the same method twice a time. But I slowly click, then it works perfectly.

In method below I call the dialog box. When I click the item it opens the dialog box but when I speedily click the item then dialog box open two times. What should I do to solve this?

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
        showDialog(position);
    }
});
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Radhika
  • 13
  • 6

2 Answers2

2

You can use this code which doesn't allow multiple speed clicks instead of your onclicklistener.

public abstract class SingleClickListener implements View.OnClickListener {
    private static final long THRESHOLD_MILLIS = 500L;
    private long lastClickMillis;

    @Override
    public void onClick(View v) {
        long now = SystemClock.elapsedRealtime();
        if (now - lastClickMillis > THRESHOLD_MILLIS) {
            onClicked(v);
            lastClickMillis = 0;
        } else {
            lastClickMillis = now;
        }
    }

    public abstract void onClicked(View v);
}

and instead of normal onclicklistener use this

 stage1_nxt.setOnClickListener(new SingleClickListener() {
Nivil Boban
  • 286
  • 1
  • 13
0

I have solution for opening twice time method. I can define the below condition through solved it:

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    private long mLastClickTime = 0;

    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) return;

        mLastClickTime = SystemClock.elapsedRealtime();
        grideProductAdd(position);          
    }
});
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Radhika
  • 13
  • 6