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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With