0
votes

I don't know why, but when I tap any ListView item, the StackLayout inside it's template loses the background color.

I use default ViewCell inside ListView. Is this a Xamarin.Forms bug?

I have this problem only on Android.

    <StackLayout>             
            <ListView x:Name="Llist"
            ItemTapped="Lista_ItemTapped" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="{Binding color}">
                                <Label Text="{Binding name}"/>
                                <Label Text="{Binding age}"/>
                                <Label Text="{Binding sex}"/>

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>      
            <!--<Button x:Name="excutar" Text="Executar"/>-->
        </StackLayout>
    </ContentPage>
1
Does it change its color to orange or gray?FreakyAli
Does not change, when I define a touch event does not change, but if I delete the ItemTapped = "ListItemTapped" at the top of the list it changes to orange @G.hakimamaro tati
So on click of your ListViews Item the color changes to orange and you do not want that right?FreakyAli
It changed but when setting a Tapped event in the list, this event removes the background coloramaro tati
Okay wait i know the solution adding it in 5mins or soFreakyAli

1 Answers

0
votes

You will have to add a custom view cell where you can set the on click colour:

In your PCL:

Add a custom ViewCell something like this:

 public class ExtendedViewCell : ViewCell
{
    /// <summary>
    /// The SelectedBackgroundColor property.
    /// </summary>
    public static readonly BindableProperty SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(ExtendedViewCell), Color.Default);

    /// <summary>
    /// Gets or sets the SelectedBackgroundColor.
    /// </summary>
    public Color SelectedBackgroundColor
    {
        get { return (Color)GetValue(SelectedBackgroundColorProperty); }
        set { SetValue(SelectedBackgroundColorProperty, value); }
    }
}

In your, Android Project add the following:

public class ExtendedViewCellRenderer : ViewCellRenderer
{

    private Android.Views.View _cellCore;
    private Drawable _unselectedBackground;
    private bool _selected;

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
    {
        try
        {
            _cellCore = base.GetCellCore(item, convertView, parent, context);

            // Save original background to roll-back to it when not selected,
            // we're assuming that no cells will be selected on creation.
            _selected = false;
            _unselectedBackground = _cellCore.Background;

            return _cellCore;
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
            return null;
        }
    }

    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        try
        {
            base.OnCellPropertyChanged(sender, args);

            if (args.PropertyName == "IsSelected")
            {
                // Had to create a property to track the selection because cellCore.Selected is always false.
                _selected = !_selected;

                if (_selected)
                {
                    var extendedViewCell = sender as ExtendedViewCell;
                    _cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
                }
                else
                {
                    _cellCore.SetBackground(_unselectedBackground);
                }
            }
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
        }
    }
}

Similarly for iOS something like below:

public class ExtendedViewCellRenderer : ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        try
        {
            var cell = base.GetCell(item, reusableCell, tv);
            var view = item as ExtendedViewCell;
            cell.SelectedBackgroundView = new UIView
            {
                BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
            };
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            return cell;
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
            return null;
        }
    }

}

Now do not forget to add the custom renderer header on the native Classes above the namespace

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]

Now all you have to do is replace your above ViewCell with this controls view cell and pass the SelectedBackgroundProperty.

In your case it will look like this:

<ListView x:Name="Llist"
        ItemTapped="Lista_ItemTapped" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <nameSpace:ExtendedViewCell SelectedBackgroundColor="White">
                        <StackLayout BackgroundColor="{Binding color}">
                            <Label Text="{Binding name}"/>
                            <Label Text="{Binding age}"/>
                            <Label Text="{Binding sex}"/>

                        </StackLayout>
                    </nameSpace:ExtendedViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>