El evento de clic de los padres no se activa cuando se hace clic en recyclerview

Tengo un RecyclerView que está en un CardView que tiene un par TextViews. El CardView tiene un conjunto OnClickListener y se dispara al hacer clic en el TextViews, pero no se dispara al hacer clic en el RecyclerView.

Así es como se ve el CardView:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" card_view:cardCornerRadius="4dp" card_view:cardElevation="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="100" android:minWidth="100dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/text1" android:textColor="@color/abc_primary_text_material_light" android:layout_weight="1" android:layout_gravity="center_horizontal" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:listSelector="@color/highlighted_text_material_light" android:layout_weight="98" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/black" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeSummary" android:orientation="horizontal" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/text2" android:textAlignment="viewEnd" android:textColor="@color/abc_secondary_text_material_light" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="start" android:singleLine="true" android:layout_alignParentLeft="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/text3" android:textColor="@color/abc_primary_text_material_light" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:gravity="end" android:singleLine="true" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/text2" /> </RelativeLayout> </LinearLayout> </android.support.v7.widget.CardView> 

No necesito un oyente de clics en este RecyclerView y realmente sólo necesito que el evento de clic de la vista padre se dispare cuando se hace clic en el RecyclerView (Lo mismo ocurre con el evento OnLongClick). También necesito el RecyclerView para desplazarse. ¿Es el RecyclerView algo como comer el evento de clic y no pasarlo a los padres?

Hay una mejor solución. Es decir, subclase su CardView :

 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; } } 
 recyclerView.setLayoutFrozen(true); 

Sólo setLayoutFrozen true después de setAdapter para recyclerView

Me di cuenta de cómo obtener el evento de clic para el padre de RecyclerView. Mi solución se siente como un hack, así que espero que alguien pueda encontrar una solución mejor.

En el RecylerView.Adapter:

 @Override public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.my_item_layout, viewGroup, false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewGroup.callOnClick(); } }); viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return viewGroup.performLongClick(); } }); return viewHolder; } 

Luego tuve que conectar el evento de clic en el RecyclerView:

 RecyclerView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { parentView.callOnClick(); } }); RecyclerView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return parentView.performLongClick(); } }); 

@ywwynm, estás en el camino correcto, excepto que la solución no permite que el RecyclerView anidado se desplace. Lo combiné con la solución aquí y surgió con esta solución para manejar los eventos click y onLongClick, así como para permitir el desplazamiento.

 public class InterceptTouchCardView extends CardView { private GestureDetector mGestureDetector; private boolean mLongClicked; public InterceptTouchCardView(Context context) { super(context); Initialize(); } public InterceptTouchCardView(Context context, AttributeSet attrs) { super(context, attrs); Initialize(); } public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Initialize(); } private void Initialize() { mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { public boolean onDown(MotionEvent e) { mLongClicked = false; return true; } public void onLongPress(MotionEvent e) { mLongClicked = true; performLongClick(); } }); } /** * 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. * * We can't Intercept the touch event if we want to allow scrolling since ACTION_DOWN always * happens before ACTION_MOVE. So handle touch events here since onTouchEvent won't be triggered. */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { mGestureDetector.onTouchEvent(ev); if (ev.getAction() == MotionEvent.ACTION_UP && !mLongClicked) this.callOnClick(); return false; } } 
  • ¿Recyclerview hace que el último elemento agregado sea el elemento seleccionado?
  • Android TabLayout dentro de Fragmento
  • RecyclerView.Adapter.notifyItemChanged () nunca pasa la carga útil a onBindViewHolder ()
  • Mantenga la posición Arrastrar y colocar objetos cuando reinicie
  • Problema de altura de RecyclerView en API 17 y API 16 - Android
  • RecyclerView: Cómo borrar las vistas recicladas de RecyclerView.RecycledViewPool
  • ¿Cómo sacar una vista de niño de RecyclerView?
  • RecyclerView expandible cardView
  • RecyclerView no se desplaza hacia abajo
  • RecyclerView ItemTouchHelper Action_Drag_Ended?
  • Cómo obtener Adview debajo de Viewpager en CoordinatorLayout
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.