简体   繁体   English

适用于iOS的Xamarin Forms ListView:始终显示ScrollBar

[英]Xamarin Forms ListView for iOS: Always Show ScrollBar

I have a ProductList of type ObservableCollection<Product> where Product is a class given below: 我有一个ObservableCollection<Product>类型的ProductList ,其中Product是下面给出的类:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Path { get; set; }
}

In my Xamarin Forms iOS Application, I have a ListView whose ItemSource is ProductList . 在我的Xamarin Forms iOS应用程序中,我有一个ListViewItemSourceProductList When number of Products in the ProductList is so that the height of ListView is not enough to show all of them, additional items can be reached by scrolling the ListView . ProductList的Products数量不足以使ListView的高度不足以显示所有产品时,可以通过滚动ListView来获得其他项。 However, I want the ScrollBar that is displayed only when scrolling the ListView to be displayed always. 但是,我希望仅在滚动ListView时才显示的ScrollBar始终显示。 Is it possible or should I try another UI besides ListViev to be able to always show ScrollBar. 是否可以或者我应该尝试除ListViev之外的其他UI以便始终显示ScrollBar。

There are some solutions for Xamarin.Android but I couldn't find any effective solution for the iOS App. Xamarin.Android有一些解决方案,但我找不到适用于iOS应用程序的任何有效解决方案。

Thank you very much... 非常感谢你...

As @Cole commented, the flashScrollIndicators can only show the indicator a short time. 正如@Cole所说, flashScrollIndicators只能在短时间内显示该指示器。

So, you have to customize a scroll indicator. 因此,您必须自定义滚动指示器。

Create a custom renderer of the ListView and add your custom scroll indicator , like this: 创建ListView的自定义渲染器,并添加自定义滚动指示器 ,如下所示:

    public class MyListViewRenderer : ListViewRenderer
    {
        public UIView bar;
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            //Hide the default Scroll Indicator.
            Control.ShowsVerticalScrollIndicator = false;


            //Set Delegate
            CustomScrollDelegate customScrollDelegate = new CustomScrollDelegate();
            Control.Delegate = customScrollDelegate;

            //Create the background view of custom indicator.
            double frameHeight = Control.Frame.Size.Height;
            double frameWidth = Control.Frame.Size.Width;

            double barBackgroundWidth = 6;
            double statusBarHeight = 20;

            UIView barBackgroundView = new UIView();
            CGRect barBVRect = new CGRect(frameWidth - barBackgroundWidth, statusBarHeight, barBackgroundWidth, frameHeight);
            barBackgroundView.Frame = barBVRect;
            barBackgroundView.BackgroundColor = UIColor.Gray;
            barBackgroundView.Layer.CornerRadius = 2;
            barBackgroundView.Layer.MasksToBounds = true;


            //Create the bar of the custom indicator.
            bar = new UIView();
            CGRect barRect = new CGRect(1, 0, 4, 0);
            bar.Frame = barRect;
            bar.BackgroundColor = UIColor.Black;
            bar.Layer.CornerRadius = 2;
            bar.Layer.MasksToBounds = true;

            //Add the views to the superview of the tableview.
            barBackgroundView.AddSubview(bar);
            Control.Superview.AddSubview(barBackgroundView);

            //Transfer the bar view to delegate.
            customScrollDelegate.bar = bar;

        }

        public override void LayoutSubviews()
        {
            base.LayoutSubviews();
            Console.WriteLine("End of loading!!!");
            double contentHeight = Control.ContentSize.Height;
            double frameHeight = Control.Frame.Size.Height;
            double barHeight = frameHeight * frameHeight / contentHeight;


            //Reset the bar height when the table view finishes loading.
            CGRect barRect = new CGRect(bar.Frame.X, bar.Frame.Y, bar.Frame.Width, barHeight);
            bar.Frame = barRect;
        }

    }

Implement the Scrolled delegate which tracks the scrolling action of the scrollView. 实现Scrolled委托,该委托可跟踪scrollView的滚动动作。 You can update the position of the indicator in the delegate. 您可以更新指标在代表中的位置。

    public class CustomScrollDelegate : UIKit.UITableViewDelegate
    {
        public UIView bar;
        double barY;

        public override void Scrolled(UIScrollView scrollView)
        {
            double y = scrollView.ContentOffset.Y;
            double contentHeight = scrollView.ContentSize.Height;
            double frameHeight = scrollView.Frame.Size.Height;

            double barHeight = frameHeight * frameHeight / contentHeight;
            barY = y / (contentHeight - frameHeight) * (frameHeight - barHeight);

            //Cut the bar Height when it over the top.
            if (barY < 0)
            {
                barHeight = barHeight + barY;
                barY = 0;
            }

            //Cut the bar height when it over the bottom.
            if (barY > (frameHeight - barHeight))
            {
               barHeight = barHeight - (barY - (frameHeight - barHeight));
            }

            //Reset the barView rect. Let's move!!!
            CGRect barRect = new CGRect(bar.Frame.X, barY, bar.Frame.Width, barHeight);
            bar.Frame = barRect;

        }

    }

It works like this: 它是这样的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM