Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click through RecyclerView empty space

I have a layout where a RecyclerView is on top of another layout with a few buttons. The first recycler item is a header with a large top margin to create an empty space above it. Now I want clicks to work through that open space, swiping should also scroll the recycler. The views are in a simple frame.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <package.FeedBackgroundView
        android:id="@+id/feed_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"/>

</FrameLayout>

The recycler consumes all clicks with just top padding or margin. I need clicks to pass through, but swipes should stay in the recycler for scrolling.

Edit:

I have the clicks working, the solution was:

recycler.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            background.dispatchTouchEvent(event);
            return false;
        }
    });

Now I have a problem, since I translate the background (parallax) the clicks aren't arriving on the correct positions. Do I have to translate the events too?

like image 466
DariusL Avatar asked May 22 '15 11:05

DariusL


1 Answers

Okay, I solved both issues.

recycler.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            MotionEvent e = MotionEvent.obtain(event);
            Matrix m = new Matrix();
            m.setTranslate(0f, -backgroundTranslation);
            e.transform(m);
            background.dispatchTouchEvent(e);
            e.recycle();
            return false;
        }
    });

This duplicates the recycler touch event and "fixes" it's position. The translation is translationY for the background.

like image 194
DariusL Avatar answered Sep 23 '22 13:09

DariusL