嗨,我在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) { } }); }
总结 以上是内存溢出为你收集整理的android-在StaggeredGrid RecyclerView中的行内单击按钮会触发整个项目单击全部内容,希望文章能够帮你解决android-在StaggeredGrid RecyclerView中的行内单击按钮会触发整个项目单击所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)