fix the bug that apdapter delay to call setOnItemClickListener() that…#2708
Open
w-g-b wants to merge 4 commits intoCymChad:masterfrom
Open
fix the bug that apdapter delay to call setOnItemClickListener() that…#2708w-g-b wants to merge 4 commits intoCymChad:masterfrom
w-g-b wants to merge 4 commits intoCymChad:masterfrom
Conversation
… didn't take effect
Collaborator
|
this idea is good ,but in some case , someone wan't use setOnItemClickListener , they want to custom Touch event , we setOnClickListener on any case ,that is Incompatible,what should we do ? |
Author
|
Yes, you are right. Developer prefers to use addOnClickListener() through BaseViewHolder. private List<K> mViewHolders;
public BaseQuickAdapter(@LayoutRes int layoutResId, @Nullable List<T> data) {
this.mData = data == null ? new ArrayList<T>() : data;
if (layoutResId != 0) {
this.mLayoutResId = layoutResId;
}
mViewHolders = new ArrayList<>();
}
@Override
public K onCreateViewHolder(ViewGroup parent, int viewType) {
...
default:
baseViewHolder = onCreateDefViewHolder(parent, viewType);
bindViewClickListener(baseViewHolder);
mViewHolders.add(baseViewHolder);
...
}
public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
mOnItemClickListener = listener;
for (BaseViewHolder b : mViewHolders) {
bindViewClickListener(b);
}
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
for (BaseViewHolder viewHolder : mViewHolders) {
bindViewClickListener(viewHolder);
}
}Could it feasible? |
Author
|
Add the judge to avoid null pointer exception private void bindViewClickListener(final BaseViewHolder baseViewHolder) {
...
if (getOnItemClickListener() != null) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getOnItemClickListener() != null) {
setOnItemClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
}
}
});
}
if (getOnItemLongClickListener() != null) {
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (getOnItemLongClickListener() != null)
return setOnItemLongClick(v, baseViewHolder.getLayoutPosition() - getHeaderLayoutCount());
return false;
}
});
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some codes like this:
In this codes, the item click event will failure because item view didn't execute the setOnClickListener() function.
The codes are no meaning. But the bug will appear if the recycleview have loaded end that adpater call setOnItemClickListener().
I modify the logic of bindViewClickListener() function to solve this problem.