0
votes

My Custom List View

<common:CustomListView x:Name="MenuListView" Margin="0,2,0,0" ItemsSource="{Binding Menutems}" 
                        SelectedItemBackgroundColor="{x:Static resources:Colors.HikeColor}" ItemBackgroundColor="White" 
                        RowHeight="70" SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}" BackgroundColor="Transparent" SeparatorVisibility="None">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="0" HeightRequest="70" Spacing="0">
                                    <Label Text="{Binding .}" FontSize="16" FontFamily="{x:Static resources:Fonts.HikeDefaultFont}" VerticalOptions="CenterAndExpand" Margin="40, 10, 10, 10" TextColor="{x:Static resources:Colors.HeaderTextColor}"/>
                                    <BoxView HeightRequest="0.5" VerticalOptions="End" HorizontalOptions="FillAndExpand" BackgroundColor="{x:Static resources:Colors.BordersColor}" />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </common:CustomListView>

View Model Code

private string _SelectedMenuItem { get; set; } = "GENERAL";
    public string SelectedMenuItem
    {
        get
        {
            return _SelectedMenuItem;
        }
        set
        {

            _SelectedMenuItem = value;
            MenuItemSelected(value);
        }
    }

I have rendered list view in this way

public class CustomListViewRenderer : ListViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e);

        var view = (CustomListView)Element;

        if (Control != null && view != null)
        {
            foreach (var cell in Control.VisibleCells)
            {
                if (cell.Selected)
                {
                    cell.BackgroundColor = view.SelectedItemBackgroundColor.ToUIColor();
                }
                else { 
                    cell.BackgroundColor = view.ItemBackgroundColor.ToUIColor();
                }
            }
        }

    }
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        var control = (UITableView)Control;
        var view = (CustomListView)Element;

        if (e.PropertyName == "SelectedItem")
        {
            if (control != null && view != null)
            {
                foreach (var cell in Control.VisibleCells)
                {
                    cell.BackgroundColor = view.ItemBackgroundColor.ToUIColor();
                }

                var ind = control.IndexPathForSelectedRow;
                if (ind != null)
                {
                    control.CellAt(ind).BackgroundColor = view.SelectedItemBackgroundColor.ToUIColor();
                }
            }

        }
    }
}

its working fine on first click but when I clicke second time on same item then background was set default background. please let me know where I am going mistake.

1

1 Answers

0
votes

You can use this to understand the gesture whether user is double clicking on the item & if user is double clicking the any particular item clear selected item as below :

((ListView)sender).SelectedItem = null;