8

看起来像将 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 中的这种预期行为或错误是否导致它仍然是预览版?

PS。我希望根据文档可以点击它以对按下状态做出反应并对点击产生连锁反应。当设置为 false 时,ACTION_DOWN 工作正常,但未触发按下状态,并且 selectableBackground 没有任何效果。

4

1 回答 1

0

这是预期的行为而不是错误。

当设置 item clickable 为 true 时,ACTION_DOWN 将被消耗,recycler view 永远不会得到 ACTION_DOWN。

为什么在回收站视图的 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,请尝试使其处于活动状态,例如:

 public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN)
    lastY = ev.getRawY();
    return super.dispatchTouchEvent(ev);
于 2016-05-10T09:50:44.203 回答