Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] ItemsViewCell SelectedBackground shows Gray selected background even when content visual state has been overriden #7790

Closed
newky2k opened this issue Oct 3, 2019 · 8 comments · Fixed by #12651
Assignees
Labels
a/collectionview e/5 🕔 5 in-progress This issue has an associated pull request that may resolve it! m/high impact ⬛ t/bug 🐛
Milestone

Comments

@newky2k
Copy link

newky2k commented Oct 3, 2019

Description

On iOS
When using a CollectionView, overriding the selected visual states of the DataTemplate content the Gray SelectBackgroundView is shown and cannot be hidden. A long press will show the Gray selectedbackground color, then the overridden one which looks odd.

Additionally, if the content doesn't fill the whole cell(such as using rounded corners) the gray background selection shows through on the unused space which looks bad(see below)

On Android
The same thing happens but it uses the system selection colour if the overriden background colour is Transparent.

            <DataTemplate>
                <Grid Margin="10" BackgroundColor="Transparent" HeightRequest="200">
                    <VisualStateManager.VisualStateGroups>
                       <VisualStateGroupList>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor" Value="Transparent" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateGroupList>
                    </VisualStateManager.VisualStateGroups>

The justification is that i want to handle the selection change within the visible content and there is no way to stop the full cell background colour(Gray on iOS and System colour on android) being shown and too leave selection to the templated content.

This is the code on iOS that is causing the problem

var selectedBackgroundView = new UIView
{
BackgroundColor = UIColor.Gray
};
SelectedBackgroundView = selectedBackgroundView;

Steps to Reproduce

  1. Create a new XF project
  2. Add a CollectionView, set to use an ItemSource and a DataTemplate and SelectionMode set to single
  3. Add a Grid(with the visual state as above) as the root item object, then add a Frame as below in the ItemTemplate
  • Margin = "10"
  • BackgroundColor = "Red"
  • CornerRadius = "20"

Expected Behavior

When the item is selected the unused background will not change colour

Actual Behavior

The unused background shows either Gray(iOS) and system color on Android

You could provide a property of CollectionView to set the selected item colour, or have some other way of disabling it. I could disable selection and add a tap gesture to the sub-views but that would be more work and would lose some of the benefits of CollectionView.

Basic Information

  • Version with issue: 4.2
  • Last known good version: n/a
  • IDE: VS4M 2019 8.3
  • Platform Target Frameworks:
    • iOS: 13.0
    • Android:
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Screenshot 2019-10-03 at 09 42 47

Reproduction Link

CollectionViewTest.zip

@newky2k newky2k added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 3, 2019
@pauldipietro pauldipietro added this to New in Triage Oct 3, 2019
@samhouts samhouts added this to Backlog in CollectionView Oct 4, 2019
@adrianknight89
Copy link
Contributor

adrianknight89 commented Oct 4, 2019

@newky2k There was a discussion on the selected item background color somewhere on Github, but I can't find it, particularly with respect to overriding Android's default style colors. @hartez

