Background color of selecteditem in listview xamarin.forms

安稳与你 提交于 2019-12-05 02:56:23

问题


When working with ListView in Xamarin.forms, on selecting an item, the item remains like that with a rather ugly background colour. Can i disable this feature?

Here's my code:

<StackLayout>
  <ListView x:Name="FoodList" HasUnevenRows="True" CachingStrategy="RecycleElement" ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="0,15,0,0" >
           <StackLayout Orientation="Horizontal" BackgroundColor="White" >
            <Image Source="{Binding image_url}" Aspect="AspectFill" HeightRequest="200" WidthRequest="200"/>
            <StackLayout Orientation="Vertical">
              <Label Text="{Binding food_name}" TextColor="Black" Style="{StaticResource MainLisTtext}" />
              <Label Text="{Binding price}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
              <Label Text="{Binding food_description}" TextColor="Black" Style="{StaticResource SubLisTtext}" />
            </StackLayout>
            </StackLayout>
          </StackLayout>
        </ViewCell>vc
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

回答1:


This can be accomplished by using Custom Renderers

iOS Custom Renderer

Edit the SelectionStyle property on iOS.

Below is an example that sets the UITableViewCellSelectionStyle to None.

using System;

using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using ListViewSample.iOS;

[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.iOS
{
    public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);

            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            return cell;
        }
    }
}

Android Custom Renderer

  1. Create a new drawable, ViewCellBackground.xml and save it to the Resources>drawable folder:

    <?xml version="1.0" encoding="UTF-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" >
            <shape android:shape="rectangle">
                <!--Change the selected color by modifying this hex value-->
                <solid android:color="#FFFFFF" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF" />
            </shape>
        </item>
    </selector>
    
  2. Create Custom Renderer for the ViewCell

    using System;
    
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    using ListViewSample.Droid;
    
    [assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
    namespace ListViewSample.Droid
    {
        public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
        {
            protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
            {
                var cell = base.GetCellCore(item, convertView, parent, context);
    
                cell.SetBackgroundResource(Resource.Drawable.ViewCellBackground);
    
                return cell;
            }
        }
    }
    

Edit: Removed implementation without Custom Renderers


Sample Xamarin.Forms ListView App

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace ListViewSample
{
    public class CustomViewCell : ViewCell
    {
        public CustomViewCell()
        {
            View = new Label
            {
                Text = "Hello World"
            };
        }
    }

    public class ListViewContentPage : ContentPage
    {
        public ListViewContentPage()
        {
            var itemSourceList = new List<CustomViewCell>();
            itemSourceList.Add(new CustomViewCell());
            itemSourceList.Add(new CustomViewCell());

            var listView = new ListView();
            listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
            listView.ItemsSource = itemSourceList;
            listView.SeparatorVisibility = SeparatorVisibility.None;

            Content = listView;
        }
    }

    public class App : Application
    {
        public App()
        {
            // The root page of your application

            MainPage = new NavigationPage(new ListViewContentPage());
        }
    }
}


来源:https://stackoverflow.com/questions/40958383/background-color-of-selecteditem-in-listview-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!