Detect click on RecyclerView outside of items

前端 未结 6 2422
醉话见心
醉话见心 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 22:47

    You just need to set a TouchListener on the RecyclerView like shown above :

    categoryTable.setAdapter(new CatgoriesAdapter(categories.getWrappedList()));
        categoryTable.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN
                        && categoryTable.findChildViewUnder(event.getX(), event.getY()) == null)
                {
                    // Touch outside items here, you do whatever you want  
                    HideCategoryMenu();
                }
                return false;
            }
        });
    

提交回复
热议问题