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

Xamarin Android中ScrollView选中/输入Entry时如何禁用自动滚动

问题描述

我是Xamarin开发新手,暂未找到对应问题的解决方案:我的应用主要在单个NavigationPage上承载展示,页面布局结构如下:

<ContentPage.ToolBarItems>
  *Tool Bar items*
</ContentPage.ToolBarItems>
<ScrollView>
  <Grid>
    <Entry />
    <Label />
    <BoxView />
    <!-- 其余更多业务控件 -->
  </Grid>
</ScrollView>

遇到的问题:每当选中Entry控件进行输入操作时,ScrollView会触发默认自动滚动行为,将被选中的Entry定位到视图中心位置,需要禁用该自动滚动效果。

Android平台解决方案

通过自定义ScrollView渲染器即可实现禁用效果,核心逻辑如下:

  • 为ScrollView创建Android平台自定义渲染器
  • 在渲染器中重写OnDescendantInvalidated方法,注释掉基类的默认实现,阻断默认滚动触发逻辑
  • 在渲染器中重写RequestChildRectangleOnScreen方法,直接返回true,拦截子控件唤起的滚动到可视区域请求

具体实现代码:

public override void OnDescendantInvalidated(Android.Views.View child, Android.Views.View target)
{
    // 注释基类调用,禁用默认自动滚动触发逻辑
    // base.OnDescendantInvalidated(child, target);
}

public override bool RequestChildRectangleOnScreen(Android.Views.View child, Android.Graphics.Rect rectangle, bool immediate)
{
    // 直接返回true,拦截子控件请求滚动到屏幕可视区域的默认行为
    return true;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 12:54:32