我有RecyclerView
aCardView
作为行项目布局的根元素。如此处所述,我设法对CardView
使用下面的列表项布局产生了连锁反应。
行项目布局:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_8dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@color/cyan"
app:cardCornerRadius="20dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:padding="@dimen/margin_8dp">
<!-- Underlying views-->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
问题在于,要对RecyclerView
适配器中的行项目进行点击操作,需要点击几次,第一次点击用于选择项目并显示涟漪效果,第二次点击用于触发实际点击View.OnClickListener
。
使用android:focusableInTouchMode = "true"
属性 inCardView
是双击的原因;但是禁用它,也会因此禁用涟漪效应。
我尝试使用此答案中的自定义波纹效果,但不起作用。还试图像在这个答案CardView
中那样对底层根元素产生连锁反应,但仍然没有什么新鲜事。
与此处使用按钮样式类似。
我RecyclerView
在视图持有者的适配器中有一个跨国 itemView 点击监听器
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(@NonNull final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}