Disable scroll in ListView

johanksonjohankson SEInsider, University ✭✭

Hi!

Is it possible to disable scrolling in a ListView if you have only a few items?

Thanks

// Johan

Posts

  • Mdt87Mdt87 DEMember ✭✭

    Hello Johan,

    do you found a solution? I'm currently looking for the same, but the only solution I found is, with a custom Renderer of the ListView and the on iOS you can use the Control.scrollEnabled = NO;

    Is there any possibility to disable the scroll on a list view via Xamarin.Forms?

    kind regards

    Martin

  • johanksonjohankson SEInsider, University ✭✭

    No, I haven't found a solution I'm afraid.

  • HippolyteBarraudHippolyteBarraud FRMember ✭✭

    You can set the HeightRequest of your list view to be >= to its content with :

    listView.HeightRequest = Elements.Length * listView.RowHeight;

    You must also take into account any header/footer set.

    If the size of the listview is superior to it's content, there is no scrolling activated.
    It's pretty useful if you want to put a listview inside an existing scrollview for exemple.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @HippolyteBarraud:
    Thanks for your posting, but....:
    In Android, ListViews in ScrollView are generally no more scrollable since a few versions...
    Therefore I had to add the XLabs-PopUpControl (to be able to use various ListViews from a ScrollView):
    forums.xamarin.com/discussion/33587/how-to-use-a-listview-in-a-scrollview-with-xlabs-popup-control#latest

    while in iOS, it still works.
    I now have the reverse problem, that I have some "Single-Select"-, "MultiSelect"- and "Checkbox"-ListViews, that are scrollable in iOS (what I don't want in this case).
    Although, I set a HightRequest to the ListView (so that all items are showed) , they still are scrollable.

    Question: **
    **Are you sure, that the ListViews really are not scrollable in iOS, when you set the HightRequest?

    (if yes, I have to have a deeper look and maybe add some additional space :wink:).

    Thanks for a further reply.

  • HippolyteBarraudHippolyteBarraud FRMember ✭✭

    To be more specific ListViews in iOS are NOT scrollable if and only if their Height is strictly > than their content's height.
    Try to add an artificial gap by setting HeightRequest += 100 to check if the ListView is still scrollable when it's Height is far wider than it's actual content.

  • enzyklopenzyklop DEMember ✭✭

    @HippolyteBarraud your Solution worked great for me! The Problem with Android (everything I had tested so far) is, that the ListView which is included in a ScrollView is not scrollable and show only the amount of elements which where included in the space the HeightRequestProperty defines.

    With your Solution, there is a possibility to dynamically change this space to the needed value! :) thanks a lot that is great!

    But I have another problem so far with this. I need a grouped List, but in a grouped list the RowHeight Property is always 0... Any suggestions how to get the RowHeight of the innerList?

  • SylvainGravelSylvainGravel CAMember ✭✭
    edited December 2016

    Not sure if anybody is still looking for a solution, but a simple Effect is enough

    [assembly:ExportEffect (typeof(NoScrollListViewEffect), Constants.NoScrollListViewEffectKey)]
    namespace NS.Effects
    {
        class NoScrollListViewEffect : PlatformEffect
        {
            private UITableView _NativeList => Control as UITableView;
    
            protected override void OnAttached()
            {
                if (_NativeList != null)
                {
                    _NativeList.ScrollEnabled = false;
                }
            }
    
            protected override void OnDetached()
            {
            }
        }
    }
    
  • UdaraAlwisUdaraAlwis LKMember ✭✭

    I found a solution to handle this in Android with Xamarin Forms custom ListView renderer. As follows, :)

        public class CustomListViewRenderer : ListViewRenderer
        {
            private int _mPosition;
    
            public override bool DispatchTouchEvent(MotionEvent e)
            {
                if (e.ActionMasked == MotionEventActions.Down)
                {
                    // Record the position the list the touch landed on
                    _mPosition = this.Control.PointToPosition((int)e.GetX(), (int)e.GetY());
                    return base.DispatchTouchEvent(e);
                }
    
                if (e.ActionMasked == MotionEventActions.Move)
                {
                    // Ignore move eents
                    return true;
                }
    
                if (e.ActionMasked == MotionEventActions.Up)
                {
                    // Check if we are still within the same view
                    if (this.Control.PointToPosition((int)e.GetX(), (int)e.GetY()) == _mPosition)
                    {
                        base.DispatchTouchEvent(e);
                    }
                    else
                    {
                        // Clear pressed state, cancel the action
                        Pressed = false;
                        Invalidate();
                        return true;
                    }
                }
    
                return base.DispatchTouchEvent(e);
            }
        }
    

    Enjoy! ;)

  • asmykasmyk Member ✭✭

    While using ScrollEnabled = false; ItemAppearing event is not raising for ListView item in this case. Does anyone know how to fix this?

  • adelapt98adelapt98 Member ✭✭
    edited April 2018

    listView.HeightRequest = listView.RowHeight * ((Your List that you want to show in ListView).Count + 1);

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭

    ListView.IsEnabled = false;

  • batilakbatilak Member ✭✭

    @AndreiMisiukevich_ said:
    ListView.IsEnabled = false;

    This also disables Touch functionality.

    I want touch/tap working but just scroll disabled. is there any other way around?

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭

    @batilak why don't you want to use StackLayout instead

  • batilakbatilak Member ✭✭

    @AndreiMisiukevich_ said:
    @batilak why don't you want to use StackLayout instead

    :P design requirement to use List, any way I just extended ListViewRenderer to have scroll disabled.

    [assembly: ExportRenderer(typeof(ListViewWithoutScroll), typeof(ListViewWithoutScrollRenderer))]
    namespace .iOS.Renderers
    {
    public class ListViewWithoutScrollRenderer : ListViewRenderer
    {
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
    {
    base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Unsubscribe
            }
    
            if (e.NewElement != null)
            {
                Control.ScrollEnabled = false;
            }
        }
    }
    

    }

  • abdalrhmanabdalrhman Member ✭✭

    @HippolyteBarraud said:
    You can set the HeightRequest of your list view to be >= to its content with :

    listView.HeightRequest = Elements.Length * listView.RowHeight;

    You must also take into account any header/footer set.

    If the size of the listview is superior to it's content, there is no scrolling activated.
    It's pretty useful if you want to put a listview inside an existing scrollview for exemple.

    Ok but When İ give the RowHeight="Auto" what Can I do?!

  • zwilkinzwilkin Member

    @adelapt98 said:
    listView.HeightRequest = listView.RowHeight * ((Your List that you want to show in ListView).Count + 1);

    You, sir, just solved my 2-day long problem! This algorithm is working flawlessly on both iOS and Android for me.

    @abdalrhman said:
    Ok but When İ give the RowHeight="Auto" what Can I do?!

    Setting a RowHeight property to "Auto" means that the UI will automatically determine the required height at runtime based on the child elements. That being said, calling ListView.RowHeight AFTER the UI has been compiled (i.e. in the OnAppearing lifecycle), the RowHeight has already been defined by the compiler. Ergo you have an automatically defined RowHeight that you can tap into.

Sign In or Register to comment.