android - 在 Recyclerview 中多次调用 onClick

标签 android android-recyclerview

我正在尝试处理 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;
    }

现在当我运行它时 -: 和

This is not for multiple touches. I clicked only once. only once.

有人能告诉我为什么会这样吗?我不知道它的原因。提前致谢。

最佳答案

当您从 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/

相关文章:

java - 在 Eclipse-ADT 的路径中找不到程序 “make”

android - 在 android studio 上编译 android/phonegap 应用程序时出错

java - 无法将 map 加载到 Android(三星)手机中

Android:在 Adapter 的 onBindViewHolder 中将 ImageView 动态添加到 Linearlayout

java - 更改值 recyclerview.adapter 并将其保存到 mainactivity 中的 textview 中?

java - 如何使用不在 "Query-Collection"中的值对 Firestore RecyclerView 项目进行排序? (社交排行榜)

android - 如何在用户键入时用不同的注释替换一个注释(将 “space” 替换为 “-”

android - 当我的音频持续时间小于视频视频持续时间时如何循环音轨ffmpeg

android - RecyclerView 不是真正的 wrap_content 在 ScrollView

android - 无法滚动 RecyclerView,当尝试在滚动时隐藏工具栏