看起来像是将 RecyclerView 的项目布局设置为 clickable="true",完全消耗一些触摸事件,特别是 MotionEvent.ACTION_DOWN
(之后的 ACTION_MOVE 和 ACTION_UP 起作用):
项目.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/demo_item_container"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"> <-- this what breaks touch event ACTION_DOWN
....
</LinearLayout>
在 onCreate() 中进行非常基本的 RecyclerView 设置:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
... //Standard recyclerView init stuff
//Please note that this is NOT recyclerView.addOnItemTouchListener()
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d("", "TOUCH --- " + motionEvent.getActionMasked());
//Will never get here ACTION_DOWN when item set to android:clickable="true"
return false;
}
});
这是 RecyclerView 中的预期行为还是错误,因为它仍然是预览版?
附言。我希望它可以根据文档进行点击以对按下状态使用react并对点击产生链式 react 。当设置为 false 时,ACTION_DOWN 工作正常但未触发按下状态并且 selectableBackground 没有任何效果。
最佳答案
这是预期的行为,不是错误。
当设置item clickable为true时,ACTION_DOWN会被消耗,recycler View 永远不会得到 ACTION_DOWN。
为什么在回收站 View 的 onTouch() 中需要 ACTION_DOWN?有必要吗? 如果你想在 ACTION_DOWN 中设置 lastY,为什么不这样
case MotionEvent.ACTION_MOVE:
if (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
// initial
if (lastY == -1)
lastY = y;
float dy = y - lastY;
// use dy to do your work
lastY = y;
break;
case:MotionEvent.ACTION_UP:
// reset
lastY = -1;
break;
你愿意吗?如果您仍然想要 ACTION_DOWN,请尝试使其处于 Activity 状态,例如:
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN)
lastY = ev.getRawY();
return super.dispatchTouchEvent(ev);
关于Android:当 RecyclerView 项目设置为可点击时会阻止 onTouch 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570669/