当前位置:  开发笔记 > 编程语言 > 正文

在Recyler视图中多次调用onClick

如何解决《在Recyler视图中多次调用onClick》经验,为你挑选了1个好方法。

我想要处理回收器视图中的事件 - :

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;
    }

现在,当我运行它时: - 和

这不适用于多次触摸. 我只点击一次. 只有一次.

有人能说出为什么会这样吗?我不知道它的原因.提前致谢.



1> Nick Cardoso..:

当你打电话onClickonInterceptTouchEvent需要return true显示已经消耗的事件,而不是继续为它发起事件.

Javadoc addOnItemTouchListener说:

一旦侦听器从RecyclerView.OnItemTouchListener.onInterceptTouchEvent(RecyclerView,MotionEvent)返回true,将为每个传入的MotionEvent调用其RecyclerView.OnItemTouchListener.onTouchEvent(RecyclerView,MotionEvent)方法,直到手势结束.

来自onInterceptTouchEvent手段的虚假

继续当前行为并继续观察手势中的未来事件.

只是为了澄清你现有的代码(但是我不确定你试图用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;
}

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有