RecyclerView 中的 OnLongItemClick
Posted
技术标签:
【中文标题】RecyclerView 中的 OnLongItemClick【英文标题】:OnLongItemClick in RecyclerView 【发布时间】:2015-03-12 18:18:26 【问题描述】:我开始在 android 中使用 RecyclerView。在我为我的适配器实现触摸侦听器之前,一切正常。
来自这个话题:
https://***.com/a/26826692/2923403
我可以正确实现子视图的监听器。但是,我想同时实现 OnItemClick 和 OnItemLongClick,然后出现问题。他们总是同时被解雇。首先是 OnItemClick,然后是 OnItemLongClick,这使我的函数无用。
这是我的自定义监听器代码(基于上面的代码,感谢 Fouad):
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
private OnItemClickListener mListener;
public static interface OnItemClickListener
public boolean onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
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 true;
return false;
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent)
对于实施:
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener()
@Override
public boolean onItemClick(View view, int position)
//This is fired 1st
@Override
public void onItemLongClick(View view, int position)
//This comes later
));
你有什么解决办法吗?
【问题讨论】:
【参考方案1】:您可以在自定义适配器实现中添加侦听器。它会是这样的:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
public interface OnItemClickListener
public void onItemClicked(int position);
public interface OnItemLongClickListener
public boolean onItemLongClicked(int position);
private Fragment mFragment;
public static class ViewHolder extends RecyclerView.ViewHolder
public View v;
public ViewHolder(View v)
super(v);
this.v = v;
public RecyclerViewAdapter(Fragment fragment)
mFragment = fragment;
@Override
public void onBindViewHolder(ViewHolder holder, final int position)
holder.v.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mFragment.onItemClicked(position);
);
holder.v.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View v)
mFragment.onItemLongClicked(position);
return true;
);
. . .
注意开头的接口定义。这样,您将 onClick 和 onLongClick 事件传递给您的片段进行处理。很方便,你知道的
【讨论】:
嗨。在我的情况下,看起来这两个事件仍然同时触发 您是否从回收视图中删除(禁用)onTouch 侦听器?你不需要它们。只需在您的活动或片段中实现RecyclerViewAdapter.OnItemClickListener
和RecyclerViewAdapter.OnItemLongClickListener
,并覆盖各自的接口方法以上是关于RecyclerView 中的 OnLongItemClick的主要内容,如果未能解决你的问题,请参考以下文章
在recyclerview的嵌套recyclerview中的嵌套列表中添加项目
RecyclerView | 处理 RecyclerView 中的点击事件
AndroidStudio-添加RecyclerView包 AndroidStudio添加v7包中的RecyclerView