0

我有RecyclerViewaCardView作为行项目布局的根元素。如此处所述,我设法对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) {

            }
        });
     }
}
4

2 回答 2

0

替换View.OnClickListener为, 当行项目在被点击后获得焦点时,我运行与回调View.OnFocusChangeListener中相同的代码。onClick

但这并没有让连锁反应有机会出现,所以延迟发布。

itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Do whatever done in onClick()
                }
            }, 10); // delay to give the ripple effect a chance to show
    }

});

我希望如果有另一个解决方案没有任何延迟。

于 2020-07-04T19:49:25.960 回答
0

我通过从 中完全删除android:focusableInTouchMode = "true"&并将涟漪效果添加到 CardView 中的根项来解决这个问题android:foregroundCardViewandroid:background="?android:attr/selectableItemBackground"

<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"
    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="?android:attr/selectableItemBackground"
        android:padding="@dimen/margin_8dp">

            <!-- Underlying views-->

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

于 2021-02-16T12:59:52.267 回答