You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView子项中ScrollView无法滚动且MaterialCardView点击失效求助

解决RecyclerView列表项中MaterialCardView嵌套ScrollView的滚动与点击问题

问题根源

  1. ScrollView无法滚动:当前ScrollView高度设为wrap_content,和底部渐变View同级,导致ScrollView没有被限制在MaterialCardView的130dp高度内,无法形成可滚动空间;而且渐变View会覆盖ScrollView底部,进一步压缩滚动区域。
  2. MaterialCardView失去点击:ScrollView作为可滚动控件,会优先拦截触摸事件,外层CardView拿不到点击信号。

解决方案

1. 调整布局结构与ScrollView参数

把ScrollView高度改成match_parent,给它加底部内边距预留渐变View的高度,同时把渐变View移到ScrollView内部的ConstraintLayout里,避免层级覆盖。另外给ScrollView加属性阻止它拦截外层点击事件。

2. 确保CardView可点击状态

给MaterialCardView添加clickable和focusable属性,保证它能响应点击。

修改后的完整布局代码

<com.google.android.material.card.MaterialCardView 
    android:layout_width="match_parent"
    android:layout_height="130dp"
    app:cardCornerRadius="12dp"
    app:strokeColor="@color/primary_text_color_50"
    app:strokeWidth="0.5dp"
    android:clickable="true"
    android:focusable="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="30dp"
        android:descendantFocusability="blocksDescendants">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clContract"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/ivCompleted"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="8dp"
                android:src="@drawable/ic_completed"
                android:visibility="visible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tvContract"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="12dp"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="8dp"
                android:fontFamily="@font/gotham_rounded_bold"
                android:textColor="@color/primary_text_color"
                android:textSize="@dimen/text_12"
                app:layout_constraintEnd_toStartOf="@+id/ivCompleted"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Agreement" />

            <!-- 把渐变View移到内部,避免遮挡ScrollView -->
            <View
                android:id="@+id/viewGradient"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:background="@drawable/shape_gradient_bottom_separator"
                app:layout_constraintBottom_toBottomOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</com.google.android.material.card.MaterialCardView>

额外说明

  • android:descendantFocusability="blocksDescendants":让ScrollView不抢外层CardView的点击事件,同时不影响自身滚动。
  • android:paddingBottom="30dp":给渐变View留位置,确保长文本滚动时不会被遮挡。
  • 如果用代码设置CardView点击,还可以通过触摸事件优化:
scrollView.setOnTouchListener { v, event ->
    when (event.action) {
        MotionEvent.ACTION_DOWN -> cardView.requestDisallowInterceptTouchEvent(false)
        MotionEvent.ACTION_MOVE -> cardView.requestDisallowInterceptTouchEvent(true)
    }
    v.onTouchEvent(event)
}

内容的提问来源于stack exchange,提问作者esra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 02:05:23