Detect click on RecyclerView outside of items

前端 未结 6 2424
醉话见心
醉话见心 2021-02-18 22:10

I have a RecyclerView with 2 items that don\'t fill the whole screen. How can I detect that the user clicked on the empty part of the RecyclerView (meaning clicked directly on t

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-18 23:02

    As mentioned in the comment

    mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    
      @Override
      public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        if (motionEvent.getAction() != MotionEvent.ACTION_UP) {
            return false;
        }
        View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
        if (child != null) {
          // tapped on child
          return false;
        } else {
          // Tap occured outside all child-views.
          // do something
          return true;
        }
      }
    
      @Override
      public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
      }
    });
    

提交回复
热议问题