0

I have a weird issue with my checkboxes in RecyclerView.

When I click once on it everything works perfectly (move and change style) but when I click twice (enough fast) it performs some part of the code (just move it down and uncheck). I want to prevent it but I don't know how.

I tried the solution from here https://stackoverflow.com/a/16514644/10802597 but it makes it even worse (maybe I do it in a wrong way).

Here is my code:

holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int currentPosition = holder.getAdapterPosition();
            if(isChecked){
                    FinalListItem finalItemBefore = finalListItems.get(currentPosition);
                    FinalListItem finalItemAfter = new FinalListItem(finalItemBefore.getName(), true);
                    finalListItems.remove(finalItemBefore);
                    finalListItems.add(finalItemAfter);
                    recyclerView.scrollToPosition(0);
                    holder.linearLayout.setBackgroundResource(R.drawable.listitem_green);
                    notifyItemMoved(currentPosition, getItemCount() - 1);

            }
            else{
                finalListItems.get(currentPosition).setChecked(false);
                notifyItemChanged(currentPosition);
            }

        }
    });

Here is image which shows how i want to do it

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MrFisherman
  • 720
  • 1
  • 7
  • 27

1 Answers1

0

You can use this

private long mLastClickTime = 0;
     if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                        return;
                    }
                    mLastClickTime = SystemClock.elapsedRealtime();

inside your click and it will prevent another click for 1 sec

but don't use it inside your checkbox method, from what I can see its adapter so set ID for the whole row and put this method on the row click.

Hossam Hassan
  • 795
  • 2
  • 13
  • 39