locked
ListView desperately needs a SelectedItemBackgroundColor property RRS feed

  • Question

  • User66401 posted

    It's a very basic thing, nobody wants an ugly grey (or blue) bar to appear as the background when you select an item in a ListView. It looks terrible from a UI point of view. Everybody has been implementing their own workarounds for this (custom renderers etc) but it is getting out of hand.

    The ViewCellRenderer custom renderer approach works fine with a ListView with the default caching strategy ListViewCachingStrategy.RetainElement but it does NOT work if you set it to ListViewCachingStrategy.RecycleElement or ListViewCachingStrategy.RecycleElementAndDataTemplate .

    If you need a different caching strategy you have to go one step further and create a custom ListViewRenderer and then assign your own UITableViewDelegate based delegate, and from there you need to override the WillDisplay method in order to eventually be able to set the selected item's background color to something that matches your app's color scheme. And after all this you'll find there are problems with the fact that you're replacing the "default delegate". (note: conversely and ironically this ListViewRenderer approach seems to work ok with a ListView that has a non-default caching strategy set but it does NOT work with the default caching strategy - in the latter case it doesn't pick up the ItemSelected event anymore). The problem with overwriting the default delegate is described here: https://forums.xamarin.com/discussion/42863/setting-delegate-of-control-in-custom-renderer-results-in-lost-functionality

    The problem and various workarounds that people have been trying is summarised here (but still there are some problems as mentioned in paragraph above): * https://forums.xamarin.com/discussion/20798/listview-selected-item-background-color * https://stackoverflow.com/posts/47054718

    In short this is madness that each developer must go through all this pain for such a simple thing.

    To solve all this a simple SelectedItemBackgroundColor property should be added to the ListView class. It won't be difficult to add the required code (just some code in the custom renderers will do the trick). Then we won't have to worry about all these custom renders or about how to handle the overwritten default delegate.

    Wednesday, November 1, 2017 11:10 PM

All replies

  • User173776 posted

    I wholeheartedly support this request

    Wednesday, November 8, 2017 10:28 AM
  • User5320 posted

    We need this BASIC functionality it's just a simple color, nothing fancy(gradient, images, ...)

    Wednesday, November 15, 2017 9:12 AM
  • User356054 posted

    I totally agree. Xamarin needs more of the simple things to simply work rather than having to lookup how to create customrenderers for a.. color.

    Tuesday, November 21, 2017 11:15 AM
  • User271590 posted

    This should of been a priority feature when the ListViewRenderer was first implemented. Would be nice if you could alter the colour of the text and background when selected in the styling of the application.

    Thursday, November 23, 2017 1:59 PM
  • User191123 posted

    Why is it not possible to like this post...

    +1 on the suggestion

    Friday, November 24, 2017 9:01 AM
  • User257530 posted

    +1

    Thursday, November 30, 2017 8:06 AM
  • User331804 posted

    I support this. Listview need some love.

    Thursday, November 30, 2017 10:59 AM
  • User359711 posted

    I'll give my vote for this!

    Friday, December 1, 2017 7:38 PM
  • User350192 posted

    +1

    Thursday, December 7, 2017 1:31 PM
  • User155041 posted

    +1

    Friday, December 8, 2017 12:46 PM
  • User253743 posted

    An enthusiastic +1!

    Thursday, December 21, 2017 9:19 PM
  • User288404 posted

    YES.PLEASE. THUMBS UP

    Wednesday, January 3, 2018 3:59 AM
  • User331674 posted

    This needs to happen.

    Monday, January 15, 2018 4:51 PM
  • User364739 posted

    +1

    Monday, January 29, 2018 9:55 AM
  • User8800 posted

    +1

    Monday, April 2, 2018 4:54 AM
  • User250583 posted

    +1

    Wednesday, April 18, 2018 11:24 PM
  • User368144 posted

    +1

    Wednesday, May 2, 2018 1:41 PM
  • User120147 posted

    +100000

    Saturday, May 5, 2018 12:29 AM
  • User167259 posted

    This should be marked as in progress. Looking at Github it looks like the implementation is almost complete.

    https://github.com/xamarin/Xamarin.Forms/pull/2050

    Monday, May 7, 2018 2:49 PM
  • User388648 posted

    Please, this is absolutely essential.

    Thursday, August 29, 2019 6:49 PM
  • User76049 posted

    Can be easily done with a DataTrigger in the template and some selection logic in the viewmodel tbh.

    Or if your an Indy developer, just use sfListview, if you have less than 5 devs and turn over less than 1 million USD, no I don't work for Syncfusion but I work on a project that uses their products commercially so it's a bargain...trust me.

    https://www.syncfusion.com/products/communitylicense

    Thursday, August 29, 2019 9:04 PM
  • User76049 posted

    Also there is zero point bitching here, honestly. The forms team don't really look in here.

    Github https://github.com/xamarin/Xamarin.Forms

    Or annoy people on twitter but that rarely works.

    Thursday, August 29, 2019 9:06 PM
  • User388648 posted

    @NMackay said: Can be easily done with a DataTrigger in the template and some selection logic in the viewmodel tbh.

    Or if your an Indy developer, just use sfListview, if you have less than 5 devs and turn over less than 1 million USD, no I don't work for Syncfusion but I work on a project that uses their products commercially so it's a bargain...trust me.

    https://www.syncfusion.com/products/communitylicense

    Hey @NMackay. Don't worry, I won't take you as a Syncfusion vendor... I understand it can be cheap, but it just feels frustrating to be paying for such a simple thing (just coloring a ListView item when selected!!!). Thanks though.

    Regarding your first point, do you have any code to take a look at your DataTrigger workaround? Be aware that it would be of great help for all the community

    Thursday, August 29, 2019 11:42 PM
  • User76049 posted

    @titoleru said:

    @NMackay said: Can be easily done with a DataTrigger in the template and some selection logic in the viewmodel tbh.

    Or if your an Indy developer, just use sfListview, if you have less than 5 devs and turn over less than 1 million USD, no I don't work for Syncfusion but I work on a project that uses their products commercially so it's a bargain...trust me.

    https://www.syncfusion.com/products/communitylicense

    Hey @NMackay. Don't worry, I won't take you as a Syncfusion vendor... I understand it can be cheap, but it just feels frustrating to be paying for such a simple thing (just coloring a ListView item when selected!!!). Thanks though.

    Regarding your first point, do you have any code to take a look at your DataTrigger workaround? Be aware that it would be of great help for all the community

    Totally get it, in 2019 to see the daft orange color in Android...I know. CollectionView will take over for vanilla Listview but someone posted a similar approach to what I've used here.

    https://stackoverflow.com/questions/43832726/xamarin-listview-selected-item-appearance

    It's not perfect but overriding the data template background color gets round the problem.

    Thursday, August 29, 2019 11:48 PM
  • User388648 posted

    Hi @NMackay, I didn't get that working either... I think the problem is not in the code itself, it's just that newer versions of Xamarin forms have completely broken all of these ListView implementations because they've changed the way they approached their ListViewCachingStrategy. Please check this link for more info.

    However, sync fusion:SfListView works perfectly for those interested: (add first corresponding Nuget Package "Syncfusion.Xamarin.SfListView" and use it in your XAML)

          <syncfusion:SfListView  x:Name="lvMenu"
                                    SelectionMode="Single"
                                    SelectionGesture="Tap"
                                    SelectionBackgroundColor="#009B66"
                                    ItemsSource="{Binding Durations}"
                                    SelectedItem="{Binding OnDurationSelected}">
                <syncfusion:SfListView.ItemTemplate>
                    <DataTemplate>
                            <StackLayout Spacing="10" HorizontalOptions="Center" VerticalOptions="Center">
                                <Label TextColor="Gray" Font="20" Text="{Binding}"/>
                            </StackLayout>
                    </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>
            </syncfusion:SfListView>
    

    Many thanks!

    Saturday, August 31, 2019 12:09 AM