首页 > 解决方案 > 滚动 Recyclerview 时如何防止项目触摸?

问题描述

我想将 touchlistner 添加到 Recyclerview 的项目中。主要是想要获取ACTION_UPACTION_DOWN用于 Recyclerview 的 perticuler 项目。我添加了 OnItemTouchListner 并在 OnBindViewHolder 中尝试了 OnTouchListner。但是当我滚动 Recyclerview 时,Item 的 Touchlistner 被调用了。我也尝试将 Gesture Listner 添加到项目中。但是使用手势侦听器没有任何反应。这里有一些触摸监听器代码,

holder.txtView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch(motionEvent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    Log.d("LogTest11","Action_DOWN");
                    break;

                case MotionEvent.ACTION_MOVE:
                    Log.d("LogTest11","Action_MOVE");
                    break;

                case MotionEvent.ACTION_UP:
                    Log.d("LogTest11","ACTION_UP");
                    break;
            }
            return false;
        }
    });

标签: androidandroid-recyclerview

解决方案


我使用可运行的处理程序解决了这个问题。

 Handler handler =  new Handler();

 holder.txtView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch(motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.d("PrintingLogs","ACTION_DOWN");
                        handler.postDelayed(runnable,500);
                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        Log.d("PrintingLogs","ACTION_MOVE");
//                        handler.removeCallbacks(runnable);
//                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.d("PrintingLogs","ACTION_CANCEL");
                        handler.removeCallbacks(runnable);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("PrintingLogs","ACTION_UP");
                        handler.removeCallbacks(runnable);
                        break;
                }
                return true;
            }
        });

推荐阅读