Compose中AndroidView嵌套App Widget滚动失效问题及2025.08版本修复
编辑说明
该Bug已在Compose 2025.08版本中修复,AndroidView内的嵌套滚动现已可正常使用。
问题场景
在自定义Android启动器开发中,我将包含AppWidgetHostView的Compose AndroidView放入了可垂直滚动的Compose Column中。
代码示例
WidgetComposable被包含在带有verticalScroll()修饰符的Column内:
@Composable fun WidgetComposable( widget: Widget, widgetsViewModel: WidgetsViewModel ) { val context = LocalContext.current BoxWithConstraints( modifier = Modifier .fillMaxWidth() .height(250.dp) .scrollable( state = rememberScrollableState { it }, orientation = Vertical ) ) { AndroidView( modifier = Modifier.fillMaxSize(), factory = { widgetsViewModel.getViewForWidget(widget, context) .apply { ViewCompat.setNestedScrollingEnabled(this, true) } }, update = { widgetView -> widgetView.updateAppWidgetOptions(Bundle().apply { putInt(OPTION_APPWIDGET_MIN_WIDTH, maxWidth.int) putInt(OPTION_APPWIDGET_MIN_HEIGHT, maxHeight.int) putInt(OPTION_APPWIDGET_MAX_WIDTH, maxWidth.int) putInt(OPTION_APPWIDGET_MAX_HEIGHT, maxHeight.int) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { putParcelableArrayList( OPTION_APPWIDGET_SIZES, arrayListOf(SizeF(maxWidth.value, maxHeight.value)) ) } }) Log.i(TAG, "Updated widget size") } ) } } private val Dp.int get() = value.toInt()
问题现象
我按照官方文档中的第二个嵌套滚动配置示例进行设置,但滚动Widget时,Widget内容和Column都无法滚动。我已上传最小复现示例到代码仓库。
尝试的方案及缺点
我尝试了多种方案,目前找到的最优解是遍历AppWidgetHostView的视图层级,查找可滚动布局,若存在则使用自定义AppWidgetHostView子类,在onInterceptTouchEvent()中调用requestDisallowInterceptTouchEvent(),阻止Column拦截Widget上的触摸事件。但该方案存在以下缺点:
- 同时会拦截横向滚动事件
- 可能与Jetpack Glance不兼容
- 若未来Google允许更多类型的可滚动Widget视图,该方案会失效
内容的提问来源于stack exchange,提问作者TUX
相关产品推荐
相关产品推荐

