1

I have a RecyclerView where each ViewHolder contains a nested Linear Layout with some TextViews and a button. (The button is a Facebook ShareButton but I see the same behavior with a regular Button as well.) When I click the ShareButton once, nothing happens. When I click it again, it is successful.

Some clues:

  1. Other answers on stackoverflow say to set the button's focusable and focusableInTouchMode to false -- this has not made a difference in my case.
  2. By using some print statements, I see that the first time I click the button, it calls bind again on each ViewHolder.
  3. If I minimize the app, then pull it up again, the first click works.

Relevant bind code below:

public void bind(Plaque plaque) {
    Log.d(TAG, "BINDING!");

    LinearLayout layout = (LinearLayout) itemView;
    layout.setDrawingCacheEnabled(true);
    layout.buildDrawingCache();
    Bitmap map = layout.getDrawingCache();

    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(map)
            .build();
    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();
    mShareButton.setShareContent(content);
}

When I click mShareButton the first time, it just recalls this code (triggering the print statement at the top). When I click it again, it sends the content to Facebook, as it's supposed to do. Any ideas how to get it to work the first time, without calling bind again?

Noam
  • 550
  • 2
  • 11
  • where is this method called? ViewHolder - itemView.setOnClickListener() or in BindViewHolder()? – Darshan Sep 06 '18 at 06:21
  • It's part of the `RecyclerView.ViewHolder`. It's not part of `onBindViewHolder` and it's not in an `onClick` or a `setOnClickListener`. – Noam Sep 06 '18 at 14:41

1 Answers1

1

I am still not sure why the bind method was being called when my button was clicked. But I was able to avoid this by moving all of the button logic out of the bind method and into the ViewHolder constructor method. I also switched to using ShareDialog within a regular Button so that I could have more control over the onClick.

Noam
  • 550
  • 2
  • 11