滚动Recyclerview时如何防止物品接触



我想将touchlistner添加到Recyclerview的Items。我想为Recyclerview的相关项获取ACTION_UPACTION_DOWN。我添加了OnItemTouchListner,并在OnBindViewHolder中尝试了OnTouchLister。但当我滚动回收视图时,项目的Touchlistner被调用了。我还尝试将手势列表添加到项目中。但是使用手势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;
}
});

单独使用Recyclerview滚动监听器。对于项目,请改用onClickListener

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

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

最新更新