0

I have a list using RecyclerView and a OnClickListener to handle the item clicks.

I used the same solution from: Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?

public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

    public TextView txtViewTitle;
    public ImageView imgViewIcon;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        itemLayoutView.setOnClickListener(this);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
        imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
    }

    @Override
    public void onClick(View v) {
        // start a new fragment
    }

}

The problem is that if I quickly select two items, it will execute the onClick() two times and on this case, starting two new fragments.

Am I doing something wrong? What is the best approach to avoid this? Is a simple boolean flag enough for all the cases?

UPDATE: This is a "pack selection screen", which you will select which pack you want to play. Then it will show a new fragment with all the puzzles from the selected pack.

Thanks!

Community
  • 1
  • 1
thiagolr
  • 6,909
  • 6
  • 44
  • 64

1 Answers1

1

You are doing right about the onClick. Try this:

 @Override
    public void onClick(View v) {
        if(fragment is playing) {
            return;
        }
        // start a new fragment
    }
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
chad
  • 141
  • 1
  • 5
  • I didn't understand the if statement, can you explain better? – thiagolr Aug 19 '16 at 15:51
  • @thiagolr , if your target fragment is visible, you should not show it again. see this: [how-to-know-if-a-fragment-is-visible](http://stackoverflow.com/questions/16652095/how-to-know-if-a-fragment-is-visible). – chad Aug 22 '16 at 00:41