Another approach here could possibly be adding content insets to CollectionView along with item spacing, which would not show the gray/orange selection colors(#7442), but it would restrict the tappable item area.

I agree, however, that VSM should work as intended.

@PureWeen PureWeen moved this from New to Ready For Work in Triage Oct 5, 2019
@MSiccDev
Copy link

MSiccDev commented Oct 5, 2019

I have to confirm this one. Happens also here on my end.

collviewselectedbug

EDIT: In my case, I was able to work around the behavior using a Grid and setting the background in both cases to the page background...

@samhouts samhouts moved this from Ready For Work to Needs Estimate in Triage Oct 7, 2019
@hartez hartez removed this from Needs Estimate in Triage Nov 26, 2019
@hartez hartez self-assigned this Feb 14, 2020
@hartez hartez removed the s/unverified New report that has yet to be verified label Feb 14, 2020
@eli191
Copy link

eli191 commented May 21, 2020

On Android, you can make it transparent by setting
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
Does someone knows a renderer stuff to achieve transparency on iOS?

@LeoJHarris
Copy link

@eli191 Ive followed as suggested in the docs on Xamarin using the visual state manage but doesnt work, I havnt been able to find work around yet. My problem is my background is a blurred image so I cannot simply change the row to the same as the background source.

Below does not work:

<VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal" >
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="Transparent" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState Name="Selected">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="Transparent" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

@eli191
Copy link

eli191 commented Jun 15, 2020

@LeoJHarris I ended up with SelectionMode="None" and custom GestureRecognizer like this for example

 <DataTemplate>
                                        <StackLayout
                                            InputTransparent="False">
                                            <syncEffectsView:SfEffectsView
                                                TouchDownEffects="Ripple"
                                                RippleColor="{DynamicResource AccentColor}"
                                                CornerRadius="8"
                                                InputTransparent="True">
                                                <Label
                                                    InputTransparent="True"
                                                    Padding="5,15"
                                                    HorizontalOptions="Center"
                                                    VerticalOptions="Center"
                                                    Text="{Binding}"
                                                    TextColor="{DynamicResource TextColor}"
                                                    FontSize="12">
                                                </Label>
                                            </syncEffectsView:SfEffectsView>
                                            <StackLayout.GestureRecognizers>
                                                <TapGestureRecognizer
                                                    Command="{prism:NavigateTo 'NewPage'}">
                                                    <TapGestureRecognizer.CommandParameter>
                                                        <prism:NavigationParameters>
                                                            <prism:NavigationParameter
                                                                Key="Book"
                                                                Value="{Binding ViewModel.SelectedBook.BookName, Source={x:Reference book_page}}" />
                                                            <prism:NavigationParameter
                                                                Key="BookId"
                                                                Value="{Binding ViewModel.SelectedBookId, Source={x:Reference book_page}}" />
                                                        </prism:NavigationParameters>
                                                    </TapGestureRecognizer.CommandParameter>
                                                </TapGestureRecognizer>
                                            </StackLayout.GestureRecognizers>
                                        </StackLayout>
                                    </DataTemplate>

@LeoJHarris
Copy link

LeoJHarris commented Jun 16, 2020

@eli191 I thought about doing that, not a biggie atm to have the gray background color on selected rows. We can wait it out if they fix this issue soon otherwise Ill look at implementing something similiar to that.

@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label Jul 30, 2020
@samhouts samhouts added this to In Progress in .NET MAUI Backlog Jul 30, 2020
@samhouts samhouts moved this from Backlog to In Progress in CollectionView Jul 30, 2020
@samhouts samhouts removed this from In Progress in .NET MAUI Backlog Aug 4, 2020
@samhouts samhouts added this to In Progress in vNext+1 (5.0.0) Aug 11, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@timahrentlov
Copy link

This really needs fixing.

@developer9969
Copy link

Hi wondering if this will be fixed, whatever I do I cannot remove the gray background.Any suggestion

CollectionView automation moved this from In Progress to Done Nov 1, 2020
vNext+1 (5.0.0) automation moved this from In Progress to Done Nov 1, 2020
rmarinho pushed a commit that referenced this issue Nov 1, 2020
#12651) fixes #7790 fixes #9590 fixes #11421

* Correction of CollectionView to work properly with selected item color when using VisualState on Android and iOS

* Update TemplatedCell.cs

* Update TemplatedItemViewHolder.cs

* Add scaling to make the bug more obvious

* Make fix work for on-template visual states on iOS

* Make fix for for on-template visual states on Android

* Minor performance optimization

* Make method name more explicit

* Spaces -> Tabs

* Fix UI test failure (item 3 doesn't fit on the screen anymore)

Co-authored-by: Rony Mesquita <ronymesquitadasilva@gmail.com>
@samhouts samhouts moved this from Done to In Progress in CollectionView Nov 3, 2020
@hartez hartez moved this from In Progress to Done in CollectionView Nov 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.