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

Custom view, New data is added to StackLayout with jumping

$
0
0

I create two XAML files that are responsible for displaying account (Participant) information.

This is a XAML file that describes the participant and uses labels such as name and phone.

<ContentView.Content>
        <StackLayout>
            <Grid>
                <BoxView Grid.RowSpan="3" BackgroundColor="LightGreen"/>
                <StackLayout Grid.Row="0" Orientation="Horizontal">
                    <Label                      
                       FontSize="20"
                       VerticalOptions="Start"
                       HorizontalOptions="Start"
                       x:Name="LabelFirstName"/>
                    <Label
                       FontSize="20"
                       VerticalOptions="Start"
                       HorizontalOptions="Start"
                       x:Name="LabelLastName"/>
                </StackLayout>
                <Label Grid.Row="1"
                       FontSize="20"
                       VerticalOptions="StartAndExpand"
                       HorizontalOptions="StartAndExpand"
                       x:Name="LabelPhoneNumber"/>
                <StackLayout Grid.Row="2" Orientation="Horizontal">
                    <Label Text="Select: "
                           FontSize="20"
                           VerticalOptions="Start"
                           HorizontalOptions="Start"/>
                    <CheckBox
                        VerticalOptions="Start"
                        HorizontalOptions="Start"
                        CheckedChanged="OnCheckedChanged_MainCheckBox"/>
                </StackLayout>
            </Grid>
        </StackLayout>
    </ContentView.Content>

This is an XAML file that represents the list of participants

<ContentView.Content>
        <StackLayout>
            <SearchBar VerticalOptions="Start" 
                        HorizontalOptions="FillAndExpand"/>
            <ScrollView VerticalOptions="Start" 
                    HorizontalOptions="CenterAndExpand"
                    x:Name="MainScrollView">
            </ScrollView>
        </StackLayout>
  </ContentView.Content>

The list of participants comes from the server. My app uses the BackgroundWorker to wait for responses from the server and then the BackgroundWorker calls to the SetParticipants as a callback method.

public void SetParticipants(string participantsJSON)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List<ParticipantsTable> participantsTable =
                JsonSerializer.Deserialize<List<ParticipantsTable>>(participantsJSON);

            foreach (var participantTable in participantsTable)
            {
                ParticipantTemplate participant = new ParticipantTemplate();
                participant.ParticipantsTable = participantTable;
                mainStackLayout.Children.Add(participant);
            }

            if (participantsTable.Count != 0)
            {
                isAllowLoadParticipants = true;
            }
            sw.Stop();
            Console.WriteLine("SetParticipants = " + sw.ElapsedTicks);
        }

The ScrolledListener method is used to request new data.

public Action<int, int> RequestParticipants;

public void ScrolledListener(object sender, EventArgs args)
        {
            if (!isAllowLoadParticipants)
            {
                return;
            }
            var scrollView = (ScrollView)sender;

            bool isNeededMoreParticipants =
                scrollView.ScrollY >
                (int)(scrollView.ContentSize.Height - scrollView.ContentSize.Height / 2);

            if (isNeededMoreParticipants)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                RequestParticipants(numberToSkipParticipants, numberOfParticipants);
                sw.Stop();
                Console.WriteLine("RequestParticipants = " + sw.ElapsedTicks);
                numberToSkipParticipants += numberOfParticipants;
                isAllowLoadParticipants = false;
            }
        }

I had posted all code to github.com/OleksandrMyronchuk/ParticipantModule/tree/master/ParticipantModule

Jumping youtu.be/x1iEHFPINnE

How to fix the scrolling ?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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