7

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?

DariusL
  • 4,007
  • 5
  • 32
  • 44
  • I've solved this problem the same way you have, however it seems as though the dispatched touch events don't always seem to work. It behaves as if there is a touch slop of 0. Have you experienced this, or does anyone know of a solution? – JVillella Mar 27 '16 at 23:12

3 Answers3

4

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.

DariusL
  • 4,007
  • 5
  • 32
  • 44
1

With the answered solution it seems as though the dispatched touch events don't always seem to work. It behaves as if there is a touch slop of 0. I think what's happening is that the RecyclerView is calling requestDisallowInterceptTouchEvent(true); causing intermittent behaviour when there is an ACTION_MOVE event fired.

Instead I got better results with the following:

recycler.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return background.dispatchTouchEvent(event);
    }
});
JVillella
  • 1,029
  • 1
  • 11
  • 21
-1

Here are some of the best solutions of RecyclerView Item Click

RecyclerView rvQueues = (RecyclerView) findViewById(R.id.rvQueues);
`rvQueues.addOnItemTouchListener(
                new RecyclerItemClickListener(mActivity, rvQueues, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        //TODO do your stuff

                    }

                    @Override
                    public void onLongItemClick(View view, int position) {
                         //TODO do your stuff
                    }
                })
        );

import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View;
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener itemClickListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onLongItemClick(View view, int position);
    }

    private GestureDetector gestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        itemClickListener = listener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && itemClickListener != null) {
                    itemClickListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View mChildView = view.findChildViewUnder(e.getX(), e.getY());
        if (mChildView != null && itemClickListener != null && gestureDetector.onTouchEvent(e)) {
            itemClickListener.onItemClick(mChildView, view.getChildAdapterPosition(mChildView));
            return true;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}
Patrick R
  • 6,621
  • 1
  • 24
  • 27