RMR
RMR

Reputation: 629

Long pressed event for MR.grid (inside listview) call twice in xamarin.forms in IOS

I am using MR.Gestures plugin to handle single tap and long pressed event.

<ListView Grid.Row="1"  x:Name="lstProducts" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="Transparent" IsPullToRefreshEnabled="True" Refreshing="lstProducts_Refreshing">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                            <ViewCell>
                                <ViewCell.View>
                                    <Grid>
                                        <Grid.Margin>
                                            <OnIdiom x:TypeArguments="Thickness" Phone="10" Tablet="20"/>
                                        </Grid.Margin>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="{Binding grdRowHeight}"/>
                                        </Grid.RowDefinitions>
                                        <mr:Grid x:Name="grd_left_product" Grid.Column="0" Grid.Row="0" ClassId="{Binding Left_attachedFilePath}" Style="{StaticResource MostOuterGridForBorder}"
                                                 Tapped="grd_left_product_Tapped" 
                                                 LongPressed="grd_left_product_DoubleTapped">
                                            <Grid Style="{StaticResource OuterGrid}">
                                                <ffimageloading:CachedImage Source="{Binding Left_thumbnailImage}" Style="{StaticResource ThumbNailImageStyle}" />
                                                <StackLayout VerticalOptions="EndAndExpand" >
                                                    <custom:CustomWrapLabel Text="{Binding Left_name}" Style="{StaticResource CustomWrapLabelStyle}"/>
                                                </StackLayout>
                                                <StackLayout IsVisible="{Binding Left_IsChecked}" Style="{StaticResource MultipleDownloadStack}">
                                                    <ffimageloading:CachedImage Source="check_ic.png" Style="{StaticResource MultipleDownloadCheckIcon}" />
                                                </StackLayout>
                                            </Grid>
                                        </mr:Grid>
                                        <mr:Grid x:Name="grd_right_product" Grid.Column="1" Grid.Row="0" ClassId="{Binding Right_attachedFilePath}" Style="{StaticResource MostOuterGridForBorder}" IsVisible="{Binding Right_grd_product_visibility}"
                                                 Tapped="grd_right_product_Tapped" 
                                                 LongPressed="grd_right_product_DoubleTapped">
                                            <Grid Style="{StaticResource OuterGrid}">
                                                <ffimageloading:CachedImage Source="{Binding Right_thumbnailImage}" Style="{StaticResource ThumbNailImageStyle}" />
                                                <StackLayout VerticalOptions="EndAndExpand" >
                                                    <custom:CustomWrapLabel Text="{Binding Right_name}" Style="{StaticResource CustomWrapLabelStyle}" />
                                                </StackLayout>
                                                <StackLayout IsVisible="{Binding Right_IsChecked}" Style="{StaticResource MultipleDownloadStack}">
                                                    <ffimageloading:CachedImage Source="check_ic.png" Style="{StaticResource MultipleDownloadCheckIcon}" />
                                                </StackLayout>
                                            </Grid>
                                        </mr:Grid>
                                    </Grid>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

After service call below function divide my data into two column and return observable collection.

private ObservableCollection<ProductData> BifurcationInLeftRight(List<ResponseDetail> lstResponseDetail)
        {
            lstcollectionproductDatas = new ObservableCollection<ProductData>();
            for (int i = 0; i < lstResponseDetail.Count; i++)
            {
                var items = new ProductData();
                items.Left_IsChecked = false;
                items.Right_IsChecked = false;
                items.Left_productId = lstResponseDetail[i].productId;
                items.Left_name = lstResponseDetail[i].name;
                items.Left_attachedFilePath = lstResponseDetail[i].attachedFilePath;
                items.Left_attachedLink = lstResponseDetail[i].attachedLink;
                items.Left_thumbnailImage = lstResponseDetail[i].thumbnailImage;

                i++;
                if (i < lstResponseDetail.Count)
                {
                    items.Right_productId = lstResponseDetail[i].productId;
                    items.Right_name = lstResponseDetail[i].name;
                    items.Right_attachedFilePath = lstResponseDetail[i].attachedFilePath;
                    items.Right_attachedLink = lstResponseDetail[i].attachedLink;
                    items.Right_thumbnailImage = lstResponseDetail[i].thumbnailImage;
                    items.Right_grd_product_visibility = true;
                }
                else
                {
                    items.Right_productId = null;
                    items.Right_name = null;
                    items.Right_attachedFilePath = null;
                    items.Right_attachedLink = null;
                    items.Right_thumbnailImage = null;
                    items.Right_grd_product_visibility = false;

                    items.grdRowHeight = ThumbNailHeight;
                    lstcollectionproductDatas.Add(items);
                    break;
                }
                items.grdRowHeight = ThumbNailHeight;
                lstcollectionproductDatas.Add(items);
            }
            return lstcollectionproductDatas;
        }

on long press I checked the item and rebind my whole item source. "grd_left_product_DoubleTapped" and "grd_right_product_DoubleTapped" are my long pressed events

private void grd_left_product_DoubleTapped(object sender, MR.Gestures.LongPressEventArgs e)
        {
            try
            {
                var productId = sender as Grid;
                if (!string.IsNullOrEmpty(productId.ClassId) && lstcollectionproductDatas != null && lstcollectionproductDatas.Count > 0)
                {
                    var selectedObject = lstcollectionproductDatas.Where(d => d.Left_attachedFilePath == productId.ClassId).FirstOrDefault();
                    if (selectedObject != null)
                    {
                        int index = lstcollectionproductDatas.IndexOf(selectedObject);
                        if (!selectedObject.Left_IsChecked)
                            lstcollectionproductDatas[index].Left_IsChecked = true;
                        else
                            lstcollectionproductDatas[index].Left_IsChecked = false;
                    }
                    lstProducts.ItemsSource = null;
                    lstProducts.ItemsSource = lstcollectionproductDatas;
                }
                else
                    new ShowSnackbar(AppResources.Validation_FileNotFound_Message);
            }
            catch (Exception)
            {
                new ShowSnackbar(AppResources.Validation_Exception);
            }
        }

Problem :

This code working fine in android but in IOS, when I long press one item it checked but when I long press another item from different row, Long pressed event call twice.

At the first time, for previous Checked item and second time for the item which we want to checked. That is why at first time which has been already checked become unchecked.

My question is why long pressed event called twice in MR.Gestures?

There are three row (index - 0,1,2)

  1. Left item from index-2 is Checked

  2. Now I long press on left item of index-1.

  3. Long press event call twice.

  4. First time for index-2 and unchecked the selected item.

  5. second time for index-1 and checked.

Upvotes: 0

Views: 211

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

Have you looked this? free GestureSample app from GitHub

The longpressed event is called once in both began and ended, you can make the distinction .When the state is equal to ended direct return,and do something(such as refush UI ) when the states is equal to began.

Upvotes: 1

Related Questions