Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block item click callback during item layout. #301

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airbnb.epoxy;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
Expand Down Expand Up @@ -54,7 +55,11 @@ public void onClick(View v) {
if (originalClickListener == null) {
throw new IllegalStateException("Long click listener was set.");
}
originalClickListener.onClick(model, object, v, holder.getAdapterPosition());

final int adapterPosition = holder.getAdapterPosition();
if (adapterPosition != RecyclerView.NO_POSITION) {
originalClickListener.onClick(model, object, v, adapterPosition);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.Collections;
import java.util.List;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class ControllerLifecycleHelper {
private EpoxyViewHolder viewHolder;

Expand Down Expand Up @@ -36,11 +39,16 @@ public View bindModel(EpoxyModel<?> model) {
}

static EpoxyViewHolder createViewHolder(BaseEpoxyAdapter adapter, int position) {
int itemViewType = adapter.getItemViewType(position);
return adapter.onCreateViewHolder(
new FrameLayout(RuntimeEnvironment.application),
itemViewType
final EpoxyViewHolder viewHolder = spy(
adapter.onCreateViewHolder(
new FrameLayout(RuntimeEnvironment.application),
adapter.getItemViewType(position)
)
);

// The simplest way to inject the position for testing.
when(viewHolder.getAdapterPosition()).thenReturn(position);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I feel this isn't ideal, I found it to be the least obtrusive. Would love feedback if anyone has a more proper idea on how to fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable, thanks for fixing the tests too 👍

return viewHolder;
}

void recycleLastBoundModel(EpoxyController controller) {
Expand Down