maui: Scrolling of Editor placed in ScollView does not work.
Description
Provide a mechanism for users to control what control captures touch when the user is trying to scroll inside a nested control
Workaround
https://github.com/dotnet/maui/pull/24531#issuecomment-2374724124
Notes
If this issue is related to #9827, is there a specific solution? This Issue also occurred in Xamarin.Forms, but continues to occur in .NET MAUI.
Discussed in https://github.com/dotnet/maui/discussions/13632
<div type='discussions-op-text'>Originally posted by cat0363 March 2, 2023 To reproduce, layout as follows.
<ScrollView Orientation="Vertical">
<Grid RowDefinitions="100,100,100,100,*,100">
<Grid Grid.Row="0" BackgroundColor="Red" />
<Grid Grid.Row="1" BackgroundColor="Yellow" />
<Grid Grid.Row="2" BackgroundColor="Green" />
<Grid Grid.Row="3" BackgroundColor="Blue" />
<Editor Grid.Row="4" x:Name="txtTest" HeightRequest="150" WidthRequest="250"
VerticalOptions="Start" HorizontalOptions="Start"
Text="1
2
3
4
5
6
7
8
9
10" />
<Grid Grid.Row="5" BackgroundColor="Purple" />
</Grid>
</ScrollView>
Code: https://github.com/cat0363/Maui-Issue13634
Assume that the Editor has enough Text to scroll. If you scroll vertically on the editor after scrolling to the editor, scrolling on the editor side will not work. Similar to when the Map is placed inside a ScrollView (#13628), It seems that the tap event is being handled by the Editor’s parent control, ScrollView.
Since the build environment has not been prepared, it cannot be confirmed for iOS, but at least it is as described for Android.
The only solution I found is below.
public override bool DispatchTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down ||
e.Action == MotionEventActions.Move ||
e.Action == MotionEventActions.Up)
{
// Search EditText Control
var editors = (Window.DecorView as ViewGroup).GetChildrenOfType<EditText>();
foreach (var editor in editors)
{
// Initialize EditText screen position
int[] pos = new int[2];
// Set EditText screen position
editor.GetLocationOnScreen(pos);
// Create hit test rectangle
Rect hitRect = new Rect(pos[0], pos[1], editor.Width, editor.Height);
// Judge hit test
bool isHitTest = hitRect.Contains(e.GetX(), e.GetY());
// Hit test result is OK
if (isHitTest)
{
// Touch event intercept
editor.Parent.RequestDisallowInterceptTouchEvent(true);
}
}
}
return base.DispatchTouchEvent(e);
}
In the MainActivity.cs file, re-implement DispatchTouchEvent, get the EditText that exists in the page, execute the hit test, and if it is a EditText, intercept the touch event. Alternatively, you can reimplement the EditText’s TouchListener in your EditorHandler to intercept touch events.
Any other good ideas? I have not confirmed how it works on iOS, so I would like to know if there is no problem with iOS. Thank you.</div>
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 15 (7 by maintainers)
I’ve hit same issue. I went with the Handler based alternative workaround, which worked well:
Anywhere in /Platforms/Android folder:
On startup in MauiProgram.cs:
Thanks for sharing @cat0363
@LaraSQP all that is missing is the using statements:
Hope this helps.
I’ve added some notes here
https://github.com/dotnet/maui/pull/24531#issuecomment-2374724124
This isn’t inherently a bug
Ideally here we wouldn’t just change the behavior for Android users as that might frustrate some set of users. The approach here would be to add some mechanism to allow developers to control how touch behavior is captured when the user scrolls