android-在StaggeredGrid RecyclerView中的行内单击按钮会触发整个项目单击
作者:互联网
嗨,我在RecyclerView行(用于书签)中有一个按钮.当我单击按钮时,它会触发该按钮单击,还会触发整个行的itemclick.我有单独的代码来处理按钮和整个项目的单击.为什么该行中的buttonclick触发整个项目单击?
如何解决此问题?
这是我在适配器内单击按钮的代码:
viewHolder.imgBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
这是我的RecyclerView项目点击代码:
mGridView.addOnItemTouchListener(new RecyclerItemClickListener(
getActivity(), mGridView,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
...
}
@Override
public void onItemLongClick(View view, int position) {
...
}
}));
RecyclerItemClickListener.java
public class RecyclerItemClickListener implements
RecyclerView.OnItemTouchListener {
public static interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context,
final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(
e.getX(), e.getY());
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView,
recyclerView.getChildPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null
&& mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}
解决方法:
为列表项添加触摸侦听器并使用ClickListeners不是一个好主意.
这是设置RecyclerView的itemClick侦听器的小示例
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public IMyViewHolderClicks mListener;
public ViewHolder(View itemLayoutView, IMyViewHolderClicks listener) {
super(itemLayoutView);
mListener = listener;
itemLayoutView.setOnClickListener(this);
}
}
更新 :
在列表项中添加按钮的侦听器.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Set the data for the views.
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
标签:onitemclicklistener,recycler-adapter,android-recyclerview,android 来源: https://codeday.me/bug/20191120/2041917.html