Kine
Kine

Reputation: 41

Prevent a RecyclerView inside CardView from intercepting touch

I have a RecyclerView inside a MaterialCardView and this card has a bunch of others views.

The RecyclerView items are not clickable, the only purpose is to show some data from a list. When i click the MaterialCardView, i must navigate to another screen. If i touch anywhere outside from the RecyclerView area, the Ripple Effect occurs and i navigate to the other screen.However, if i click on the RecyclerView, nothing happens.

I suspect the view is intercepting the touch events, preventing the card from consuming the click, but couldn't find a way to make it work.

Relative to the RecyclerView i've tried to:

recyclerView.setOnCLickListener(v -> card.callOnClick())
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                if (e.getAction() == MotionEvent.ACTION_DOWN) {
                    card.callOnClick();
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });

If i call 'card.callOnClick()' or 'card.performClick()' inside the 'onItemTouchListener' with it works, but the Ripple Effect does not happen, it does not seem like a natural click.

I want the RecyclerView to not intercept click/touch and let the parent card handle these events.

Upvotes: 2

Views: 278

Answers (1)

Kine
Kine

Reputation: 41

I managed to achiev the desired behaviour by using recyclerView.setLayoutFrozen(true) after updating the adapter data.

Upvotes: 1

Related Questions