android - 滚动 Recyclerview 时如何防止项目触摸?
问题描述
我想将 touchlistner 添加到 Recyclerview 的项目中。主要是想要获取ACTION_UP
和ACTION_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;
}
});
解决方案
我使用可运行的处理程序解决了这个问题。
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;
}
});
推荐阅读
- python - Numpy 形状相同,均值返回不同形状
- python - Airflow DAGs 文件目录树没有更新?
- c# - mqttnet 客户端没有得到订阅的主题
- c - 如何让我的程序提示用户输入并使用函数将其存储到数组中?
- c# - 如何从 .NET Web 应用程序将日历事件发布到 Google 日历
- php - 使用 php 在谷歌日历中插入事件的时间格式问题
- mysql - MySQL:InnoDB Buffer 使用量继续增长
- c# - 如何从客户端获取当前日期时间?
- java - Kubernetes Java 客户端 API(CopyfileToPod)
- r - 将 Json 文件导入 R 并出现 unicode 错误