Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

CollectionView jumping

$
0
0

Hi guys and girls!
In my project it contains CollectionView, which contain viewes with switches. After toggled the switch, each waste item increases in size. Everything is good if the waste is at the top, but i have problems with the bottom of the view: the list (collectionview) automatically scrolls up.
What needs to be done so that the list does not scroll?

Code in view of CollectionView. I don't use XAML

>! public class PaymentBillRelationViewCell: StackLayout
    {
        private readonly XLabel DocumentName;
        private readonly XLabel Date;
        private readonly XLabel Sum;
        private readonly XLabel Remainder;
        private readonly NamedSwitch RelationshipSwitch;
        private readonly NamedInputForForm inputSum;
        private readonly Divider middleDivider;
        private readonly Divider bottomDivider;
        private readonly Action<PaymentBillRelationViewData> scrollItemToStart;
        private readonly Action<PaymentBillRelationViewData, bool> changeRelationStatus;
        private readonly Action<PaymentBillRelationViewData, decimal?> changeRelationAmount;
        private readonly Func<PaymentBillRelationViewData, bool> isShowRemainder;
        private readonly Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder;
        private readonly Func<PaymentBillRelationViewData, string> checkRelationForError;
        private readonly Action updateHeader;

        private readonly Grid grid;

        public PaymentBillRelationViewCell(
            Action<PaymentBillRelationViewData> scrollItemToStart,
            Action<PaymentBillRelationViewData, bool> changeRelationStatus,
            Action<PaymentBillRelationViewData, decimal?> changeRelationAmount,
            Func<PaymentBillRelationViewData, bool> isShowRemainder,
            Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder,
            Func<PaymentBillRelationViewData, string> checkRelationForError,
            Action updateHeader,
            bool hasBottomButton = false,
            string textForButton = "",
            Action goToCreateMoney = null)
        {
            this.scrollItemToStart = scrollItemToStart;
            this.changeRelationStatus = changeRelationStatus;
            this.changeRelationAmount = changeRelationAmount;
            this.isShowRemainder = isShowRemainder;
            this.calculateRelationRemainder = calculateRelationRemainder;
            this.checkRelationForError = checkRelationForError;
            this.updateHeader = updateHeader;
            grid = new Grid
                   {
                       ColumnDefinitions = new ColumnDefinitionCollection
                                           {
                                               new ColumnDefinition
                                               {
                                                   Width = GridLength.Auto
                                               },
                                               new ColumnDefinition
                                               {
                                                   Width = GridLength.Auto
                                               },
                                               new ColumnDefinition
                                               {
                                                   Width = GridLength.Star
                                               },
                                               new ColumnDefinition
                                               {
                                                   Width = Sizes.FormItemRightPadding
                                               }
                                           },
                       RowDefinitions = new RowDefinitionCollection
                                        {
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto },
                                            new RowDefinition { Height = GridLength.Auto }
                                        }
                   };

            grid.Children.Add(GetTopBoldDivider().Export(out var boldDividerLayout), 0, 0);
            Grid.SetColumnSpan(boldDividerLayout, 4);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  HorizontalOptions = LayoutOptions.Start,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out DocumentName), 0, 1);
            Grid.SetColumnSpan(DocumentName, 3);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out Date), 0, 2);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out Sum), 0, 3);
            Grid.SetColumnSpan(Sum, 3);
            grid.Children.Add(new NamedSwitch("").Export(out RelationshipSwitch), 2, 1);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.DashboardCellText,
                                  VerticalOptions = LayoutOptions.End,
                                  HorizontalOptions = LayoutOptions.StartAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  TextColor = Colors.TextDisableColor,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           },
                                  IsVisible = false
                              }.Export(out Remainder), 0, 4);
            Grid.SetRowSpan(RelationshipSwitch, 4);
            Grid.SetColumnSpan(Remainder, 4);
            grid.Children.Add(new Divider().Export(out middleDivider), 0, 5);
            Grid.SetColumnSpan(middleDivider, 4);
            grid.Children.Add(new NamedInputForForm(new NamedInput("Связано, ₽", true, maxLength: 30)
                                                    {
                                                        Keyboard = Keyboard.Numeric,
                                                        Padding = new Thickness { Bottom = 8 },
                                                    }).Export(out inputSum), 0, 6);
            Grid.SetColumnSpan(inputSum, 4);
            grid.Children.Add(new Divider
                              {
                                  VerticalOptions = LayoutOptions.EndAndExpand,
                                  FullWidth = true
                              }.Export(out bottomDivider), 0, 7);
            Grid.SetColumnSpan(bottomDivider, 4);
            RelationshipSwitch.SwitchControl.Toggled += (_, __) => ChangeRelation();
            inputSum.Content.TextChanged += (_, __) => ChangeRelatedSum();
            inputSum.DividerVisible = false;
            grid.BackgroundColor = Color.White;
            Children.Add(grid);
            if (hasBottomButton)
                Children.Add(new PaymentBillRelationFooter(textForButton, goToCreateMoney));
        }

        private LightStackLayout GetTopBoldDivider()
        {
            return new LightStackLayout
                   {
                       Children =
                       {
                           new Divider(Colors.LightGrayDivider)
                           {
                               VerticalOptions = LayoutOptions.End,
                               FullWidth = true,
                               HeightRequest = 16,
                           },
                           new Divider
                           {
                               VerticalOptions = LayoutOptions.End,
                               FullWidth = true
                           }
                       }
                   };
        }

        protected override void OnBindingContextChanged()
        {
            var data = (PaymentBillRelationViewData) BindingContext;
            if (data == null)
                return;

            SetData(data);
            base.OnBindingContextChanged();
        }

        private void SetData(PaymentBillRelationViewData item)
        {
            if (inputSum.IsVisible == RelationshipSwitch.Value)
                return;

            if (isShowRemainder.Invoke(item))
            {
                Remainder.IsVisible = true;
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                middleDivider.SetValue(Grid.RowProperty, 5);
                inputSum.SetValue(Grid.RowProperty, 6);
                RelationshipSwitch.SetValue(Grid.RowSpanProperty, 4);
            }
            else
            {
                Remainder.IsVisible = false;
                middleDivider.SetValue(Grid.RowProperty, 6);
                inputSum.SetValue(Grid.RowProperty, 7);
                RelationshipSwitch.SetValue(Grid.RowSpanProperty, 3);
            }

            inputSum.Focused += () => scrollItemToStart.Invoke(item);
            RelationshipSwitch.Value = item.ConnectedSumToCurrentDoc > 0;
            inputSum.IsVisible = RelationshipSwitch.Value;
            middleDivider.IsVisible = RelationshipSwitch.Value;
            if (inputSum.IsVisible)
                inputSum.Text = item.ConnectedSumToCurrentDoc != 0
                                    ? item.ConnectedSumToCurrentDoc.ToString()
                                    : "";

            DocumentName.Text = item.DocumentName;
            Date.Text = item.DateOfDocument;
            Sum.Text = $"{item.AllAmountMoney.FormatMoneyWithCoopeks()} {MoneyHelpers.RubleSumbol}";
        }

        private void ChangeRelatedSum()
        {
            var item = (PaymentBillRelationViewData) BindingContext;
            if (item == null)
                return;
            decimal? inputSum = null;
            try
            {
                if (this.inputSum.Text.Length > 0)
                    inputSum = decimal.Parse(this.inputSum.Text);
            }
            catch (Exception e)
            {
            }

            changeRelationAmount?.Invoke(item, inputSum);
            if (isShowRemainder.Invoke(item))
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
            var textError = checkRelationForError?.Invoke(item);
            if (textError.Length > 0)
            {
                this.inputSum.Content.Hint = textError;
                this.inputSum.Content.HintColor = Color.Red;
                bottomDivider.BackgroundColor = Color.Red;
            }
            else
            {
                this.inputSum.Content.Hint = "Связано, ₽";
                this.inputSum.Content.HintColor = Colors.TextDisableColor;
                bottomDivider.BackgroundColor = Colors.Divider;
            }

            updateHeader?.Invoke();
        }

        private void ChangeRelation()
        {
            var item = (PaymentBillRelationViewData) BindingContext;
            if (inputSum.IsVisible == RelationshipSwitch.Value)
                return;
            inputSum.IsVisible = RelationshipSwitch.Value;
            middleDivider.IsVisible = RelationshipSwitch.Value;
            changeRelationStatus?.Invoke(item, RelationshipSwitch.Value);
            inputSum.Text = RelationshipSwitch.Value ? item.ConnectedSumToCurrentDoc.ToString() : "";
            if (isShowRemainder.Invoke(item))
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
            updateHeader?.Invoke();
        }
    }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>