ArthurCPPCLI
ArthurCPPCLI

Reputation: 1082

Xamarin Forms ListView in ScrollView - Android disable scrolling in listview

I am facing problem when using a ListView inside a Scrollview in Android (via Xamarin Forms).

I am calculating the height of the listview based on item source and a row height but i can still scroll inside the listview :

enter image description here

The listview is the white area contained in the scrollview, with few labels below, so we can scroll :

enter image description here

But in Android we can also scroll inside the listview itself :

enter image description here

I tried to catch the move event in a custom renderer :

public class CustomListViewRenderer : ListViewRenderer
{
    public CustomListViewRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
    {
        base.OnElementChanged(e);

        var customListView = Element as CustomListView;
        if (customListView == null)
        {
            return;
        }

        if (customListView.IsScrollingEnable == false)
        {
            Control.Touch += ListView_Touch;
        }
    }

    private void ListView_Touch(object sender, TouchEventArgs e)
    {
        if (e.Event.Action == MotionEventActions.Move)
        {
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
    }
}

This can solve the space issue but if the user scroll from listview area with touch, the global scrollview doesn't work (you can only scroll with touch from Aqua Blue area).

Is there a way to disable the scroll in the listview without affect the parent scrollview ? The link to the sample project to reproduce the issue : https://1drv.ms/u/s!An8JKHwJo47up2ss_6pdzlXOhtDz?e=GdXrbf

Upvotes: 0

Views: 1074

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10938

Like Jason said, you could use the Listview directly.

With your code, you could set setScrollContainer to false in custom renderer.

 protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
            return;

        if (Control != null)
        {
            Control.SetScrollContainer(false);
        }
    }

enter image description here

Upvotes: 1

Related Questions