我正在尝试处理 Recycler View 中的触摸事件:
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private ClickListener clickListener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null)
{
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
Log.i("TAG", "Radhe handling LongPress ");
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e));
{
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public static interface ClickListener{
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
我还有以下代码-:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
filterAdapter = new FilterAdapter(getActivity(), getData());
recyclerView.setAdapter(filterAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
Log.i("TAG", "Radhe child Clicked ");
Toast.makeText(getActivity(),"CLick",Toast.LENGTH_SHORT).show();
}
@Override
public void onLongClick(View view, int position) {
Log.i("TAG", "Radhe child Long Clicked ");
Toast.makeText(getActivity(),"Long CLick",Toast.LENGTH_SHORT).show();
}
}));
return layout;
}
现在当我运行它时 -: 和
有人能告诉我为什么会这样吗?我不知道它的原因。提前致谢。
最佳答案
当您从 onInterceptTouchEvent
调用 onClick
时,您需要返回 true
以显示事件已被消耗,而不是继续为它触发事件.
addOnItemTouchListener
的 Javadoc 说:
Once a listener returns true from RecyclerView.OnItemTouchListener.onInterceptTouchEvent(RecyclerView, MotionEvent) its RecyclerView.OnItemTouchListener.onTouchEvent(RecyclerView, MotionEvent) method will be called for each incoming MotionEvent until the end of the gesture.
onInterceptTouchEvent
中的 false 表示
continue with the current behavior and continue observing future events in the gesture.
只是为了在您现有的代码中澄清(但是我不确定您在 if 语句中使用 gestureDetector.onTouchEvent(e) 尝试实现什么,请确保您也没有意外地使用滚动事件):
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
return true;
}
return false;
}
关于android - 在 Recyclerview 中多次调用 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510215/