Click Through RecyclerView Blank Space

I have a layout where the RecyclerView is on top of another layout with a few buttons. The first recycler element is a header with a large top to create an empty space above it. Now I want the clicks to work through this open space, scrolling should also scroll through the handler. 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> 

A recycler consumes all clicks using only the top or margin. I need clicks to go through, but the scroll should remain in the handler to scroll.

Edit:

My clicks work, 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 translated the background (parallax), clicks do not get to the correct positions. Should I also translate events?

+5
source share
3 answers

Ok, I solved both problems.

 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 event of touching the recyclers and "fixes" its position. The translation is Y translation for the background.

+3
source

In the answer, it seems that the sent touch events do not always work. It behaves as if there is a sensory loop 0. I think that it happens that the RecyclerView calls requestDisallowInterceptTouchEvent(true); causing intermittent behavior when the ACTION_MOVE event ACTION_MOVE .

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); } }); 
0
source

Here are some of the best RecyclerView element solutions. 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) { } } 
0
source

All Articles