Search code examples
xamarin.formsxamarin.androidxamarin.ios

how to use pagination in group listview


enter image description hereI am using ItemAppearing method pagination in normal listview. but I developed the same code in group listview. But it is a crash. How to use the ItemAppearing method in grouped Listview. Anyone guide me for this scenario. I Paste my code for ur reference.

     <ListView x:Name="CustomerListview" ItemAppearing="Handle_ItemAppearing"
 IsVisible="true" Footer=""   IsGroupingEnabled="true" BackgroundColor="Transparent" Margin="0,10,0,10" SeparatorVisibility="None"   HasUnevenRows="True" VerticalOptions="FillAndExpand"  HorizontalOptions="CenterAndExpand" >
             <ListView.ItemTemplate>
            <DataTemplate>
           <ViewCell Height="80"> 
           <StackLayout
        Margin="20,0,0,0" Spacing="0" Padding="0"
        Orientation="Horizontal"
        VerticalOptions="FillAndExpand">

        <StackLayout 
            x:Name="firstStackLayout"
            Margin="0,0,0,-6"
            HorizontalOptions="Center"
            Orientation="Vertical"
            VerticalOptions="FillAndExpand">
            <BoxView
                Grid.Row="0"
                 Grid.Column="0"
                 Margin="0,0,0,-6"
                HeightRequest="30"
                HorizontalOptions="Center"
                WidthRequest="2"
                VerticalOptions="FillAndExpand"
                Color="#b11541" />
            <BoxView
                Grid.Row="1"
                Grid.Column="0"
                Margin="0,0,0,0"
                HeightRequest="20"
                CornerRadius="10"
                BackgroundColor="Gray"
                WidthRequest="20">
                <!--<ff:CachedImage.Transformations>
                    <ffTransformations:RoundedTransformation
                        BorderHexColor=""
                        BorderSize="20"
                        Radius="240" />
                </ff:CachedImage.Transformations>-->
            </BoxView>
            <BoxView 
                Grid.Row="2"
                Grid.Column="0"
                Margin="0,-6,0,0"
                HeightRequest="30"
                HorizontalOptions="Center"
                IsVisible="true"
                WidthRequest="2"
                 VerticalOptions="FillAndExpand"
                Color="#b11541" />
        </StackLayout>

                    <StackLayout
                Margin="10,15,5,0"
                HorizontalOptions="Start"
                Orientation="Vertical"
                VerticalOptions="Center">
                <Label
                    FontAttributes="Bold"
                    FontSize="15"
                    HorizontalOptions="Start"
                    Text="{Binding Lease_refno}"
                    TextColor="#b11541"
                    XAlign="Start" />
                <StackLayout
                    Margin="0,0,5,0"
                    Orientation="Horizontal"
                    VerticalOptions="EndAndExpand">
                    <Label
                        FontSize="14"
                        Text="{Binding Billing_start_date}"
                        TextColor="#4e5156" />
                </StackLayout>

            </StackLayout>
                 <StackLayout.GestureRecognizers>
                                                    <TapGestureRecognizer Tapped="leaseClicked"/>
                                               </StackLayout.GestureRecognizers>
    </StackLayout>
                    </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
            <ListView.GroupHeaderTemplate>
                <DataTemplate>
                               <ViewCell Height="55">
                                 <StackLayout BackgroundColor="Transparent" Padding="5, 5, 5, 0">

                            <Frame Margin="5,0,5,5" HasShadow="False" IsClippedToBounds="true" BorderColor="Transparent" BackgroundColor="Transparent" CornerRadius="5" Padding="0">
                                <Grid RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.25}"
                                      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.75}"
                                      BackgroundColor="White" ColumnSpacing="0" RowSpacing="0">

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="5"/>
                                    <RowDefinition Height="Auto"/>
                                     <RowDefinition Height="30"/>
                                </Grid.RowDefinitions>
                                 <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>


        <StackLayout BackgroundColor="White" VerticalOptions="FillAndExpand" Grid.Row="0" Grid.RowSpan="8">
                                    <BoxView HorizontalOptions="Start"  WidthRequest="5"   BackgroundColor="#b11541" VerticalOptions="FillAndExpand"/>
                </StackLayout>

    <Label Text="{Binding Customer_company}" TextColor="Black"
               FontSize="14" Grid.Row="1"  Grid.Column="0" Grid.ColumnSpan="7"
               x:Name="originEntry" Margin="20,5,0,5" 
               VerticalOptions="Start" FontFamily="{StaticResource OpenSansBold}"
               HorizontalOptions="FillAndExpand" />

 <!--<StackLayout Spacing="0"  Grid.Column="0" Padding="0" Margin="0,0,0,10" Grid.Row="2" Orientation="Horizontal">
         <Label Text="View Lease" Padding="5,0,5,0" TextColor="White" BackgroundColor="#97304d"

               Grid.ColumnSpan="4" FontFamily="{StaticResource OpenSansSemiBold}"  Margin="20,0,0,0"  FontSize="12"
               x:Name="destinationEntry"   VerticalOptions="Start"
               HorizontalOptions="Start" >
       <Label.GestureRecognizers>
                                                    <TapGestureRecognizer Tapped="leaseClicked"/>
                                               </Label.GestureRecognizers>
             </Label>

        </StackLayout>-->

                                       <Grid.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" CommandParameter="{Binding .}" Tapped="HeaderTapped"/>
                   </Grid.GestureRecognizers>
                                </Grid>
                                </Frame>
                                     </StackLayout>
                        </ViewCell>
                     </DataTemplate>
        </ListView.GroupHeaderTemplate>
        </ListView>

private async void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e) { var currentIdx = Items.IndexOf((ProductColor)e.Item);

            if (currentIdx > _lastItemAppearedIdx)
            {
                if (Items.Count == 0)
                    return;
                var selectedItem = e.Item as ProductColor;

                //hit bottom!
                if (isLoading == false && selectedItem == ResultList[ResultList.Count - 1])
                {

                    i++;
                    await UserTweetsList("lease", "");


                }
            }
            _lastItemAppearedIdx = Items.IndexOf((ProductColor)e.Item);


    }

Solution

  • var currentIdx = Items.IndexOf((ProductColor)e.Item

    Actually , in the event ItemAppearing you could get the index of current appearing item directly like

    int currentIdx = e.ItemIndex ;
    

    And improve the code like :

    In group ListView the index with equal -1 when the first item appearing .

    if(currentIdx !=-1)
    {
      // if...
    }
    

    In addition , from your code I could not see the part of group . So it could be better to share your sample which contains the issue so that I can test it on my side .

    Update

    private  void CusHandle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
            {
                // var currentIdx = customerList.IndexOf((ProductCustomer)e.Item);
                int currentIdx = e.ItemIndex;
    
                if (currentIdx != -1)
                {
                    if (currentIdx > _lastItemAppearedIdx)
                    {
                        if (_allGroups.Count == 0)
                            return;
                       
                        if (currentIdx== _expandedGroups.Count-2)
                        {
    
                           //...
    
    
                        }
                        _lastItemAppearedIdx = currentIdx;
                    }
                }
        
            }