This workaround works for Android API 21 and later. For lower API, see this.
Microsoft Xamarin.Forms Official Documentation says that ScrollViews should not be nested. In addition, ScrollViews should not be nested with other controls that provide scrolling, like ListView and WebView.
- <ScrollView Padding="20">
- <StackLayout>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Panda Family"></Label>
- <controls:NestedScroll x:Name="listview"
- MinimumHeightRequest="96"
- Margin="16,0">
- </controls:NestedScroll>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- <Label Text="Hello!!! Welcome to Smiley Panda"></Label>
- <Label Text="Hello!!! Smiley Panda Loves you"></Label>
- </StackLayout>
- </ScrollView>
Create NestedScroll class
- public class NestedScroll : Xamarin.Forms.ListView
- {
- public static readonly BindableProperty IsNestedScrollProperty = BindableProperty.Create(
- propertyName: "IsNestedScroll",
- returnType: typeof(bool),
- declaringType: typeof(NestedScroll),
- defaultValue: false
- );
- public bool IsNestedScroll
- {
- get
- {
- return (bool)GetValue(IsNestedScrollProperty);
- }
- set
- {
- SetValue(IsNestedScrollProperty, value);
- }
- }
- }
Add Namespace in Xaml
- xmlns:controls="clr-namespace:NestedScrollTest.CustomRenderer; NestedScrollTest"
Create CustomRenderer for Android
- public class NestedScrollDroid: ListViewRenderer
- {
- public NestedScrollDroid(Android.Content.Context context) : base(context)
- {
- }
- protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
- {
- base.OnElementChanged(e);
- if(e.NewElement != null)
- {
- var listview = this.Control as Android.Widget.ListView;
- listview.NestedScrollingEnabled = true;
- }
- }
- }
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- listview.ItemsSource = AddPandaFamily();
- }
- List<string> pandaFamily = new List<string>();
- public List<string> AddPandaFamily()
- {
- for(int i=0; i<10; i++)
- {
- pandaFamily.Add("Smiley Panda");
- pandaFamily.Add("Red Panda");
- pandaFamily.Add("Qinling Panda");
- pandaFamily.Add("Oleosa Panda");
- pandaFamily.Add("Ailuropoda Panda");
- }
- return pandaFamily;
- }
- }
Demo



admin userPosted Apr 16, 2020, 11:15 PM EDT
Have you worked for iOS code ?