androidandroid-layoutandroid-recyclerviewandroid-viewgroup

Make recycler view clickable inside a card view


  <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_subscribe_card"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:onClick="@{vm.onGoalPress}"
    card_view:cardCornerRadius="4dp">

<android.support.v7.widget.RecyclerView
                android:id="@+id/feeds_list"
                android:layout_width="200dp"
                android:layout_height="30dp"
                android:layout_marginTop="6dp" />

</CardView>

The card view on Click is working .But the region of recycler view is not clickable. How can it be made clickable so that the event of card view is captured.


Solution

  • public class InterceptTouchCardView extends CardView {
    
    public InterceptTouchCardView(Context context) {
        super(context);
    }
    
    public InterceptTouchCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    /**
     * Intercept touch event so that inner views cannot receive it.
     * 
     * If a ViewGroup contains a RecyclerView and has an OnTouchListener or something like that,
     * touch events will be directly delivered to inner RecyclerView and handled by it. As a result, 
     * parent ViewGroup won't receive the touch event any longer.
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
    

    }