Clicking Unused Space in RecyclerView

Nathan Sokalski 4,126 Reputation points
2022-01-16T20:44:09.04+00:00

I have a RecyclerView that does not always have enough items to fill the screen (it has varying numbers of items as is usually the case). Therefore, part of the RecyclerView will be transparent, which is what I want. However, I want to detect when the user clicks or touches this empty space (but not when they click or touch the items, that is handled by the individual items). The Click event does not seem to do anything, and the Touch event seems to interfere with the code used when dragging the items (I allow the user to reorder the items by dragging). Is there any good way to detect when the user clicks or touches the unused space without interfering with the items?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nathan Sokalski 4,126 Reputation points
    2022-01-24T16:33:17.79+00:00

    I finally found a reasonably simple way that uses InterceptTouchEvent & FindChildViewUnder by doing the following:

    private void rvPlayerNamesInput_InterceptTouchEvent(object sender, InterceptTouchEventEventArgs e)
    {
        if (e.Event.Action == MotionEventActions.Down && !(e.RecyclerView.FindChildViewUnder(e.Event.GetX(), e.Event.GetY()) is GridLayout))
        {
            //My Code Here
            e.Handled = true;
        }
        else { e.Handled = false; }
    }
    

    Other people may need to add a few more conditions & tweaks depending on their layouts, but I'm guessing that the modifications should be reasonably simple. I'll admit that this is my first time using FindChildViewUnder, GetX & GetY, so maybe there are scenarios I haven't found yet that could cause problems (there always are, right?), but this seems good for now.

    0 comments No comments