Как сделать чтобы ReclerView стал не кликабельным
У меня есть CardView в котором LinearLayoutиRecyclerView, при нажатии на CardView работает setOnClickListener, но если я нажимаю в область RecyclerView,setOnClickListener не работает. Как сделать так чтобы RecyclerViewне мешал нажатию CardView?
<androidx.cardview.widget.CardView
android:id="@+id/card_h_faq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="6dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_faq"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:orientation="horizontal"
android:paddingStart="6dp"
android:paddingLeft="6dp">
<ProgressBar
android:id="@+id/prgs_2"
android:layout_width="28dp"
android:layout_height="match_parent"
android:indeterminateTint="@color/color_11"
android:paddingEnd="6dp"
android:paddingRight="6dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="@font/iiiii"
android:paddingVertical="10dp"
android:text="Часто задаваемые вопросы"
android:textColor="@drawable/color_profile"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/prgs_2" />
<ImageView
android:id="@+id/imageView"
android:layout_width="28dp"
android:layout_height="match_parent"
android:padding="1dp"
android:src="@drawable/arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@drawable/color_profile" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Rec_H_FAQ"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/ll_faq"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp" />
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/ll_faq"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:background="@drawable/bg_overlay"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
card_h_faq = requireView().findViewById(R.id.card_h_faq);
card_h_faq.setOnClickListener(v -> {
FragmentFAQ fragment = new FragmentFAQ(); // you fragment
FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
fragmentTransaction.addToBackStack("Profile");
fragmentTransaction.commit();
});
Ответы (1 шт):
Автор решения: Вася Воронцов
→ Ссылка
Думаю, можно немного исправить androidx.cardview.widget.CardView. Создадим собственный класс, наследуемый от него:
package com.example.app;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.cardview.widget.CardView;
public class CustomCardView extends CardView {
public CustomCardView(Context context) {
super(context);
};
public CustomCardView(Context context, AttributeSet attrs) {
super(context, attrs);
};
public CustomCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
};
@Override
public boolean onInterceptTouchEvent(MotionEvent me) {
return true;
};
}
Тогда в разметке используйте его так:
<com.example.app.CustomCardView
...>
...
</com.example.app.CustomCardView>
Больше об onInterceptTouchEvent здесь.