Click through a RecyclerView list item
Asked Answered
R

1

9

I have a RecyclerView with a LinearLayoutManager and an Adapter:

@Override public int getItemViewType(int position) {
    return position == 0? R.layout.header : R.layout.item;
}

Here's header.xml:

<View xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/header"
      android:layout_width="match_parent"
      android:layout_height="@dimen/header_height"
    />

What I want to achieve is to have a hole in the RecyclerView where I can click through to anything behind the RecyclerView. I tried a lot of combinations the following attributes to no avail:

      android:background="@android:color/transparent"
      android:background="@null"
      android:clickable="false"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:longClickable="false"

How could I make the first item transparent (allowing touch events to anything behind it) in the list, but let it still occupy the space?

Recall answered 15/3, 2015 at 21:50 Comment(0)
E
9

You could set the OnClickListener/OnTouchListener of all of your "holes" to the parent of the view behind the RecyclerView and delegate any of the MotionEvent and touch events to that parent ViewGroup's touch handling.

Update by TWiStErRob:

class HeaderViewHolder extends RecyclerView.ViewHolder {
    public HeaderViewHolder(View view) {
        super(view);
        view.setOnTouchListener(new OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
                // https://mcmap.net/q/1316800/-is-it-possible-to-add-a-scrollable-textview-to-a-listview
                v.getParent().requestDisallowInterceptTouchEvent(true); // needed for complex gestures
                // simple tap works without the above line as well
                return behindView.dispatchTouchEvent(event); // onTouchEvent won't work
            }
        });

There's nothing special needed in the XML, just plain views (none of the attributes in the question). v.getParent() is the RecyclerView and that long method stops it from starting a scroll gesture while holding your finger on the "hole".

My "hole" view in the list also has a semi-transparent background, but that doesn't matter because we're hand-delivering the touches.

Empathize answered 15/3, 2015 at 21:58 Comment(2)
Hmm, interesting :) I want complex gestures to work (e.g. swipe), so only the touch listener is worth trying.Recall
Also, you could allow the RecyclerView to identify any holes for any of the touch handling you would like to handle, then delegate to the back if the events are holy ;)Empathize

© 2022 - 2024 — McMap. All rights reserved.